Sending and Forwarding Messages

The SmtpClient class allows applications to send email using the Simple Mail Transfer Protocol (SMTP).

To send the email message and block while waiting for the email to be transmitted to the SMTP server, use one of the synchronous Send methods. To allow your program’s main thread to continue executing while the email is transmitted, use the BeginSend method.

Sending Emails Synchronously

An email message can be sent synchronously using the SmtpClient class Send method. It sends the specified email message through an SMTP server for delivery. The message sender, recipients, subject, and message body are specified using String objects. To send an email message synchronously, follow the steps given below:

  1. Create an instance of MailMessage class and set its properties.
  2. Create an instance of SmtpClient class and specify the Host, port, username & Password.
  3. Send the Message using the SmtpClient class Send method and pass the MailMessage instance.

The following code snippet shows you how to send emails synchronously.

Sending Emails Asynchronously

Sometimes, you may want to send mail asynchronously. For example, if you are sending a lot of mail through your application, the synchronous approach might not work. In such a scenario, you can use BeginSend. The BeginSend method of the SmtpClient class sends an email message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes. To send an email message asynchronously, follow these steps:

  1. Create an instance of MailMessage class and use its different properties.
  2. Create an instance of SmtpClient class and specify the host, port, username, and password.
  3. Create a user-defined instance that will be passed to the method and invoked when the asynchronous operation completes.
  4. Send the message using BeginSend method of SmtpClient class and pass the MailMessage instance and user-defined instance in it along with a callback function to be called when the operation is completed.

To receive a notification when the email has been sent or the operation has been canceled, the callback function passed to the BeginSend method is called. After calling the SmtpClient class BeginSend method it is not necessary to wait for an email message to be sent completely. We can call another method BeginSend at the same time. When an email has been sent using the BeginSend method, the code snippet prints a message (“Message Sent”). The following code snippet shows you how to send emails asynchronously.

Sending Stored Messages from Disc

EML files, (Outlook Express Electronic Mail files) contains an email’s header, message body, and any attachments. Aspose.Email lets developers work with EML files in different ways. This article shows how to load EML files from disk and send them as emails with SMTP. You can load .eml files from disk or stream into the MailMessage class and send the email message using the SmtpClient class. The MailMessage class is the main class for creating new email messages, loading email message files from disk or stream and saving the messages. The following code snippet shows how to sending stored messages from the disc.

Sending Plain Text Email

The programming samples below show how to send a plain text email message. The Body property, a property of the MailMessage class, is used to specify the plain text content of the message body. To send a plain text email message, follow these steps:

  • Create an instance of the MailMessage class.
  • Specify the sender and receiver email addresses in the MailMessage instance.
  • Specify the Body content, used for the plain text message.
  • Create an instance of the SmtpClient class and send the email.

The following code snippet shows you how to send a plain text email.

Sending Email with HTML body

The programming samples below show how you can send a simple HTML email message. The HtmlBody, a property of the MailMessage class, is used to specify the HTML content of the message body. To send a simple HTML email, follow these steps:

  • Create an instance of the MailMessage class.
  • Specify sender and receiver email address in the MailMessage instance.
  • Specify the HtmlBody content.
  • Create an instance of the SmtpClient class and send the email using the Send method.

For the purposes of this article, the HTML content of the email is rudimentary:This is the HTML bodyMost HTML emails will be more complex. The following code snippet shows you how to send an email with HTML body.

Sending Email with Alternate Message Text

The programming samples below show how to send a simple HTML email message with alternative content. Use the AlternateView class to specify copies of an email message in different formats. For example, if you send a message in HTML, you might also want to provide a plain text version for recipients who use email readers that cannot display HTML content. Or, if you are sending a newsletter, you might want to provide a plain text copy of the text for those recipients who have chosen to receive a plain text version. To send an email with alternate text, follow these steps:

  1. Create an instance of the MailMessage class.
  2. Specify sender and receiver email addresses in the MailMessage instance.
  3. Create an instance of the AlternateView class.

This creates an alternate view to an email message using the content specified in the string.

  1. Add the instance of the AlternateView class to the MailMessage object.
  2. Create an instance of the SmtpClient class and send the email using the Send method.

The following code snippet shows you how to send an email with alternate text.

