This technical tip explain shows how to read outlook email template File (.OFT). Outlook templates are very useful when you want to send a similar email message again and again. Instead of preparing the message from scratch each time, first prepare the message in Outlook and save it as an Outlook template (OFT). After that, whenever you need to send the message, you can create it from the template, saving time writing the same text in the body or the subject line, setting formatting and so on.



Aspose.Emailís MailMessage class can be used to load and read an Outlook template (OFT) file. Once the Outlook template is loaded in an instance of the MailMessage class, you can update the sender, recipient, body, subject and other properties. After updating the properties:


- Send the email using the SmtpClient class or
- Save the message as MSG and do further updates/validation using Microsoft Outlook.



Sample Code for loading outlook email template (.OFT) File, updating the message and saving it in MSG format


[C#]

Code:
// Load the Outlook template (OFT) file in MailMessage's instance
MailMessage message = MailMessage.Load("invitation to meeting.oft", MessageFormat.Msg);

// Set the sender and recipients information
stringsenderDisplayName = "John";
stringsenderEmailAddress = "john@abc.com";
stringrecipientDisplayName = "William";
stringrecipientEmailAddress = "william@xzy.com";

message.Sender = new MailAddress(senderEmailAddress, senderDisplayName);
message.To.Add(new MailAddress(recipientEmailAddress, recipientDisplayName));
message.HtmlBody = message.HtmlBody.Replace("DisplayName", "<b>" + recipientDisplayName + "</b>");

// Set the name, location and time in email body
stringmeetingLocation = "<u>" + "Hall 1, Convention Center, New York, USA" + "</u>";
stringmeetingTime = "<u>" + "Monday, June 28, 2010" + "</u>";
message.HtmlBody = message.HtmlBody.Replace("MeetingPlace", meetingLocation);
message.HtmlBody = message.HtmlBody.Replace("MeetingTime", meetingTime);

// Send the email or save as MSG and open in Outlook for further editing
SmtpClient client = new SmtpClient("host", 25, "username", "password");
client.Send(message);

// Save the message in MSG format and open in Office Outlook
MapiMessagemsg = MapiMessage.FromMailMessage(message);
msg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
msg.Save("Invitation.msg");
Process.Start("Invitation.msg");


[VB.NET]


Code:
' Load the Outlook template (OFT) file in MailMessage's instance
Dim message As MailMessage = MailMessage.Load("invitation to meeting.oft", MessageFormat.Msg)

' Set the sender and recipients information
Dim senderDisplayName As String = "John"
Dim senderEmailAddress As String = "john@abc.com"
Dim recipientDisplayName As String = "William"
Dim recipientEmailAddress As String = "william@xzy.com"

message.Sender = New MailAddress(senderEmailAddress, senderDisplayName)
message.To.Add(New MailAddress(recipientEmailAddress, recipientDisplayName))
message.HtmlBody = message.HtmlBody.Replace("DisplayName", "<b>" &recipientDisplayName& "</b>")

' Set the name, location and time in email body
Dim meetingLocation As String = "<u>" & "Hall 1, Convention Center, New York, USA" & "</u>"
Dim meetingTime As String = "<u>" & "Monday, June 28, 2010" & "</u>"
message.HtmlBody = message.HtmlBody.Replace("MeetingPlace", meetingLocation)
message.HtmlBody = message.HtmlBody.Replace("MeetingTime", meetingTime)

' Send the email or save as MSG and open in Outlook for further editing
Dim client As SmtpClient = New SmtpClient("host", 25, "username", "password")
client.Send(message)

' Save the message in MSG format and open in Office Outlook
Dim msg As MapiMessage = MapiMessage.FromMailMessage(message)
msg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT)
msg.Save("Invitation.msg")
Process.Start("Invitation.msg");