Not able to send an email with attachment

15 views (last 30 days)
Shantanu
Shantanu on 28 Oct 2015
Commented: chiyaan chandan on 21 Jul 2020
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 Comment
chiyaan chandan
chiyaan chandan on 21 Jul 2020
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');

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 28 Oct 2015
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.
  1 Comment
Shantanu
Shantanu on 28 Oct 2015
Many thanks for your response. in both the case I have mentioned my email and also on the otherway (ie without using matlab or our normal manual process) I am able to recieve an email on my email id with an attachment.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!