Sending Email thorugh MATLAB
Electronic mail or E-mail is around us since 1993. The pioneer of this mode of communication was no doubt 'Hotmail' which is now owned by Microsoft. From computer based application, they have taken it to web and publicize it to an extent that today the official communication seems incomplete without it. Although the first of the web email was sent through a CERN project.
Sending Email thorugh MATLAB |
In this MATLAB code I have used MATLAB JAVA classes to use their SMTP access property. For more about EMAIL and SMTP or other protocols which is used to send mails please refer web.
The MATLAB is commented well, but if you have any doubt please comment below. MATLAB code in bold for better contrast from the comments.
%%MATLAB Code for Sending Email %%
clc;close all;clear all;
mail_to='[email protected]'; %replace with your choice of mail id
%*required
service='gmail';
%*required
%set service='yahoo' for yahoo.com
%set service='gmail' for gmail.com
%set service='live' for live.com
%set service='outlook' for outlook.com
%set service='anyother' for anyother.com
ext='.com';
%*required
%set ext='.com' for .com domain
%set ext='.in' for .in domain
%set ext='.co.in' for .co.in domain
%set ext='.mobi' for .mobi domain
%set ext='.xyz' for .xyz domain
user_name='vibhutesh1494'; %replace the string with your user name
%*required
password='xyz'; %replace the string with your password (sender's password)
%*required
subject='Anything you want'; %replace the string with subject of your mail
%if you want subject be blank simply make subject=''
body='Type you email content here'; %replace it with content of your mail
%if you want to send a blank mail simply make body=''
attachment='Provide full path of the file to be attached';
%if you dont want to attach anything simply replace attachment='Path' by attachment=[]
%setting up email server by using java classes
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
email_port='465';
%Change this to port 25, 465, 587, 995 based on server properties
%port 25 for smtp
%port 465 for secured/encrypted smtp
%port 587 for TLS enabled servers
%port 995 for SSL/TLS encrypted POP
props.setProperty('mail.smtp.socketFactory.port',email_port);
switch service
% add in different cases based on server settings
case 'gmail'
id = [user_name,'@gmail.com'];
setpref('Internet','SMTP_Server','smtp.gmail.com');
case 'live'
id = [user_name,'@live.com'];
setpref('Internet','SMTP_Server','smtp.live.com');
case 'outlook'
id = [user_name,'@outlook.com'];
setpref('Internet','SMTP_Server','smtp-mail.outlook.com');
case 'yahoo'
id = [user_name,'@yahoo.com'];
setpref('Internet','SMTP_Server','smtp.mail.yahoo.com');
otherwise
id = [user_name,'@',service,'.com'];
setpref('Internet','SMTP_Server',['smtp.mail.',service,ext]);
end
%for login through your smtp settings
setpref('Internet','E_mail',id);
setpref('Internet','SMTP_Username',id);
setpref('Internet','SMTP_Password',password);
sendmail(mail_to,subject,body,attachment)
NOTE :
*Your mail could end up in SPAM folder particularly if you end up using port 25, so be sure to check this folder too.
*You must be connected to internet for using this code.
*For Gmail it is required to have a secure connection by default. If you get a security certificate error try fiddling up something in your GMAIL account setting to make the mailing transaction unsecured. (At-least for this program).
0 comments: