how to send email from matlab??

13 views (last 30 days)
AYUSH RAJ ACHARYA
AYUSH RAJ ACHARYA on 19 Apr 2018
Answered: Rajanya on 9 Aug 2024
I have tried for the article posted by matlab but get many error mainly
Could not connect to SMTP host: 'my IP address', port: 'my port number'; Connection refused: connect
the code used was
mail: my email
password : my password
setpref('Internet','E_mail',mail); setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
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.socketFactory.port','465');
sendmail(mail,'Test from MATLAB','Hello! This is a test from MATLAB!')

Answers (1)

Rajanya
Rajanya on 9 Aug 2024
I was able to reproduce the issue with ‘sendmail()’ that you probably are facing. Maybe you could try adding the server details along with the mail address and password using ‘setpref(.)’.
Here’s how you can achieve it:
server = 'smtp-mail.outlook.com'; %(if you are using outlook)
setpref('Internet','SMTP_Server', server);
This should solve your issue in establishing a connection.
An alternative can also be to use TLS instead of SSL. TLS generally works on port 587 unlike SSL which works on 465. An example code could be like:
mail='your_mail_address';
password='your_password';
server = 'smtp-mail.outlook.com';
Subject = 'Some_subject';
Text = 'Some_text';
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('recipient_mail_address', Subject, Text)
Point to note: MATLAB’s ‘sendmail’ does not work with Gmail as it will be having authentication issues. You will need to “Allow less secure apps” in your account settings before setting up a connection on the Gmail server.
Feel free to reach out if you have any further queries regarding this.

Tags

Community Treasure Hunt

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

Start Hunting!