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

99 views (last 30 days)
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 Comments
Amanda Fortuna
Amanda Fortuna on 15 Apr 2024
Hi,
I'm having the same issue. Did you get this resolved? If so would you mind sharing the resolution please.
Thank you.
Rajanya
Rajanya on 9 Aug 2024
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.

Sign in to comment.

Answers (1)

Samay Sagar
Samay Sagar on 23 Aug 2024
Edited: Samay Sagar on 23 Aug 2024
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 ‘sendemail’ 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!

Community Treasure Hunt

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

Start Hunting!