File: ef230-2023-08/www/examples/send_email.m
Download
% email send example (NOT FINISHED)
% March, 2011
function main
clc; clear all; close all;
prompt = {'From:','To:','Re:','Message:','Server:','Username:'};
options.Resize='on';
dlg_title = 'Email writer';
num_lines = [1; 1; 1; 5; 1; 1];
def={'wschleter@gmail.com' ,'wschleter@utk.edu','test', 'How are you doing?','smtp.gmail.com','wschleter'};
answer = inputdlg(prompt,dlg_title,num_lines,def,options);
if isempty(answer)
return
end
tic
msg.from=answer{1};
msg.to=answer{2};
msg.subject=answer{3};
msg.content = answer{4};
msg.server = answer{5};
msg.username = answer{6};
msg.password = input_password('Enter email server password:')
setpref('Internet','E_mail',msg.from);
setpref('Internet','SMTP_Server',msg.server);
setpref('Internet','SMTP_Username',msg.username);
setpref('Internet','SMTP_Password',msg.password);
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', ...
'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','465');
attachment = uigetfile('', 'Select a file','MultiSelect','on');
if ~attachment || isempty(attachment)
sendmail(msg.to, msg.subject, msg.content);
else
sendmail(msg.to, msg.subject, msg.content, attachment);
end
toc
return
function pwd = input_password(prompt)
scrsz = get(0,'ScreenSize');
h=figure('keypressfcn',@gotkey,'windowstyle','modal','Position',[scrsz(3)/3 scrsz(4)/3 scrsz(3)/3 scrsz(4)/3])
axis off;
text(0,1,'Enter password:');
p=text(0,0,'_');
get(p)
set(h,'UserData','');
uiwait(h)
pwd = get(h,'UserData');
close(h)
return
function gotkey(s,e)
pwd = get(gcbf,'UserData');
switch (e.Key)
case 'return'
uiresume(gcbf);
case 'backspace'
if length(pwd)>1
pwd=pwd(1:end-1);
end
otherwise
pwd = [pwd e.Character];
end
set(gcbf,'UserData',pwd);
return