【问题标题】:Using the sendmail function to send an email through MATLAB使用 sendmail 函数通过 MATLAB 发送电子邮件
【发布时间】:2017-06-27 14:55:31
【问题描述】:

我正在尝试使用 MATLAB sendmail 函数发送电子邮件。我一直按照这个链接上Mathworks中sendmail函数分析的说明:https://uk.mathworks.com/help/matlab/import_export/sending-email.html

我在命令窗口中使用的代码如下:

setpref ('Internet','E_mail','myemailaddress@gmail.com');
setpref ('Internet','SMTP_Server','smtp.gmail.com') ;
sendmail('emailofreceiver@gmail.com','texttobesent') ;

运行这些命令后我收到的消息如下:

使用 sendmail 时出错(第 169 行)

无法连接到 SMTP 主机:smtp.gmail.com,端口:25;

连接超时:连接

据我了解,我必须根据返回的 gmail smtp 端口更改我调用的第二个 setpref 函数中的参数,尽管我不确定究竟应该包含什么参数。任何帮助将不胜感激!提前谢谢!

【问题讨论】:

  • 我没用过,但你不应该在首选项中输入密码吗?
  • 其实sendmail函数(R2016b中的第76行)调用getpref('Internet','SMTP_Password',''),所以你应该设置密码为setpref('Internet','SMTP_Password','mypassword');
  • 您好,感谢您的回答。我也尝试过这样做,但我从 MATLAB 得到了相同的消息。
  • @LuisMendo 我尝试运行这两个命令:getpref('Internet',SMTP_Username','myusername');getpref('Internet','SMTP_Password','mypassword') ;。但我仍然从 MATLAB 得到相同的结果。有任何想法吗?提前致谢!
  • (在已经打好的代码上加了这两条命令)

标签: matlab smtp gmail


【解决方案1】:

您可能还需要设置与 SSL 相关的内容。尝试在 SMTP_Username 和 SMTP_Password 之外添加以下内容,它应该适用于 gmail:

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.fallback', 'false');
props.setProperty('mail.smtp.socketFactory.port', '465');

【讨论】:

    【解决方案2】:

    正如@Xiangru Li 在他的回答中所说,确实需要与 SSL 相关的设置。但这还不够。最终,我不得不更改我的谷歌设置以打开对不太安全的应用程序的访问。可以在此链接中找到有关执行此操作的信息:https://support.google.com/accounts/answer/6010255?hl=en

    所以在这样做之后,下面的代码是成功的,我设法用它发送了一封电子邮件:

    setpref('Internet','SMTP_Server','smtp.gmail.com');  
    setpref('Internet','E_mail','myemailaddress');
    setpref('Internet','SMTP_Username','myusername');
    setpref('Internet','SMTP_Password','mypassword');
    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('emailofreceiver','testtobesent') ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 2016-02-21
      • 2017-01-20
      • 2018-07-22
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多