We help IT Professionals succeed at work.

starting an outlook email from inside a win form in C#

adamtrask
adamtrask asked
on
Good morning,,
I created an application in C# where, at some point, the user will need to use outlook  to send an email to a colleague.

The email address of the percipient as well as the body and subject matter are placed in text boxes in the win form waiting to be moved or copied to the outgoing email in Outlook.

So, basically my question is: how do I run outlook form inside a win form, select a new email copy the recipient email address and the body of text into the email and send it.

Thank you
Comment
Watch Question

Commented:
You can use outlook object to create the new email item with selected values
The only trick is not to call item.send instead call item.display this will show message window to user

Author

Commented:
FarWest: Would you give me an example, please?

Thank you

Author

Commented:
What namespace of reference do I need to use...?

Commented:
OK . I will send it once I get back to my computer
Commented:
the namespace is Outlook = Microsoft.Office.Interop.Outlook;
this sample assumes use this statement, also the sample use html template so you can make layout without complicating the code

using Outlook = Microsoft.Office.Interop.Outlook;

     private void SendEmail()
        {
            String htmlTemplate = File.ReadAllText("myTemplate.html");

            try
            {
                Outlook.Application outlook = new Outlook.Application();
                Outlook._MailItem outLookMailMessage = outlook.CreateItem(Outlook.OlItemType.olMailItem) as Outlook._MailItem;
                outLookMailMessage.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
                }
                htmlTemplate = htmlTemplate.Replace("##Text##", tbBody.text); ' replace this magic string in template
                outLookMailMessage.HTMLBody = htmlTemplate;
                outLookMailMessage.Subject = tbSubject.Text;
                outLookMailMessage.Recipients.Add(tbRecipients.Text);
                outLookMailMessage.Display();
                outLookMailMessage = null;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

Open in new window

Author

Commented:
This is one of the problems: I actually found several examples of code which list the namespace you give but II try using it I get a red line under the "Office" part... I am attaching a screen shot. Thanks.outlook.jpg

Author

Commented:
I should add that i am using Visual Studio 2010

Author

Commented:
OK, I now know how to add a reference to the project.... and will be able to try your code. Thank you for your patience FarWest...

Commented:
Nevermind . you are welcome  and sorry for not  mentioning the reference  part
Éric MoreauSenior .Net Consultant
Top Expert 2016
Commented:

Author

Commented:
Thank you both...

I couldn't respond during the weekend because I don't have access to Outlook at home. Now that I am back in the office I was able to test what I got from you and a lot of other code I found online.
Finally I was able to do what I needed with the following piece of code which I would like to share with others who might find a need for it:

 private void CreateMailItem()
        {
            Outlook.Application outlookApp = new Outlook.Application();
            Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
            mailItem.Subject = this.txtSubject.Text;
            mailItem.To = txtAddress.Text;
            mailItem.Body = txtBody.Text;
            mailItem.Importance = Outlook.OlImportance.olImportanceLow;
            mailItem.Display(false);
        }
Éric Moreau: Right now I don't plan to include attachments to my code, but I am sure your article will be of help at some point in the future. Thank you

Adam
Senior .Net Consultant
Top Expert 2016
Commented:
even if you don't have attachments, everything else remains valid!

Author

Commented:
Thank you guys.