Problem with : Error using sendmail; Can't send command to SMTP host; Remote host closed connection during handshake

Hi I am trying to sendmail using the below, but i get :
Error using sendmail; Can't send command to SMTP host; Remote host closed connection during handshake
mail='#mail@mail';
password='#password';
server = '#smtp.server.com';
Subject = 'Test';
Text = 'Test';
setpref('Internet','E_mail', mail);
setpref('Internet','SMTP_Server', server);
setpref('Internet','SMTP_Username', mail);
setpref('Internet','SMTP_Password', password);
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.port', '587');
props.setProperty('mail.smtp.starttls.enable','true');
sendmail('#mail@mail', Subject, Text)
Any ideas what the problem is and how to fix it?
Thank you

댓글 수: 2

Hi,
I'm having the same issue. Did you get this resolved? If so would you mind sharing the resolution please.
Thank you.
Can you please specify the version of MATLAB that you are using and also the mail server you are trying to establish a connection on? I tried the same code as provided above using outlook smtp server on MATLAB R2024a and did not get any error.
Thanks.

댓글을 달려면 로그인하십시오.

 채택된 답변

My understanding is that the error you are encountering is related to the TLS protocol negotiation failing during the initial SMTP handshake with the email provider SMTP servers.
As mentioned in the MathWorks documentation available here https://www.mathworks.com/help/matlab/ref/sendmail.html ‘sendmail’ supports email servers with Transport Layer Security (TLS) 1.0. However, most email providers no longer support TLS 1.0 .
I have also faced a similar issue in the past. Here’s how I was able to resolve this issue:
  1. Download the following JAR file: https://github.com/javaee/javamail/releases/download/JAVAMAIL-1_6_2/javax.mail.jar
  2. Rename this JAR file to mail.jar.
  3. Replace <matlabroot>/java/jarext/axis2/mail.jar with the downloaded JAR file.
  4. Restart MATLAB.
  5. Add the following line to your code before using the “sendmail” function:
props.setProperty('mail.smtp.ssl.protocols', "TLSv1.2");
Here’s how your wrapper function should look like:
function sendEmailWrapper(senderEmailAddress, senderPassword, recipientEmailAddress, emailSubject, emailBody)
mail = senderEmailAddress;
password = senderPassword;
server = 'smtp.server.com';
props = java.lang.System.getProperties;
props.remove('mail.smtp.socketFactory.class');
props.setProperty('mail.smtp.port', '587');
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.starttls.enable','true');
props.setProperty('mail.debug', 'true');
props.setProperty('mail.smtp.ssl.protocols', "TLSv1.2");
setpref('Internet','E_mail', mail);
setpref('Internet','SMTP_Server', server);
setpref('Internet','SMTP_Username', mail);
setpref('Internet','SMTP_Password', password);
sendmail(recipientEmailAddress, emailSubject, emailBody);
end
Please confirm that the DEBUG output from “sendmail” has the following line: "DEBUG: JavaMail version 1.6.2" and NOT 1.4ae.
This should be able to resolve your issue.
Hope this helps!

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Call Web Services from MATLAB Using HTTP에 대해 자세히 알아보기

질문:

Nik
2023년 9월 20일

편집:

2025년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by