ASP.NET - Add Email Message to Outlook Draft Folder with Attachements

I was recently looking for a way to add an email to a user's draft folder with attachements. Exchange web services was the ticket.  See the screen shot and code snippet attached.

The trick is to add to a user's outlook draft folder you need a user id with higher level rights and allow impersonation must be turned on in Exchange.

using System.DirectoryServices;
using Microsoft.Exchange.WebServices.Data;

namespace ExchangeWSTest
{
    public partial class ActiveXTest : System.Web.UI.Page
    {       
              
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
        protected void Page_Load(object sender, EventArgs e)
        {
            service.Credentials = new WebCredentials("youremail", "yourpassword");
            service.Url = new Uri("https://client.yourexchangeserver.com/ews/exchange.asmx");           
        }

        protected void BtnCreateMsg_Click(object sender, EventArgs e)
        {          
            EmailMessage message = new EmailMessage(service);
            message.Subject = TxtSubject.Text;

            string MSG = TxtBody.Text;
            MSG = MSG.Replace("\n", "
"); message.Body = MSG; message.Attachments.AddFileAttachment(@"C:\temp\TEST Word Doc.docx"); message.Attachments.AddFileAttachment(@"C:\temp\TestPDF.pdf"); message.Save(); LblMsg.Text = "Email created, please check your draft folder"; } } }
Comments are closed