DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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

David Zondray user avatar by
David Zondray
·
May. 14, 14 · Code Snippet
Like (0)
Save
Tweet
Share
4.93K Views

Join the DZone community and get the full member experience.

Join For Free

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
//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
VB.NET

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 10 Best Practices for Scaling Your Application: Expert Tips for Optimizing Performance and Ensuring Reliability
  • The Importance of Delegation in Management Teams
  • ChatGPT — The Google Killer? The Job Taker? Or Just a Fancy Chatbot?
  • Real-Time Stream Processing With Hazelcast and StreamNative

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: