How to Create a Mail Message Object in C# and vb.net

The Mail Message object has four different constructors that allow you to create a blank Mail Message subject, and message body. If you are creating a simple message with a single recipient, you can do the bulk of the work in the ; specify both the sender and recipient; or specify the sender, recipient , Mail Message constructor:

' VB
Dim m As MailMessage = New MailMessage _
("info@ehsanenaloo.com", _
"ehsan@ehsanenaloo.com", _
"Quarterly data report.", _
"See the attached spreadsheet.")
// C#
MailMessage m = new MailMessage
("info@ehsanenaloo.com",
"ehsan@ehsanenaloo.com",
"Quarterly data report.",
"See the attached spreadsheet.");

You can specify the sender and the recipient as either a string or MailAddress object. The MailAddress object allows you to specify an e-mail address, a display name, and an encoding type, as the following code sample demonstrates:

' VB
Dim m As MailMessage = New MailMessage _
(New MailAddress("info@ehsanenaloo.com", "information "), _
New MailAddress("ehsan@ehsanenaloo.com", "ehsan enaloo"))
// C#
MailMessage m = new MailMessage
(new MailAddress("info@ehsanenaloo.com", "information "),
new MailAddress("ehsan@ehsanenaloo.com", "ehsan enaloo"));

NOTE Encoding types
Specifying the encoding type for e-mail addresses is rarely necessary.

If you need to specify multiple recipients, use the blank Then add MailMessage constructor.MailAddress objects to the MailMessage.To property (which is of the MailAddressCollection type), and specify MailMessage.From, MailMessage.Subject, and MailMessage.Body:

' VB
Dim m As MailMessage = New MailMessage()
m.From = New MailAddress("info@ehsanenaloo.com", "information ")
m.To.Add(New MailAddress("james@ehsanenaloo.com", "name 1"))
m.To.Add(New MailAddress("ehsan@ehsanenaloo.com", "name 2"))
m.To.Add(New MailAddress("burke@ehsanenaloo.com", "name 3"))
m.Subject = "Quarterly data report."
m.Body = "See the attached spreadsheet."
// C#
MailMessage m = new MailMessage();
m.From = new MailAddress("info@ehsanenaloo.com", "information ");
m.To.Add(new MailAddress("james@ehsanenaloo.com", "name 1"));
m.To.Add(new MailAddress("ehsan@ehsanenaloo.com", "name 2"));
m.To.Add(new MailAddress("burke@ehsanenaloo.com", "name 3"));
m.Subject = "Quarterly data report.";
m.Body = "See the attached spreadsheet.";

Additionally, you can add recipients to the MailMessage.Cc and MailMessage.Bcc properties in exactly the same way as you would add recipients to MailMessage.From. Recipients specified with MailMessage.Cc will receive the message, and their names will show up on the CC line of the e-mail, which is isible to all recipients. Recipients specified with MailMessage.Bcc will receive the message, but their names will not be visible to other recipients. BCC stands for “blind carbon copy,” a term that originated when people made duplicates of typed paper memos using carbon paper.

NOTE The risk of BCC
Instead of using BCC, you should send a separate copy of your message to each recipient that you want to receive a blind copy. The problem with BCC is that spam filters frequently block messages that do not have the recipient’s e-mail address in the From header. Therefore, if you use BCC, the message will very likely be filtered.

MailMessage has the following less frequently used properties:

DeliveryNotificationOptions
Instructs the SMTP server to send a message to the address specified in MailMessage.From if a message is delayed, fails, or is successfully delivered or relayed to another server. The enumeration is of type Delivery – NotificationOptions, and the values are OnSuccess, OnFailure, Delay, None, nd Never.

ReplyTo
The e-mail address that replies will be sent to. Because the .NET Framework does not act as an e-mail client, and therefore your application will not typically be receiving e-mail, in most cases you should simply set MailMessage . From to the address that should receive replies instead of sing ReplyTo.
Priority
The priority of the message. This does not in any way affect how the .NET Framework or the mail server handles the message. However, the priority might be visible in the recipient’s e-mail client. The priority is also useful for filtering automatically generated e-mail based on the priority of the event that initiated the e-mail. This enumeration is of type MailPriority and can have values of Normal, High, and Low.

Leave a Reply

Your email address will not be published. Required fields are marked *