How to send emails to. net1.1 and. net2.0

Source: Internet
Author: User
Tags mailmessage smtpclient

When writing a user registration yesterday, you need to write the user to fill in the user's email address, but you have to verify the validity of the email address. The solution to this problem is based on the user's registered email address, if you have an email, send a registration code, and then complete the next registration based on the registration code. I searched for the method on the Internet. Only the. net1.1 method is available. I modified it on this basis. Two versions are provided.

1). net1.1

System. Web. Mail namespace:
The naming control contains the following objects and three attributes:
Objects included:
Mailattachment: Object Class Related to email attachments
Mailmessage: Email Subject
Smtpmail: SMTP protocol used to send emails.
Attribute list:
Mailencoding: email encoding (base64, uuencode)
Mailformat: Mail format (HTML hypertext format, text plain text format)
Mailpriority: Mail priority (high, medium, low)

Construct a mailmessage object:
The mailmessage object is the subject of the mail. Generally, you can build the mailmessage object first, and then set its attributes to build the mail program. The following lists some common attributes:
Attachments: email attachment
BCC: Dark delivery address
Body: Email Subject
Bodyformat: email format (HTML, text)
Cc: CC address
From: sender address
Priority: Mail priority (high, medium, low)
Subject: Email Subject
To: recipient's address
Urlcontentbase: URL encoding method in HTML-format mail
Urlcontentlocation: Priority of mail Information (high, medium, low)

Use smtpmail to send emails
After creating the mailmessage object, you also need to use another object-smtpmail-to send emails. smtpmail has an important method: Send, which has two different usage methods, one of them can only send the entire mailmessage object:
Smtpmail. Send (myemailobject );
You can specify the sender, email address, email subject, and email subject respectively, and then send them out:
Smtpmail. Send (strfrom, strto, strsubject, strbody );

 

Example:

Void Sendmail ()

{

System. Web. Mail. mailmessage myemail = new system. Web. Mail. mailmessage ();

// Set message Parameters
Myemail. From = "hohisoft@163.com ";
Myemail. To = "hohisoft@163.com ";
Myemail. Subject = "Consulting Training ";
Myemail. bodyformat = system. Web. Mail. mailformat. html;
Myemail. Body = "learning. net. When will your practical training course start ?. ";

// Send the message
System. Web. Mail. smtpmail. Send (myemail );

}

 

2). net2.0

System. net. Mail namespace:
The naming control contains the following main objects and attributes:
Objects included:
Mailaddress: the address of the sender or recipient of the email.
Attachment: the attachment of the email.
Mailaddresscollection: stores the email addresses associated with emails.
Mailmessage: an email that can be sent using the smtpclient class
Smtpclient: allows applications to send emails using Simple Mail Transfer Protocol (SMTP.
Attribute list:
Deliverynotificationoptions: Specifies the Email Delivery Notification option.
Mailpriority: Specifies the priority of mailmessage.
Smtpaccess: Specifies the allowed access level for Simple Mail Transfer Protocol (SMTP) servers
Smtpdeliverymethod: Specifies how to send an email
Smtpstatuscode: Specifies the result of sending an email using the smtpclient class

Construct a mailmessage object:
The mailmessage object is the subject of the mail. Generally, you can build the mailmessage object first, and then set its attributes to build the mail program. The following lists some common attributes:
Attachments: email attachment
BCC: Dark delivery address
Body: Email Subject
Cc: CC address
From: sender address
Subject: Email Subject
To: recipient's address

Use smtpclient to send emails
After creating the mailmessage object, you also need to use another object-smtpclient-to send emails. smtpclient has a very important method: Send, which can send the entire mailmessage object:
Smtpclient. Send (mailmessage );

In this example, use the system. net. Mail namespace

/// <Summary>
/// Automatically send the email
/// </Summary>
/// <Param name = "strmail"> email address </param>
/// <Param name = "strregistcode"> Registration Code </param>
/// <Returns> </returns>
Bool Sendmail (string strmail, string strregistcode)
{
String strhost = "mail.cdce.cn"; // address of the stmp Server
String straccount = "jackguo"; // SMTP service account
String strpwd = "111111"; // SMTP service Password

Smtpclient _ smtpclient = new smtpclient ();
_ Smtpclient. deliverymethod = smtpdeliverymethod. Network; // specify the email sending Method
_ Smtpclient. Host = strhost; // specify the SMTP server
_ Smtpclient. Credentials = new system. net. networkcredential (straccount, strpwd); // user name and password

String content = "your verification code is:" + strregistcode + ". Please complete registration immediately and associate the basic information. ";
Content + = "<br> This email is automatically sent. Please do not reply! ";

Mailaddress sender = new mailaddress ("jackguo@cdce.cn", "exam information ");
Mailaddress receriver = new mailaddress (strmail );

Mailmessage mail = new mailmessage (sender, receriver );
Mailaddresscollection tomail = new mailaddresscollection ();

Mail. Subject = "unified information system verification code"; // subject

Mail. Body = content; // content
Mail. bodyencoding = system. Text. encoding. Default; // body Encoding
Mail. isbodyhtml = true;
Mail. Priority = mailpriority. High; // priority

Try
{
_ Smtpclient. Send (Mail );
Return true;
}
Catch (exception E)
{
Return false;
}

}

 

For software development training and project training, please go to huahai training, first-class experience, first-class project training engineers, and the cradle of Software Engineer growth.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.