sending E-mail error with outlook

32 次查看(过去 30 天)
I am trying to send an email, using the MATLAB sendmail function.
% User input
source = 'aaaa@outlook.com'; %from address (gmail)
destination = 'bbbbb@outloook.com'; %to address (any mail service)
myEmailPassword = '123456'; %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 Outlook
setpref('Internet','E_mail',source);
setpref('Internet','SMTP_Server','smtp.office365.com');
setpref('Internet','SMTP_Username',source);
setpref('Internet','SMTP_Password',myEmailPassword);
% Outlook server.
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.ssl.trust','smtp.office365.com');
props.setProperty('mail.smtp.user',source);
props.setProperty('mail.smtp.password',myEmailPassword);
props.setProperty('mail.smtp.host','smtp.office365.com');
props.setProperty('mail.smtp.port','587');
props.setProperty('mail.smtp.auth', 'false');
props.setProperty('mail.smtp.starttls.enable', 'true');
% Send the email
sendmail(destination,subj,msg);
While using this scritp code I am getting error :
Error using sendmail
530 5.7.57 Client not authenticated to send mail. [VI1PR08CA0229.eurprd08.prod.outlook.com
2023-10-03T15:18:10.674Z 08DBC23A7C55B27A]
Error in mail (line 25)
sendmail(destination,subj,msg);
What should I do for this error?
Thanks for helping.

回答(2 个)

Ayush
Ayush 2023-10-5
The ActiveX/COM interface can be used to control MS Outlook from MATLAB and send e-mails while using all the features available in MS Outlook. The function below is an example of how this can be done. Note that this function works almost the same as SENDMAIL.
function sendolmail(to,subject,body,attachments)
%Sends email using MS Outlook. The format of the function is
%Similar to the SENDMAIL command.
% Create object and set parameters.
h = actxserver('outlook.Application');
mail = h.CreateItem('olMail');
mail.Subject = subject;
% multiple recipients
if length(to) > 1
to = strjoin(to,';')
endmail.To = to;
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = body;
% Add attachments, if specified.
if nargin == 4
for i = 1:length(attachments)
mail.attachments.Add(attachments{i});
end
end
% Send message and release object.
mail.Send;
h.release;
Hope this helps!
  2 个评论
Satoshi Furukawa
Satoshi Furukawa 2023-10-25
Could you tell me how to create a body with forced line breaks.
For example, even if I set a line break text using the "newline" function,
it will be a single line on Outlook's email screen.
t_body = ("Line1 of message" + newline +"Line2 of message" + newline + "Line3 of message" )
t_body =
"Line1 of message
Line2 of message
Line3 of message"
[result]
Chris Nygren
Chris Nygren 2023-12-12
Is this method of using
h = actxserver('outlook.Application');
mail = h.CreateItem('olMail');
etc
mail.Send;
h.release;
more reliable than using the sendolmail function? Using sendolmail, I've been inconsistantly getting this error 'ERROR: The process "outlook.exe" not found. '

请先登录,再进行评论。


Paul Furlow
Paul Furlow 2024-4-17
To work in R2024a, I changed the line
mail = h.CreateItem('olMail');
with the following:
mail = h.CreateItem('olMailItem');

Community Treasure Hunt

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

Start Hunting!

Translated by