SmtpMail and Mail Message : Send Mails in .NET


Tools Used            Visual C# .NET
Namespace           System.Web.Util
Assembly              System.Web.dll

The SmtpMail class can be used to send from your C# application. Mail is by default queued on the system, ensuring that the calling program does not block network traffic. The SmtpMail class is defined in the namespace System.Web.Util. You need to call

using System.Web.Util

before you use SmtpMail. This class has only one member function Send. Send sends a mail message. The Send method is overloaded. Either a MailMessage class or four arguments can be passed to the Send message. You can call the Send method in two manners:

SmtpMail.Send(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text);

Or, if you don't want to call System.Web.Util.

System.Web.Util.Smptmail.Send(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text);

You can call the Send method in two ways.

1. By passing MailMessage as a parameter

public
static void Send(MailMessage);

Here MailMessage is a class.

            MailMessage mailMsg = new MailMessage();
            mailMsg .From = "
[email protected]";
            mailMsg .To = "
[email protected]";
            mailMsg .Cc = "
[email protected]"";
            mailMsg .Bcc = "
[email protected]";
            mailMsg .Subject = "SubjectOfTheMailString";
            mailMsg .Body = "BodyOfTheMailString";
            SmtpMail.Send(mailMsg );

2. Direct method.

public
static void Send(string from , string to, string subject, string messageText);

Parameters Description
from Email address of sender
to Email address of recipient
subject Email subject line
messageText  Body of Email message

Example:

SmtpMail.Send("[email protected]", "[email protected]", "Subject", "Message body");

The MailMessage Class

The MailMessage class can be used to send mails with cc, bcc or attachments. The MailMessage constructor initializes a new instance of the MailMessage class. It doesn't take any parameters.

MailMessage mail = new MailMessage();

Members  Description
Bcc Specifies a semicolon-separated list of email addresses that will receive a private copy of the email message.
Body Indicates the body content of the email message.
BodyEncoding Indicates the encoding type of the email message.
BodyFormat Indicates the type of the email message body (text or html).
Cc Specifies a semicolon-separated list of email addresses that will receive a copy of the email message.
From Indicates the email address of sender.
Priority Indicates the priority of the email message.
Subject Indicates the subject line of the email.
To Indicates the email address of the recipient.
UrlContentBase Indicates the base of all relative URLs used within an HTML-encoded message.
Attachments List of MailAttachments to be transmitted with the message. This property is read-only.
Headers Indicates the dictionary of custom headers to be transmitted with the message. This property is read-only.

Each member of this class are pretty easy to understand. Say you want to send an attachment with the mail. You use Attachment member to attach the mail. Say I want to attach mcb.jpg with the mail. 

mail.Attachments.Add(new MailAttachment(@"C:\mcb.jpg"));

Each member of this class are pretty easy to understand. Say you want to send an attachment with the mail. You use Attachment member to attach the mail.

Other MailMessage members have been used in the above example.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.