ASP. NET sends email

Source: Internet
Author: User
Tags mailmessage net send smtpclient

1. Additional knowledge
(1) What are POP3 and SMTP servers?
To put it simply, POP3 is used to receive emails, and SMTP is used to send emails.
(1) What does POP3 mean?
POP3 (Post Office Protocol 3) is the 3rd version of the Post Office Protocol. It specifies how a personal computer connects to the mail server on the Internet to send and receive mail. It is the first offline protocol standard for Internet email. POP3 allows you to store emails on a server to a local host (your computer, at the same time, emails are deleted or saved on the mail server according to client operations, while the POP3 server is a mail server that follows the POP3 protocol to receive emails. POP3 is a member of the TCP/IP protocol family, defined by RFC 1939.
(2) What does SMTP mean?
SMTP is short for "Simple Mail Transfer Protocol", that is, Simple Mail transmission Protocol. It is a set of specifications used to transmit mails from the source address to the destination address. It is used to control the transfer mode of mails. The SMTP protocol is a TCP/IP protocol cluster that helps each computer locate the next destination when sending or transferring letters. The SMTP server is the mail sending server that follows the SMTP protocol.
 
2. System. Net. Mail
To send an email using ASP. NET, you must reference the System. Net. Mail namespace. The System. Net. Mail namespace contains classes used to send emails to Simple Mail Transfer Protocol (SMTP) servers for transmission.
(1) There are three main classes in the namespace:
MailMessage: Provides attributes and methods to create a mail message object, that is, the Mail content.
Attachment: Provides attributes and methods to create an email Attachment object, that is, an email Attachment.
SmtpClient: transfers an email to the SMTP host you specified for mail delivery.
(2) MailMessage class:
From: email sending Address
To: email Receiving address
Subject: Mail title
Priority: Mail Priority (High, Low, Normal)
Attachments: a collection of Attachments to email data
Bcc: Bcc address
Cc: Cc address
Body: Body of the email
SubjectEncoding: encoding used for the subject content of the email
IsBodyHtml: whether the email body is in Html Format
For details, refer to MailMessage.

(3) Attachment class:
For details, refer to Attachment.
(4) SmtpClient class:
DeliveryMethod: Specifies how to process emails to be sent.
Host: Name or IP address of the SMTP transaction Host
Credentials: sets Credentials for Sender authentication.
For details, see SmtpClient.
 
3. ASP. NET send mail in two ways
(1) send emails through SMTP of the mail service provider
First, you need to register a free email address for the corresponding service provider. Because you want to use the SMTP address of the mail service provider, they need to verify their identity so as to avoid a large amount of spam.
There are three important information: SMTP server, user name, and password. Several Classes are collected from the Internet. For more information, see. Sample download
 
 
# Region
/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "mailTo"> email address to be sent </param>
/// <Param name = "mailSubject"> Email Subject </param>
/// <Param name = "mailContent"> email content </param>
/// <Returns> return the email sending result </returns>
Public static bool SendEmail (string mailTo, string mailSubject, string mailContent)
{
// Set the sender's mail information, for example, using Netease's smtp
String smtpServer = "smtp.163.com"; // SMTP Server
String mailFrom = "XXX@163.com"; // Login User Name
String userPassword = "XXX"; // logon Password

// Mail service settings
SmtpClient smtpClient = new SmtpClient ();
SmtpClient. DeliveryMethod = SmtpDeliveryMethod. Network; // specify the email sending Method
SmtpClient. Host = smtpServer; // specify the SMTP server
SmtpClient. Credentials = new System. Net. NetworkCredential (mailFrom, userPassword); // user name and password

// Send email settings
MailMessage mailMessage = new MailMessage (mailFrom, mailTo); // sender and recipient
MailMessage. Subject = mailSubject; // Subject
MailMessage. Body = mailContent; // content
MailMessage. BodyEncoding = Encoding. UTF8; // body Encoding
MailMessage. IsBodyHtml = true; // set it to HTML Format
MailMessage. Priority = MailPriority. Low; // Priority

Try
{
SmtpClient. Send (mailMessage); // Send an email
Return true;
}
Catch (SmtpException ex)
{
Return false;
}
}
 
 
(2) Use the SMTP of the local SMTP virtual server to send emails
SMTP configuration is the first simple and practical method.
 
4. References: common email servers
Gmail.com:
POP3 server address: pop.gmail.com
SMTP server address: smtp.gmail.com
Qq.com:
POP3 server address: pop.qq.com
SMTP server address: smtp.qq.com
163. com:
POP3 server address: pop.163.com
SMTP server address: smtp.163.com
Sina.com:

POP3 server address: pop3.sina.com.cn
SMTP server address: smtp.sina.com.cn
Yahoo.com:
POP3 server address: pop.mail.yahoo.com
SMTP server address: smtp.mail.yahoo.com
Sohu.com:
POP3 server address: pop3.sohu.com
SMTP server address: smtp.sohu.com
China.com:
POP3 server address: pop.china.com
SMTP server address: smtp.china.com

21cn.com:
POP3 server address: pop.21cn.com
SMTP server address: smtp.21cn.com sina.com:

 


 
Author: ForEvErNoME

Related Article

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.