Not able to send an email with attachment
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I am not able to send an attachment in the email. Please see below mentioned code. I am receiving no error, code is running successfully however able to send an email but without an attachment. Requesting you to please let me know where I am making a mistake.
code :
function sendolmail(to,subject,body,attachments) to={'shantanu.belsare@igate.com'}; subject='Trial Email using Matlab'; body='Hi, This is a Trial Email send by using Matlab'; filename = ['Trial_Attachment.xlsx']; file = strcat('E:\00_SHANTANU BELSARE\',filename); attachments={file};
h = actxserver('outlook.Application'); for j=1:length(to) mail = h.CreateItem('olMail'); mail.Subject = subject;
mail.To = to{j};
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;
end
h.release;
end
1 个评论
chiyaan chandan
2020-7-21
Try this code
Once you run the code it may show Authentication error then follow this steps
1). Go to your Gmail account
2). manage your Google account
3) Security
4). Less secure app access (Turn on access)
then run this code
***************************************************
clc %% to clear the work space
clear all %% To clear the previous storage
%% uncomment this section if you know the file name
% A=xlsread('data.xlsx'); %% enter the excel file name
%% searching and selecting the file name
[filename, pathname] = uigetfile('*.*');
xfile1 = fullfile(pathname, filename);
Input_table = xlsread(xfile1);
A=xlswrite('data.xlsx',Input_table); %% creating the excle file (data.xlsx) from the selected file.
%% input section
Your_Emil_Id='chandanks@gmail.com'; %% Your mail id
my_password='**********'; %% mail id password
Sending_mail_id='chandanvishnu@gmail.com'; %% your sending mail id
%%
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','E_mail',Your_Emil_Id);
%% java script
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.starttls.enable','true');
%% Internet connection
setpref('Internet','SMTP_Username',Your_Emil_Id);
setpref('Internet','SMTP_Password',my_password);
dt=datestr(now,'mmmm dd, yyyy HH:MM:SS.FFF AM'); %% date and time calculation
sendmail(Sending_mail_id, 'MATLAB Test',dt,'data.xlsx');
回答(1 个)
Image Analyst
2015-10-28
It's possible that either your or the recipient's mail server is stripping off the attachment without telling you, especially if the extension is of certain executable file types. Check with your email service.
另请参阅
类别
在 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!