以163邮箱为例进行介绍
前期准备
开启POP3/SMTP服务后,你就可以在邮件客户端软件中配置邮箱,通过这些服务器发送和接收邮件。
-
登录163邮箱,点击页面右上角的“设置”按钮,选择“POP3/IMAP/SMTP/Exchange”选项。
-
在该页面中,找到“POP3/SMTP服务”这一栏,点击“开启”按钮。
-
开启后,系统会提示你设置客户端授权码,按照提示操作生成授权码并妥善保存。
-
页面会显示开启成功的信息,同时提供POP3服务器(pop.163.com)和SMTP服务器(smtp.163.com)等相关信息。
发送文字内容邮件
1. 首先需要设置:
- 发送邮件的邮箱地址
- 发送邮件的邮箱授权码——在开启POP3/SMTP服务时提供的授权码
2. 设置服务器信息;
3. 设置收件人、邮件题目、邮件内容,通过matlab自带的sendmail函数发送;
function mailme(receiver,mailtitle,mailcontent)
% 账号设置
mail = '****@163.com'; % ①发送邮件的邮箱地址
password = '****'; % ②发送邮件邮箱授权码
% 服务器设置
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server','smtp.163.com'); % ③SMTP服务器,这里我选择了163邮箱
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);
fprintf("发送成功");
end
测试:
receiver = '****@163.com'; % 设置收件人邮箱地址
title = 'test'; % 设置邮件标题
context = '从****@163.com发送的测试邮件'; % 设置邮件内容
mailme(receiver,title,context); % 调用函数发送邮件
发送图片内容邮件
设置步骤与上无异,参考上述设置步骤
需要提前将图片保存,并给出图片在计算机中的地址
function mailwithFigure(receiver, mailtitle, mailcontent, attachment)
% 账号设置
mail = '****@163.com'; % ①发送邮件的邮箱地址
password = '****'; % ②发送邮件邮箱授权码
% 服务器设置
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server','smtp.163.com'); % ③SMTP服务器,这里我选择了163邮箱
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, attachment);
fprintf("发送成功\n");
end
测试:
receiver = '****@163.com'; % 设置收件人邮箱地址
title = 'test'; % 设置邮件题目
context = '从****@163.com发送的测试邮件'; % 设置邮件文字内容
attachment = './static_map.png'; % 设置图片路径
mailwithFigure(receiver,title,context,attachment); % 调用函数发送邮件
发送表格内容邮件
设置步骤与上无异,参考上述设置步骤
提前将表格csv格式,并给出表格csv在计算机中的地址
function mailwithTable(receiver, mailtitle, mailcontent, tablename, attachment)
% 账号设置
mail = '****@163.com'; % ①发送邮件的邮箱地址
password = '****'; % ②发送邮件邮箱授权码
% 服务器设置
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server','smtp.163.com');
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');
T = readtable_findThreshold(tablename);
if size(T,1) >= 1
text = table2text(T);
mailcontent = [mailcontent 10 text];
% 发送邮件,检测是否包含图片附件
if nargin == 4
sendmail(receiver, mailtitle, mailcontent);
elseif nargin == 5
sendmail(receiver, mailtitle, mailcontent, attachment);
end
fprintf("发送成功\n");
else
fprintf("无超出阈值数据");
end
end
测试
receiver = '****@qq.com';
title = 'test';
location = '123.411817,41.766384';
context = '从****@163.com发送的测试邮件';
url = sprintf('https://uri.amap.com/marker?position=%s', location);
context = [context 10 url];
tablename = "data.csv"; % 设置表格路径
attachment = 'static_map.png';
mailwithTable(receiver, title, context, tablename,attachment)