When I use the sendmail function, I got this error 'Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1' How can I fix this error?

36 ビュー (過去 30 日間)
Chaoran Han
Chaoran Han 2021 年 8 月 9 日
コメント済み: Chaoran Han 2021 年 8 月 13 日
Here is my code:
myaddress='example';
mypassword='example';
setpref('Internet','E_mail',myaddress);
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','SMTP_Username',myaddress);
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.remove( 'mail.smtp.socketFactory.class' );
props.setProperty('mail.smtp.socketFactory.port','465');
props.setProperty('mail.smtp.starttls.enable','true');
sendmail(myaddress,'test','This is a test.');
After running it, I got:
'Error using sendmail (line 179)
Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1'
What does it mean? Why I can't connect to SMTP host?

採用された回答

Gargi Patil
Gargi Patil 2021 年 8 月 13 日
Hi!
The given error could not be reproduced and the code shared by you successfully sends a mail as expected on MATLAB R2020b. Therefore, the origin of the error lies beyond the code.
The following reasons could be the cause:
  1. Gmail requires the account to have the option “Allow less secure apps” enabled in order to send mails from this code. Please note that this considerably reduces the security of your Gmail account. For further details, you can refer to the following thread: https://in.mathworks.com/matlabcentral/answers/433031-sending-e-mail-error#answer_349841
  2. It is recommended to use port 587 over port 465 for SMTP communications. Modern SMTP servers use port 587 as the default mail communication port and it is thus more reliable than port 465.
  3. Kindly verify that the network being used allows mails to be sent through less secure protocols. Additionally, ensure any firewalls or antivirus softwares are not blocking this communication.
  4. This error could also occur if the server certificate is not trusted.
  1 件のコメント
Chaoran Han
Chaoran Han 2021 年 8 月 13 日
Thank you very much, Gargi.
Let me try your suggestions one by one to see if thoes work.
Appreciate that!

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by