Send mail using SMTP

Source: Internet
Author: User
Tags mailmessage

When it comes to email, start by mentioning SMTP. The full name of SMTP is "Simple Mail Transfer Protocol", which is simply a message transfer protocol. It is a set of specifications for transmitting messages from the source address to the destination to control how the message is relayed. The SMTP protocol belongs to the TCP/IP protocol cluster, which helps each computer find the next destination when sending or relaying letters. The SMTP server is the outgoing mail server that follows the SMTP protocol.
A brief introduction to the objects, properties, and methods sent by the message in the namespace (Namespace) System.Web.Mail class Library
1. It has three categories: SmtpMail, MailMessage and MailAttachment. These three objects are applied to the sample program code in this article.
(1). MailMessage: Provides properties and methods to create a mail message object. (Provides properties and methods for constructing an e-mail message.)
(2). Mailattachments: Provides properties and methods to create a message attachment object. (Provides properties and methods for constructing an e-mail attachment.)
(3). SmtpMail: Provides properties and methods to send mail messages by using a federated data object for the message component of Windows CDOSYS. (Provides properties and methods for sending messages using the collaboration Data Objects for Windows (CDOSYS) Messa GE component)
2. Properties of each class.
(1) Briefly introduce the properties of SmtpMail: SmtpServer--SMTP address.
(2) mainly to introduce the properties of the MailMessage object
From--Address of sending mail
To-accept the address of the message
Subject--The title of the message
Priority-The priority of the message (valid values are high, low, Normal)
Attachments--Returns a collection representing the attachment
BCC--Secret address
CC--CC address
Body--Gets or sets the content of an e-mail message
BodyFormat-Gets or sets the enumeration value of the mail format that specifies the format of the message body message (HTML format, text format)
Bodyencoding--Specifies how the message is encoded (mainly Base64,uuencode)
Several other unimportant omissions. Casually mention the difference between the BCC and CC: The secret is that you send bulk mail when the person can not see how many people you send and their e-mail address, CC is a mass mailing when the person receiving the mail can see how many people you sent and their e-mail address.
(3) The Send method of the SmtpMail class, which is designed to send messages with two overloaded methods.
①smtpmail.send ("Address of Mail Sent", "Address to accept Mail", "title of Message", "Content of mail Message") This method is simple enough to send messages with attachments.
②smtpmail.send (MailMessage) This method is complex, flexible, suitable for sending attachments, and can set various property values for MailMessage objects. If we use ASP.net to write a mail-sent program, then how do we get SMTP first? There are two ways: The first method calls SMTP of the currently well-known mail service providers, such as Sina, Sohu, NetEase's free e-mail SMTP; the second method is to install an SMTP virtual server, which is installed together with IIS.

First, use the well-known mail service provider's SMTP to send mail
The first thing you need to do is sign up for a free mailbox on their mail site, because you want to use the Mail service provider's SMTP, they need to authenticate their identity, which can avoid generating a lot of spam. Suppose we registered a free email on Sina's Mail site (mail.sina.com.cn), the username is Mysina, the password is Chenjie, the account is fictitious, please use your registered user name and password instead. We learned from Sina's mail site that its SMTP address is: smtp.sina.com.cn. We need to send a message to scucj@126.com (my email address). The code that uses ASP.net (C #) to send mail is as follows:

Using System.Web.Mail; Private void SendMail () {//Create an Attachment object mailattachment objmailattachment = new MailAttachment ("D://test.txt");//Send mail Attachment// Create mail message MailMessage objmailmessage = new MailMessage (); Objmailmessage. from = "mysina@sina.com";//source mail address objmailmessage. to = "scucj@126.com";//destination mail address objmailmessage. Subject = "Mail send title: Hello";//Send the header of the message objmailmessage. BODY = "Send the contents of the message: test to see if the send success." ";//Send the content of the message objMailMessage.Attachments.Add (objmailattachment);//Attach the attachment to the mail message object//Then use the SMTP of Sina to send mail, you need to use Microsoft The. NET Framework SDK v1.1 and its versions//basic Permissions ObjMailMessage.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/ Smtpauthenticate "," 1 "); User name ObjMailMessage.Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendusername", "Mysina"); Password ObjMailMessage.Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", "Chenjie"); If you do not have the above three lines of code, you receive the following error message: The server rejected one or more recipient addresses. The server response is: 554:client host rejected:access denied//SMTP address smtpmail.smtpserver = "smtp.sina.com.cn"; Start sending mailPieces of Smtpmail.send (objmailmessage); }

Second, use the SMTP virtual server of this computer to send mail
(1) The SMTP configuration should be performed first.

Http://blog.csdn.net/zhangzhaoxin555/archive/2011/06/02/6460566.aspx

(2) The code is as follows:

Using System.Web.Mail; Private void SendMail () {//Create an Attachment object mailattachment objmailattachment = new MailAttachment ("D://test.txt");//Send mail Attachment// Create mail message MailMessage objmailmessage = new MailMessage (); Objmailmessage. from = "mysina@sina.com";//source mail address objmailmessage. to = "scucj@126.com";//destination mail address objmailmessage. Subject = "Mail send title: Hello";//Send the header of the message objmailmessage. BODY = "Send the contents of the message: test to see if the send success." ";//Send the content of the message objMailMessage.Attachments.Add (objmailattachment);//Attach the attachment to the mail message object//SMTP address Smtpmail.smtpserver =" 192.168.1.100 "; Native IP address//start sending mail smtpmail.send (objmailmessage); }

Reprint from: http://www.cnblogs.com/hymxtang/archive/2007/06/27/797247.html

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.