Tuesday, May 13, 2014

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

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
Overview: Aspose.Email for .NET
Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

Tuesday, May 6, 2014

Create & Save an Appointment in Draft Mode inside Java Applications

This technical tip shows how Java developers can create and save an appointment in draft mode inside their own applications using Aspose.Email for Java. It can be useful to create an appointment request in draft mode, so that the basic information is added and then the same draft appointment can be forwarded to other users who might make changes. Aspose.Email for Java provides the flexibility to create and save an appointment in draft mode for later use. In order to save an appointment in draft mode, the Method property of the Appointment class should be set to Publish. The following code sample demonstrates the use of this property as an example.
Create a Draft Appointment Request
[Java]
String sender = "test@gmail.com";
String recipient = "test@email.com";

MailMessage message = new MailMessage(sender, recipient, "", "");
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2012, Calendar.NOVEMBER, 1, 0, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2012, Calendar.DECEMBER, 1);
Date endDate = calendar.getTime();

MailAddressCollection attendees = new MailAddressCollection();
attendees.add(new MailAddress("attendee_address@aspose.com", "Attendee"));
WeeklyRecurrencePattern expected = new WeeklyRecurrencePattern(3);

Appointment app = new Appointment("Appointment Location", "Appointment Summary", "Appointment Description",
        startDate, endDate,
        new MailAddress("organizer_address@aspose.com", "Organizer"), attendees, expected);

//Set the Appointment as Draft
app.setMethod(AppointmentMethodType.Publish);//.Method = AppointmentMethodType.Publish;
message.addAlternateView(app.requestApointment());
MapiMessage msg = MapiMessage.fromMailMessage(message);

// Save the appointment as draft.
msg.save("Draft.msg");

Draft Appointment Creation from Text
[Java]
String ical = "BEGIN:VCALENDAR\r\nMETHOD:PUBLISH\r\nPRODID:-//Aspose Ltd//iCalender Builder (v3.0)//EN\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nATTENDEE;CN=test@gmail.com:mailto:test@gmail.com\r\nDTSTART:20130220T171439\r\nDTEND:20130220T174439\r\nDTSTAMP:20130220T161439Z\r\nEND:VEVENT\r\nEND:VCALENDAR";
String sender = "test@gmail.com";
String recipient = "test@email.com";

MailMessage message = new MailMessage(sender, recipient, "", "");
AlternateView av = AlternateView.createAlternateViewFromString(ical, new ContentType("text/calendar"));
message.getAlternateViews().add(av);
MapiMessage msg = MapiMessage.fromMailMessage(message);
// Save the appointment as draft.
msg.save("DraftAppointment.msg");

Overview: Aspose.Email for Java

Aspose.Email for Java is a Non-Graphical Java component that enables Java applications to read and write MS Outlook MSG files from within a Java application without using MS Outlook. It enables developers to create new MSG file from scratch, update an existing MSG file, read Outlook MSG file & get its properties like subject, body, recipients in to, cc and bcc, adding or removing attachment, sender information & MAPI properties.  Aspose.Email can be used with Web as well as Desktop Application.