Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Using e-mail with matlab

Asked by Gloust

Gloust (view profile)

on 4 Sep 2012


I would truly appreciate it if anyone may help me with this. I would like to know what, perhaps, is wrong with the collection of code herein provided to send e-mails from matlab. In this code, I am using my account and have not included some information, such as my password or the e-mail to which I am sending, for privacy purposes:

mail_address = ''; user_name = 'puma31sumsum'; password = '*******'; smtp_server = '';

setpref('Internet', 'E_mail', mail_address); setpref('Internet', 'SMTP_Username', user_name); setpref('Internet', 'SMTP_Password', password); setpref('Internet', 'SMTP_Server', smtp_server); props = java.lang.System.getProperties; props.setProperty('mail.smtp.auth', 'true'); props.setProperty('mail.smtp.socketFactory.class', ''); props.setProperty('mail.smtp.socketFactory.port', '465'); sendmail('another email', 'Mail from me','Hello')

I am getting the following message when I try to run it:

Error in ==> sendmail at 17 sendmail('another email', 'Mail from me','Hello') ??? Attempt to execute SCRIPT sendmail as a function

In addition to this, I would like to know how I may run a loop. That is, if I wanted MATLAB to send out an e-mail every 30 seconds, how would I do this? Thank you very much.

1 Comment

Jan Simon

Jan Simon (view profile)

on 4 Sep 2012

Please format your code. See the "? Help" button for instructions.


Gloust (view profile)


No tags are associated with this question.


No products are associated with this question.

1 Answer

Answer by Matt Tearle

Matt Tearle (view profile)

on 4 Sep 2012
Edited by Matt Tearle

Matt Tearle (view profile)

on 4 Sep 2012
Accepted answer

From the error message, it looks like your program is called sendmail. This is bad, because you're trying to call the sendmail function from it!

which sendmail -all

will verify this.

Try renaming your program.

To clarify: search the documentation for 'function precedence' to see a list of how MATLAB resolves a variable/script/function call. If you have a sendmail.m in your current directory, it will take precedence over the sendmail that ships with MATLAB. So inside your sendmail it's trying to call your sendmail again, not the MATLAB sendmail function.

EDIT TO ADD: regarding your additional question, a very simple way is to use a for-loop with a pause(30). This will tie up MATLAB while the program is executing (ie you won't have access to the command prompt). Another way would be to create a timer object, with a callback that calls your email program:

t = timer('TasksToExecute',5,'Period',2,'ExecutionMode','FixedRate',...
        'TimerFcn',@(~,~) disp('hello world'))

(replace the hello world callback with a call to your program)



Gloust (view profile)

on 4 Sep 2012

Ah, yes. Thank you very much for providing me with relief from this. It is now giving me this error proclamation:

??? Error using ==> sendmail at 164 Could not connect to SMTP host:, port: 25; Connection refused

Error in ==> sendemail at 17 sendmail('', 'Mail from me','Hello')

Matt Tearle

Matt Tearle (view profile)

on 4 Sep 2012

This looks like a problem with your email settings -- name, password, mail server...?


Gloust (view profile)

on 5 Sep 2012

Thank you. It works as of now; it was the SMTP server which needed slight modification. I truly appreciate your help and expertise.

Matt Tearle

Matt Tearle (view profile)

Contact us