Filter löschen
Filter löschen

System throws an error when attempting to send email from Matlab

3 Ansichten (letzte 30 Tage)
Sudharsan K.R
Sudharsan K.R am 27 Mär. 2019
Beantwortet: Ishu am 6 Mai 2024
Below is the code I have used to send email from matlab and also the error message that i am seeing when i run the code.
Could someone please help me out
function send_mail_message(id,subject,message,attachment)
mail = 'email@gmail.com';
password = '******'
if nargin == 1
message = subject;
subject = '';
elseif nargin == 2
message = '';
attachment = '';
elseif nargin == 3
attachment = '';
end
% Send Mail ID
emailto = strcat('email2','@gmail.com');
subject='test';
message='This is a test mail from matlab';
attachment='F:\plot.txt'
%% Set up Gmail SMTP service.
% Then this code will set up the preferences properly:
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
% Gmail server.
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.starttls.enable', 'true' );
props.setProperty('mail.smtp.socketFactory.port','465');
%% Send the email
if strcmp(mail,'email2@gmail.com')
disp('Please provide your own gmail.')
disp('You can do that by modifying the first two lines of the code')
disp('after the comments.')
end
sendmail(emailto,subject,message,attachment)
send_mail_message('email.id','subject1','This is a test message','F:\plot.txt')
Error using sendmail
Could not connect to SMTP host: smtp.gmail.com, port: 25;
Connection refused: connect

Antworten (1)

Ishu
Ishu am 6 Mai 2024
Hi Sudharsan
I understand from your message that the connection to the SMTP host was refused, likely because the "sendmail" function is using port 25. Modern SMTP servers use port 587, as this port is more reliable and avoids the limitations associated with the lower-level port 25.
You can also refer to the following documentation for SMTP settings: https://developers.google.com/gmail/imap/imap-smtp.
I tried running the code with port 587, and it worked fine.
props.setProperty('mail.smtp.port', '587');
Hope it helps!

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by