Send email via MATLAB

℡╲_俬逩灬. 提交于 2019-12-01 08:17:45

An alternative way on linux is to run a command line that send the email.

unix('echo "message" | mail -s "subject" example@gmail.com');

A similar method should be available for windows.

For Gmail

Change your settings to allow less secure apps to access your account. Go to the "Less secure apps" section in My Account. Next to "Access for less secure apps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)

In Matlab:

mail = 'user@otherdomain.com';
password = 'myPassword';

% Set up the preferences
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);

% The following is necessary only if you are using GMail as
% your SMTP server. 
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','465');

subject = 'Test subject';
message = 'Test message';

sendmail(mail,subject,message)

Simply declare

mail = 'user';

Drop the extension @service.com for the variable mail.

You can read https://support.kaspersky.com/4175 and get some IDEA about settings of your AntiVirus software then you can try to send email.

Here, I am going to explain using Gmail with port as 587 to send email.

Note: I will update the answer once I will get solutions for Hotmail & Yahoo.

I have tried sending emails in multiple ways that I have mentioned in the PDF which publicly available at Sending email(GMAIL) using MATLAB.

In Sending email(GMAIL) using MATLAB PDF, you will see the following with an excellent example codes and pictures.

  1. Sending email to a single user - (Only with Header message)
  2. Sending email to multiple users - (With Header and body messages)
  3. Sending email to multiple users - (With Header, body messages and 1 jpg file from system)
  4. Sending email to multiple users - (With Header, body messages and 2 attachments .jpg and .pdf)

Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!