MATLAB: Sending E-mail error

emailerrorgmailsend emailsendmail

Türk:
Merhaba;
MATLAB ile e-posta göndermek istiyorum ama bu kodları buldum (Sendmail kullanarak hata (satır 172)
İstisna okuma cevabı;
sun.security.validator.ValidatorException: PKIX yolu oluşturulamadı: sun.security.provider.certpath.SunCertPathBuilderException: geçerli bulunamıyor
istenen hedefe yönelik sertifika yolu
Bu hatayla karşılaştığım sorun nedir?
Teşekkürler…
English:
Hi;
I want to send e-mail with MATLAB but I found these codes (Error using sendmail (line 172)
Exception reading response;
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid
certification path to requested target )
What's the problem I'm having with this error?
Thanks…
%
% parameters
mail = 'mymail@gmail.com'; % my gmail address
password = 'mypassword'; % my gmail password
host = 'smtp.mail.com';
% preferences
setpref('Internet','SMTP_Server', host);
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');
% Send the email
sendmail(mail,'Test from MATLAB','Hello! This is a test from MATLAB!')

Best Answer

Here's a quick example of a demo that produces an email sent from a gmail account (specifically). Note that to send an email through your gmail account you have to turn off the app security which greatly reduces the security of your gmail account. One solution to this problem is creating a new gmail account for the sole purpose of sending emails from this script.
To turn off app security, sign into gmail and access this link: https://myaccount.google.com/lesssecureapps
[Update] See comments below regarding common problems with antivirus software and firewalls that might prevent the email from being sent.
% User input
source = 'myFakeEmail@gmail.com'; %from address (gmail)
destination = 'kofte@foobar.com'; %to address (any mail service)
myEmailPassword = 'sifre123'; %the password to the 'from' account
subj = 'This is the subject line of the email'; % subject line
msg = 'This is the main body of the email.'; % main body of email.
%set up SMTP service for Gmail
setpref('Internet','E_mail',source);
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','SMTP_Username',source);
setpref('Internet','SMTP_Password',myEmailPassword);
% 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.socketFactory.port','465');
% Send the email
sendmail(destination,subj,msg);
% [Optional] Remove the preferences (for privacy reasons)
setpref('Internet','E_mail','');
setpref('Internet','SMTP_Server','''');
setpref('Internet','SMTP_Username','');
setpref('Internet','SMTP_Password','');
Related Question