System throws an error when attempting to send email from Matlab
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Ishu
2024-5-6
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!
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!