DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 93 posts at DZone. View Full User Profile

How to Create Reply, Reply All & Forward Email Messages from MSG Using C# & VB.NET

05.14.2014
| 5865 views |
  • submit to reddit
//The sample code below creates RE (Reply/Reply All) and FW (Forward) messages from a source message.

//[C# Code Sample]

public static IEWSClient GetEWSClient()
{
    const string mailboxUri = "https://exchange.domain.com/ews/Exchange.asmx";
    const string domain = @"";
    const string username = @"username";
    const string password = @"password";
    NetworkCredential credential = new NetworkCredential(username, password, domain);
    IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credential);
    return client;
}
public static void TestMailReFw()
{
    using (IEWSClient client = GetAsposeEWSClient2())
    {
        try
        {

            MailMessage message = new MailMessage(
                "user@domain.com",
                "user@domain.com",
                "TestMailRefw - " + Guid.NewGuid().ToString(),
                "TestMailRefw Implement ability to create RE and FW messages from source MSG file");

            client.Send(message);
            // Wait for a while

            ExchangeMessageInfoCollection messageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri);
            if(messageInfoCol.Count == 1)
                Console.WriteLine("1 message in Inbox");
            else
                Console.WriteLine("Error! No message in Inbox");

            MailMessage message1 = new MailMessage(
                "user@domain.com",
                "user@domain.com",
                "TestMailRefw - " + Guid.NewGuid().ToString(),
                "TestMailRefw Implement ability to create RE and FW messages from source MSG file");

            client.Send(message1);
            // Wait for a while
            messageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri);

            if(messageInfoCol.Count == 2)
                Console.WriteLine("2 messages in Inbox");
            else
                Console.WriteLine("Error! No new message in Inbox");


            MailMessage message2 = new MailMessage(
                "user@domain.com",
                "user@domain.com",
                "TestMailRefw - " + Guid.NewGuid().ToString(),
                "TestMailRefw Implement ability to create RE and FW messages from source MSG file");
            message2.Attachments.Add(Attachment.CreateAttachmentFromString("Test attachment 1", "Attachment Name 1"));
            message2.Attachments.Add(Attachment.CreateAttachmentFromString("Test attachment 2", "Attachment Name 2"));

            client.Reply(message2, messageInfoCol[0]);
            client.ReplyAll(message2, messageInfoCol[0]);
            client.Forward(message2, messageInfoCol[0]);
        }
        catch(Exception ex)
        {
            Console.WriteLine("Error in program");
        }
    }
}

//[VB.NET Code Sample]

Public Shared Function GetEWSClient() As IEWSClient
	Const  mailboxUri As String = "https://exchange.domain.com/ews/Exchange.asmx"
	Const  domain As String = ""
	Const  username As String = "username"
	Const  password As String = "password"
	Dim credential As New NetworkCredential(username, password, domain)
	Dim client As IEWSClient = EWSClient.GetEWSClient(mailboxUri, credential)
	Return client
End Function
Public Shared Sub TestMailReFw()
	Using client As IEWSClient = GetAsposeEWSClient2()
		Try

			Dim message As New MailMessage("user@domain.com", "user@domain.com", "TestMailRefw - " & Guid.NewGuid().ToString(), "TestMailRefw Implement ability to create RE and FW messages from source MSG file")

			client.Send(message)
			' Wait for a while

			Dim messageInfoCol As ExchangeMessageInfoCollection = client.ListMessages(client.MailboxInfo.InboxUri)
			If messageInfoCol.Count = 1 Then
				Console.WriteLine("1 message in Inbox")
			Else
				Console.WriteLine("Error! No message in Inbox")
			End If

			Dim message1 As New MailMessage("user@domain.com", "user@domain.com", "TestMailRefw - " & Guid.NewGuid().ToString(), "TestMailRefw Implement ability to create RE and FW messages from source MSG file")

			client.Send(message1)
			' Wait for a while
			messageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri)

			If messageInfoCol.Count = 2 Then
				Console.WriteLine("2 messages in Inbox")
			Else
				Console.WriteLine("Error! No new message in Inbox")
			End If


			Dim message2 As New MailMessage("user@domain.com", "user@domain.com", "TestMailRefw - " & Guid.NewGuid().ToString(), "TestMailRefw Implement ability to create RE and FW messages from source MSG file")
			message2.Attachments.Add(Attachment.CreateAttachmentFromString("Test attachment 1", "Attachment Name 1"))
			message2.Attachments.Add(Attachment.CreateAttachmentFromString("Test attachment 2", "Attachment Name 2"))

			client.Reply(message2, messageInfoCol(0))
			client.ReplyAll(message2, messageInfoCol(0))
			client.Forward(message2, messageInfoCol(0))
		Catch ex As Exception
			Console.WriteLine("Error in program")
		End Try
	End Using
End Sub

This technical tip explains how .NET developers can create RE (Reply/Reply All) and FW (Forward) email messages from a source message inside their .NET applications using Aspose.Email for .NET. IEWSClient lets developers create RE (Reply/Reply All) and FW (Forward) messages from a source message. The source message is identified by selecting a particular ExchangeMessageInfo from ExchangeMessageInfoCollection obtained by IEWSClient.ListMessages(). The other argument is the actual MailMessage to be sent as RE or FW message. In the following example, a sample account is used to send a message and then the Reply and Forward message features are demonstrated against that sample message.

To perform this task:

  • Initialize the IEWSClient object by providing valid credentials.
  • Send a few sample messages.
  • Call the IEWSClient.Reply(), IEWSClient.ReplyAll() and IEWSClient.Forward() functions to send messages