调用方式

1
2
3
4
5
receiver = "[email protected]";
mailtitle = "来自Matlab";
mailcontent = "Matlab已经执行完毕";
attachments = []; % attachments ='D:\status.txt'
SentMail(receiver, mailtitle, mailcontent, attachments)

SentMail 函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function SentMail(receiver, mailtitle, mailcontent, attachments)
% Example:
% SentMail('[email protected]', '来自Matlab的email', '运行完成', [])
% SentMail('[email protected]', '来自Matlab的email', '运行完成', 'D:\status.txt')
%

% 添加邮箱和IMAP/SMTP授权码
mail = '[email protected]';
password = '****'; % IMAP/SMTP

% 设置服务器,其中只有邮箱后缀需改动
setpref('Internet', 'E_mail', mail);
setpref('Internet', 'SMTP_Server', 'smtp.163.com'); % SMTP服务码
setpref('Internet', 'SMTP_Username', mail);
setpref('Internet', 'SMTP_Password', password);
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');

sendmail(receiver, mailtitle, mailcontent, attachments);
end