C# .NET - How to create mini outlook template - Asked By saranya on 13-May-16 07:25 AM

Dear friend,

I want to create mini outlook template. I am using outlook 2013 but for my personal assistance i want to create like outlook template and i have to use some logics in send button click but for first step i want below things:

From
To
BCC
Subject

Body

send (button)


In body i need adding background,colors, text font,alignment,attachments.

Please provide me the .net piece  of code to implement these things. Thanks in advance
Ravinder Jamgotre replied to saranya on 13-May-16 09:10 AM
his Visual Basic for Applications (VBA) example uses CreateItemFromTemplate to create a new item from an Outlook template and then displays it. The CreateTemplate macro shows you how to create the template that is used in the first example. To avoid errors, replace 'Dan Wilson' with a valid name in your address book.

Sub CreateFromTemplate() 
 Dim MyItem As Outlook.MailItem 
 Set MyItem = Application.CreateItemFromTemplate("C:\statusrep.oft") 
 MyItem.Display 
End Sub 
Sub CreateTemplate() 
 Dim MyItem As Outlook.MailItem 
 Set MyItem = Application.CreateItem(olMailItem) 
 MyItem.Subject = "Status Report" 
 MyItem.To = "Dan Wilson" 
 MyItem.Display 
 MyItem.SaveAs "C:\statusrep.oft", OlSaveAsType.olTemplate 
End Sub
saranya replied to Ravinder Jamgotre on 17-May-16 07:34 AM
Hi friend,

Thanks for your reply. I want code in .cs (windows application). could you please provide me in .cs file

Ravinder Jamgotre replied to saranya on 17-May-16 07:46 AM

MailItem.Body is a string data type. If you want to append text to the Body, you should use += instead of just =. Since you are using an HTML-formatted email (BodyFormat = olFormatHTML), you should be working with MailItem.HTMLBody which is an HTML-syntax string.

See MSDN section 17.3.1 for reference on how to add text to the Body property. Section 17.3.2 shows how to add text to HTMLBody property.

Text Email Example (BodyFormat = olFormatPlain)
mail.Body += "<p>TEST</p>";

HTML Email Example (BodyFormat = olFormatHTML)
mail.HTMLBody = mail.HTMLBody.Replace("</body>", "<p>TEST</p></body>");
 
Ravinder Jamgotre replied to saranya on 17-May-16 07:51 AM
To create an Outlook 2013 email from C#, simply add the Microsoft Outlook 15.0 Object Library to your solution, add the appropriate using directives, create a new Application object, and MailItem object, and flesh out your email. When ready, simply call MailItem.Display(false) to show the email to the user.
Ravinder Jamgotre replied to saranya on 17-May-16 07:53 AM

Step 1 – Prerequisites and Assembly References

Before doing anything, it is important to note that you must have Microsoft Office 2013 installed for this to work. Seems obvious, but, its still worth mentioning.

You also need two references;

Microsoft.Office.Core

Microsoft.Office.Interop.Office

The quickest way to add these references to your project is to right click on the References folder in your project, and click Add Reference. The Reference Manager dialog window will appear as shown below;


  1. Click the COM tab
  2. Type Outlook into the search box
  3. Tick Microsoft Outlook 15.0 Object Library
  4. Click OK

You should now see that the appropriate references have been added to your project;


Step 2 – Using Directives and Initialization

Next, add the appropriate using directives to your code file.

using Microsoft.Office.Interop.Outlook;

using OutlookApp = Microsoft.Office.Interop.Outlook.Application;

The second directive is a recommendation to avoid ambiguity with other classes with the name Application.

In the constructor of your application (or wherever you want this code to go), create an instance of the Outlook Application and create a new MailItem object, as shown;

OutlookApp outlookApp = new OutlookApp();

MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);

Step 3 – Format and display the email to the user

Finally you can begin to flesh out your email.

mailItem.Subject = "This is the subject";

mailItem.HTMLBody = "<html><body>This is the <strong>funky</strong> message body</body></html>";

//Set a high priority to the message

mailItem.Importance = OlImportance.olImportanceHigh;

And to display the email, simply call the Display method;

mailItem.Display(false);

There are literally dozens of things you can do to an Outlook Email, including adding attachments, business cards, images, recipient, CC/BCC fields.

saranya replied to Ravinder Jamgotre on 18-May-16 06:52 AM
Hi,

Thanks a lot. Really very helpful to me
Ravinder Jamgotre replied to saranya on 18-May-16 08:07 AM
Your welcome, anything else. just post