Problem with : Error using sendmail; Can't send command to SMTP host; Remote host closed connection during handshake
115 次查看(过去 30 天)
显示 更早的评论
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 个评论
Amanda Fortuna
2024-4-15
Hi,
I'm having the same issue. Did you get this resolved? If so would you mind sharing the resolution please.
Thank you.
Rajanya
2024-8-9
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.
回答(1 个)
Samay Sagar
2024-8-23
编辑:Samay Sagar
2024-8-23
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:
- Download the following JAR file: https://github.com/javaee/javamail/releases/download/JAVAMAIL-1_6_2/javax.mail.jar
- Rename this JAR file to mail.jar.
- Replace <matlabroot>/java/jarext/axis2/mail.jar with the downloaded JAR file.
- Restart MATLAB.
- 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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Web Services 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!