Sending Bulk Emails

Sending emails in bulk means sending a batch of emails in one message. We can send a batch of email using the of SmtpClient class Send method overload that accepts a MailMessageCollection:

  1. Create an instance of SmtpClient class.
  2. Specify the SmtpClient class properties.
  3. Create an instance of the MailMessage class.
  4. Specify sender, receiver, mail subject and message in the instance of the MailMessage class.
  5. Repeat the above two steps again, if you want to send email to a different person.
  6. Create an instance of MailMessageCollection class.
  7. Add an instance of MailMessage class in the object of the MailMessageCollection class.
  8. Now send your email using the SmtpClient class Send method by passing the instance of MailMessageCollection class in it.

The following code snippet shows you how to send bulk emails.

Sending Emails with MultiConnection

SmtpClient provides a UseMultiConnection property which can be used to create multiple connections for heavy operations. You may also set the number of connections to be used during multiconnection mode by using SmtpClient.ConnectionsQuantity. The following code snippet demonstrates the use of the multiconnection mode for sending multiple messages.

Sending Message as TNEF

TNEF emails have special formatting which may be lost if sent using the standard API. Aspose.Email provides the capability to send emails as TNEF, thus preserving the format. The SmtpClient class UseTnef property can be set to send the email as TNEF. The following code snippet shows you how to send a message as TNEF.

Sending Meeting Requests

Microsoft Outlook offers calendar functions as well as email management. When a user opens an email with an invitation to an event, Outlook prompts them to accept or reject the invitation. Aspose.Email lets developers add calendar functions to your emails.

Sending Requests via Email

To send meeting requests via email, follow these steps:

iCalendar meeting request sent by email
todo:image_alt_text
The following code snippet shows you how to send requests via Email.

iCalendar supports for IBM Lotus Notes

Aspose.Email calendar feature is based on the iCalendar standard, a standard for calendar data exchange (RFC 2445 or RFC2445 Syntax Reference). Therefore, it supports not only Microsoft Outlook but also IBM Lotus Notes. To send a meeting request in Lotus Notes, follow the same steps as mentioned above.

Forward an Email using SMTP Client

Forwarding Email with SMTP client

Forwarding an email is common practice in daily life digital communication. An email received can be forwarded to specific recipients without sharing with the original senders. Aspose.Email API’s SmtpClient provides the capability to forward an email to specific recipients. Its Forward method can be used to forward a received or saved email to desired recipients as shown in this article. The following code snippet shows you how to Forward an Email using SMTP Client.

Forwarding Email without using MailMessage

The API also supports forwarding EML messages without first loading into MailMessage. This is useful in cases where there are limited resources in terms of system memory.

Performing Mail Merge

Mail merges help you create and send a batch of similar email messages. The core of the emails are the same, but the content can be personalized. Typically, a recipient’s contact details (first name, second name, company and so on) are used to personalize the email.

Illustration of how a mail merge works:
todo:image_alt_text
Aspose.Email lets developers set up mail merges that include data from a variety of data sources.

To perform a mail merge with Aspose.Email, take the following steps:

  1. Create a function with the name signature
  2. Create an instance of the MailMessage class.
  3. Specify the sender, receiver, subject, and body.
  4. Create a signature for the end of the email.
  5. Create an instance of the TemplateEngine class and pass it the MailMessage instance.
  6. Take signature in the TemplateEngine instance.
  7. Create an instance of the DataTable class.
  8. Add the columns Receipt, FirstName and LastName as data sources in the DataTable class.
  9. Create an instance of the DataRow class.
  10. Specify the receipt address, first and last names in the DataRow object.
  11. Create an instance of the MailMessageCollection class
  12. Specify the TemplateEngine  and DataTable instances in the MailMessageCollection instance.
  13. Create an instance of the SmtpClient class and specify the server, port, username, and password.
  14. Send emails using the SmtpClient class Send method.

In the sample below, #FirstName# indicates a column’s DataTable, whose value is set by the user. The following code snippet shows you how to perform Mail Merge.

Performing Row-Wise Mail Merge

User can merge individual data row as well to get a complete and prepared MailMessage object. The TemplateEngine.Merge method can be used to perform a row-wise mail merge.