When it comes to sending mail, first mention SMTP (hehe, the master will skip this section!). )。 The full name of SMTP is "Simple Mail Transfer Protocol", which is simply the message Transfer Protocol. It is a set of specifications for transferring messages from the source address to the destination, which controls how messages are relayed. The SMTP protocol is a TCP/IP protocol cluster that helps each computer find its 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 provided in the System.Web.Mail class library by the namespace (NameSpace)
(1) It has three classes: 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 the Federated data object of 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) The properties of each class.
1. Briefly describe the properties of the SmtpMail: SmtpServer-SMTP address.
2. Main introduction to the properties of the MailMessage object
From--address of the sending email
To--accept the address of the mail
Subject--The title of the message
Priority-The message's precedence (valid value is High,low,normal)
Attachments--Returns a collection that represents the attachment
BCC--Secret delivery address
CC--CC address
Body--Gets or sets the contents of an e-mail message
BodyFormat--Gets or sets the enumeration value of MailFormat that specifies the format of the message body message (HTML format, text format)
Bodyencoding--Specifies the encoding encoding of the message (mainly Base64,uuencode)
A few other unimportant omissions. Make any mention of the difference between BCC and cc: BCC is the person who received the e-mail when you sent the bulk of the email, and they can't see how many people you send and their e-mail address, and CC is the person who received the e-mail when they sent it, and they can see how many people and their e-mail address.
(3) The Send method of the SmtpMail class, which is intended to send a message, has two overloaded methods.
1. Smtpmail.send ("E-mail Address", "Accept mail Address", "title of Message", "Content of mail Message") This method is simple and not suitable for sending messages with attachments.
2. 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 program sent by mail, then how to get SMTP first. There are two ways: the first method calls the current well-known mail service provider's SMTP, such as Sina, Sohu, NetEase's free e-mail mailbox SMTP; The second method is to install an SMTP virtual server, which is mounted together when IIS is installed (the installation process omits:-)).
I. Send mail using SMTP from a well-known mail service provider in ASP.
First you need to go to their mail site to register for a free mailbox, because you want to use the Mail service provider's SMTP, they need to authenticate the identity, so as to avoid generating a lot of junk e-mail. Suppose we registered a free email on Sina's Mail site (mail.sina.com.cn), the username is Mysina, and the password is Chenjie. This account is fictitious, please use your registered user name and password instead. We learned at Sina's mail site that its SMTP address is: smtp.sina.com.cn. We need to[email protected](my email address) to send a message. So the core code for sending messages with ASP. NET (C #) is as follows:
Core code starts
Using System.Web.Mail;
MailMessage objmailmessage;
MailAttachment objmailattachment;
Create an Attachment object
Objmailattachment = new MailAttachment ("D:\\test.txt");//e-mail attachments
Create a mail message
Objmailmessage = new MailMessage ();
Objmailmessage.from = "[email protected]";//source mail address
Objmailmessage.to = "[email protected]";//destination e-mail address, which is sent to me ha
Objmailmessage.subject = "Mail send title: Hello";//headers for sending messages
Objmailmessage.body = "Mail send content: Test if the send is successful!" ";//Send the contents of the message
OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attaching attachments to the mail message object
Then using Sina smtp to send mail, you need to use the Microsoft. NET Framework SDK v1.1 and more than its version
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 there are no above three lines of code, the following error message appears: 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 mail
Smtpmail.send (Objmailmessage);
End of core code
Second, send mail by using SMTP from native SMTP virtual server in ASP.
First, the SMTP configuration.
(1) Right click on "SMTP Virtual Server" Select "Properties", on the "General" tab set "IP address (P)", I set 192.168.1.100.
(2) Select "Access" tab, click "Relay", select "only the following list" (by default is selected), click "Add", in "Single computer" to add 192.168.1.100.
Tip, if not completed (2), there will be a common error message: The server rejected one or more recipient addresses. Server response: 550 5.7.1 Unable to relay for[email protected](Please note: The email address in the error is different) and then start the core code, in fact, and the method (a) almost. The main difference with (a) is: 1. SMTP is different, 2.objmailmessage.from this method can be filled in, but (a) do not fill out the core code that uses ASP. NET (C #) to send mail is as follows:
/Core Code Start
Using System.Web.Mail;
MailMessage objmailmessage;
MailAttachment objmailattachment;
Create an Attachment object
Objmailattachment = new MailAttachment ("D:\\test.txt");//e-mail attachments
Create a mail message
Objmailmessage = new MailMessage ();
Objmailmessage.from = "[email protected]";//source mail address
Objmailmessage.to = "[email protected]";//destination e-mail address, which is sent to me ha
Objmailmessage.subject = "Mail send title: Hello";//headers for sending messages
Objmailmessage.body = "Mail send content: Test if the send is successful!" ";//Send the contents of the message
OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attaching attachments to the mail message object
SMTP address
Smtpmail.smtpserver = "192.168.1.100";
Start sending mail
Smtpmail.send (Objmailmessage);
The above two methods are introduced here. The simplest way to take advantage of this is to add a server button to the page and put the addition of the referenced statement in the button click event. Of course, don't forget to put the quoted statement on top.
The test of method one is completely correct, there is no problem, but method two causes the message to not receive correctly (* * * @sina. com), or received after it is also placed in the Junk e-mail (Sent to[email protected])。
Legacy ASP e-mail code