Not able to send an email with attachment
조회 수: 5 (최근 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!