Tuesday, February 18, 2014

How to send automated emails with attachments from Windows Phone apps

Step1: Download the LiveMailMesage tool (note that it comes with a FREE trial) and add references to your Windows Phone application project.

Step2: Create a new LiveMailMesage object:
 
LiveMailMessage mailMessage = new LiveMailMessage();

Step3: Set a Live/Hotmail account needed to connect to Microsoft Live SMTP mail server:
 
 
mailMessage.MicrosoftAccountEmail = myMicrosoftAccountEmail ;
mailMessage.MicrosoftAccountPassword = myMicrosoftAccountPassword;

Step4: Set some mail data:
 
 
 
mailMessage.To = myTo;
mailMessage.Subject = mySubject;
mailMessage.Body = myBody; //accepts text or HTML

Step5:  Add some attachments (max attach limit size currently is 50MB) , note that you can also use different  resources or a IsolatedStorage path:

 
 
 
 
mailMessage.AddAttachment("\resources\file.jpg");
mailMessage.AddAttachment("\rex\file.wav");
mailMessage.AddAttachment("\myFolder\file.mp3");
mailMessage.AddAttachment("\downloads\file.mp4");
 
Step6: Set message event handlers (send operation is asyncronous and not UI blocking)
 
 
 
mailMessage.Error +=  mailMessage_Error;
mailMessage.MailSent += mailMessage_MailSent;
mailMessage.Progress += mailMessage_Progress;

Step7:  Send email (async, runs on a separated thread) :
 
mailMessage.SendMail();

No comments:

Post a Comment