如何使用matlab发邮件 sendmail

13 篇文章 2 订阅
8 篇文章 1 订阅

当我们在电脑上运行一个耗时非常大的程序时,有时需要让程序运行完后自动将数据、结果通知自己,这个过程可以用matlab来完成。

MATLAB提供了一个sendmail函数以供使用:

sendmail(收件人邮箱,标题)
sendmail(收件人邮箱,标题,内容)
sendmail(收件人邮箱,标题,内容,附件名)

说明


  • sendmail(recipients,subject)recipients 发送具有指定 subject 的电子邮件。对于单个收件人,请将 recipients 指定为字符向量或字符串。对于多个收件人,请将 recipients 指定为字符向量元胞数组或字符串数组。subject 必须是字符向量或字符串。
  • sendmail(recipients,subject,message) 包括指定的 message。如果 message 是字符向量或字符串,则 sendmail 自动在 75 个字符处对文本换行。要强制在消息文本中换行,请使用 10,如示例中所示。如果 message 是字符向量元胞数组或字符串数组,则每个元素代表一行新文本。
  • sendmail(recipients,subject,message,attachments) 附加 attachments 输入参数中列出的文件。attachments 可以是字符向量、字符向量元胞数组或字符串数组。

示例


  • 将包含两个附件的消息发送到假设的电子邮件地址:
sendmail('user@otherdomain.com',...
         'Test subject','Test message',...
         {'folder/attach1.html','attach2.doc'});
  • 将包含强制换行符(使用 10)的消息发送到假设的电子邮件地址:
sendmail('user@otherdomain.com','New subject', ...
        ['Line1 of message' 10 'Line2 of message' 10 ...
         'Line3 of message' 10 'Line4 of message']);
  • 生成的消息为:
    Line1 of message
    Line2 of message
    Line3 of message
    Line4 of message

默认情况下,sendmail 函数不支持需要身份验证的电子邮件服务器。要支持这些服务器,请使用以下形式的命令更改您的系统设置并设置 SMTP 用户名和密码的预设:

setpref('Internet','SMTP_Server','my_server.example.com');
setpref('Internet','E_mail','my_email@example.com');
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');

同时还需要设置服务器端口:

setpref('Internet','SMTP_Username','myaddress@example.com');
setpref('Internet','SMTP_Password','mypassword');

如果使用QQ邮箱需要在设置–账户–POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务开启服务
在这里插入图片描述

  • 接下来就可以使用sendmail发送邮件了:
    sendmail(‘收件人’, ‘标题’, ‘内容’,‘附件路径’)

下面是一个示例,可以根据自己需求更改

 function sendmails(address,object,content)
if nargin==2
    content=object;
    object=[];
elseif nargin==1
    content=address;
    address='收件人邮箱';
    object='邮件主题';
elseif nargin==3
else
    error('参数错误');
end

mail='自己的邮箱';
password='邮箱密码';
setpref('Internet','E_mail',mail);
% setpref('Internet','SMTP_Server','smtp.gmail.com');% 如果用gmail邮箱,qq邮箱同理
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');    % 
% 设置邮箱发送服务器端口,这里是465端口
props.setProperty('mail.smtp.socketFactory.port','465');
sendmail(address,object,content);
 end
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肆拾伍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值