最近看到一段MATLAB发送邮件的代码,测试了一下可以,在此分享一下。
测试环境:MATLAB2010
function MySendMail
%% 发送邮件
a = rand(100);
DataPath = [matlabroot,filesep,'mydata.mat']; % filesep='\'
save DataPath a ; % 将变量a的值保存为mydata.mat
MailAddress = '发送邮件的邮箱地址';
password = '发送邮箱的密码';
setpref('Internet','E_mail',MailAddress);
setpref('Internet','SMTP_Server','smtp.163.com'); % STMP服务器地址,不同域名的邮箱地址有不同SMTP服务器地址,可以查询的
setpref('Internet','SMTP_Username',MailAddress); % 发送文件的邮箱
setpref('Internet','SMTP_Password',password);
props = java.lang.System.getProperties; % 使用Java
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', ...
'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','994');
subject = 'MATLAB 发的测试邮件'; % 邮件主题
content = '你好,这份邮件是我用MATLAB发的,数据见附件'; % 邮件内容
sendmail('接收邮件的邮箱地址',subject,content,DataPath); % 接收邮件的邮箱,主题名,内容和文件路径