Today on the Internet to collect some information on the use of. NET to send mail, now take out and share with you! In. NET,. NET has the ability to send mail, which has been encapsulated into the System.Web.Mail namespace of the. NET Framework in VS2003, with more System.Net.Mail namespaces under VS2005.
Today on the Internet to collect some information on the use of. NET to send mail, now take out and share with you! In. NET,. NET has the ability to send mail, which has been encapsulated into the System.Web.Mail namespace of the. NET Framework in VS2003, with more System.Net.Mail namespaces under VS2005.
By using the classes under this namespace, it is easy to build a mail-sending program, all you need to do is to structure the SMTP server in Windows.
System.Web.Mail namespaces:
This named control contains the following objects and three properties:
Included objects:
MailAttachment: Object classes related to mail attachments
MailMessage: Message body
SmtpMail: The SMTP protocol that is responsible for sending mail.
List of properties:
MailEncoding: Encoding of messages (Base64,uuencode)
MailFormat: The format of the message (HTML hypertext format, text plain text format)
Mailpriority: Message priority (High, Medium, low)
To build a MailMessage object:
The MailMessage object is the bearer of the message, usually by building the MailMessage object and then setting its properties to build the mail program, which is listed below for some common properties:
Attachments: Mail Attachment
BCC: Dark Send Address
Body: Mail Subject
BodyFormat: Message Format (html,text)
CC: CC Address
From: Sender's address
Priority: Message priority (high, Medium,low)
Subject: Message subject
To: Recipient address
Urlcontentbase: URL encoding in an HTML-formatted message
Urlcontentlocation: Priority of message information (high, Medium,low)
Send mail using SmtpMail
After building the MailMessage object, you also need to use another object-smtpmail-to send the message, SmtpMail has an important method: Send, the method has two different usages, one of which can only send the entire MailMessage object:
Smtpmail.send (Myemailobject);
Another allows you to specify the sender, the e-mail address, the subject of the message, the subject of the message, and then send it:
Smtpmail.send (Strfrom, Strto, Strsubject, strbody);
Example program:
Take a look at this complete example, in this example, using the System.Web.Mail namespace, we first create a MailMessage object, then set some properties, and then use the SmtpMail object to send it out:
protected void Page_Load (object sender, EventArgs e)
{
CREATE A MAIL Message
System.Web.Mail.MailMessage myemail = new System.Web.Mail.MailMessage ();
SET message PARAMETERS
Myemail.from = "chenjun@webjx.com";
myemail.to = "admin@webjx.com";
Myemail.subject = "Product availability Notice";
Myemail.bodyformat = System.Web.Mail.MailFormat.Html;
Myemail.body = "The sunglasses you expressed interest in are now";
SEND the message
System.Web.Mail.SmtpMail.Send (Myemail);
UPDATE STATUS
Lblmailstatus.text = "Mail successfully sent.";
}
<body>
<asp:label id= "Lblmailstatus" runat= "Server"/>
</body>
System.Net.Mail namespaces:
The following main objects and primary properties are included under this named control:
Included objects:
MailAddress: Represents the address of an e-mail sender or recipient
Attachment: An attachment that represents an e-mail message
Mailaddresscollection: Store e-mail addresses associated with e-mail messages
MailMessage: An e-mail message that can be sent using the SmtpClient class
SmtpClient: Allows applications to send e-mail using Simple Mail Transfer Protocol (SMTP).
List of properties:
Deliverynotificationoptions: Describes delivery notification options for e-mail messages
Mailpriority: Specifies the priority of the MailMessage
Smtpaccess: Specifies the Simple Mail Transfer Protocol (SMTP) server access level that is allowed
Smtpdeliverymethod: Specify how e-mail messages are sent
Smtpstatuscode: Specifies the result of sending an e-mail message using the SmtpClient class
To build a MailMessage object:
The MailMessage object is the bearer of the message, usually by building the MailMessage object and then setting its properties to build the mail program, which is listed below for some common properties:
Attachments: Mail Attachment
BCC: Dark Send Address
Body: Mail Subject
CC: CC Address
From: Sender's address
Subject: Message subject
To: Recipient address
Send mail using SmtpClient
After building the MailMessage object, you also need to use another object-smtpclient-to send the message, SmtpClient has a very important way: send, you can send the entire MailMessage object:
Smtpclient.send (MailMessage);
In this example, using the System.Net.Mail namespace
static string strhost = String.Empty;
static string straccount = String.Empty;
static string strpwd = String.Empty;
static string strfrom = String.Empty;
<summary>
Send mail
</summary>
<param name= "to" > Recipient email Address </param>
<param name= "title" > Message title </param>
<param name= "Content" > Message body Contents </param>
public static bool SendMail (string to, string title, string content)
{
Strhost = "smtp.webjx.com"; STMP server Address
Straccount = "abc@webjx.com"; SMTP Service Account
strpwd = "password"; SMTP Service password
Strfrom = "chenjun@webjx.com"; Mailing address of sending party
SmtpClient _smtpclient = new SmtpClient ();
_smtpclient.deliverymethod = smtpdeliverymethod.network;//Specify how e-mail is sent
_smtpclient.host = Strhost; ;//Specify SMTP server
_smtpclient.credentials = new System.Net.NetworkCredential (Straccount, strpwd);//username and password
MailMessage _mailmessage = new MailMessage ( Strfrom, to);
_mailmessage.subject = title;//Theme
_mailmessage.body = content;//content
_mailmessage.bodyencoding = system.text.encoding.utf8;//Body Encoding
_ mailmessage.isbodyhtml = true;//set to HTML format
_ Mailmessage.priority = mailpriority.high;//Priority
Try
{
_smtpclient.send (_mailmessage);
return true;
}
Catch
{
return false;
}
}