Monday, July 27, 2009

Open Outlook and create email w/ Visual Studio 2005 in C#?

Have a form w/ a button. When clicked it needs to open Microsoft Outlook and create an email that will populate the To, From, Subject, and Body. The To will be pulled from an access database and From, Subject, and Body will be predifined. It will also need to have a report from Crystal Reports attached to the email. Also I don't want the email to automatically send, the client says he may want to change the wording sometimes or add notes. So want it to be automated to the point the user only has to push sent (or add more text...). Any help is appreciated as I have found nothing online about this!!! AHHHHHHHHH!!!!!!!!!!!

Open Outlook and create email w/ Visual Studio 2005 in C#?
Because you're doing this in C#, I'll make some assumptions about your skill level by not going into detailed steps. I'll also assume you already now how to pull your data. Attaching the files is done using oMail.Attachments.Add().





Add a reference to the Outlook COM object.





Use the following in your Click event:





private void button1_Click(object sender, System.EventArgs e)


{


Outlook.Application oApp = new Outlook.Application();


Outlook._MailItem oMail = (Outlook._MailItem)


oApp.CreateItem


(Outlook.OlItemType.


olMailItem);





oMail.To = "email@email.com";


oMail.Subject = "Subject Here";


oMail.Body = "";





oMail.Display(true);


}





Pardon the funny breakdown, Yahoo likes to truncate.





Added: I just noticed you're doing this in VS2005, this code was created and tested in VS2003.





The Data Analyst - http://www.squidoo.com/thedataanalyst


No comments:

Post a Comment