System.Web.mail sending a message----a virtual sender

Source: Internet
Author: User
Tags mailmessage

Reprint someone else's use SMTP to send mail

When it comes to sending mail, first mention SMTP. 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. Properties for each class.
(1) First, briefly introduce the properties of SmtpMail: SmtpServer-SMTP address.
(2) mainly to introduce 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 order of precedence of the message (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 for mail format, which specifies the format of message body messages (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.
①smtpmail.send ("Email address", "Accept mail Address", "title of Message", "Content of mail Message") This method is simple and not suitable for sending 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 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 installed together with IIS.

I. Using SMTP from a well-known mail service provider to send mail
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, the password is Chenjie, the 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 send an email to [email protected](my email address). Then the code that uses ASP. NET (C #) to send the message is as follows:

[C-sharp]View Plaincopy
  1. Using System.Web.Mail;
  2. Private void SendMail ()
  3. {
  4. Create an Attachment object
  5. MailAttachment objmailattachment = new MailAttachment ("D://test.txt");//e-mail attachments
  6. Create a mail message
  7. MailMessage objmailmessage = new MailMessage ();
  8. Objmailmessage. from = "[email protected]";//Source Email address
  9. Objmailmessage. to = "[email protected]";//Destination Email address
  10. Objmailmessage. Subject = "Mail send title: Hello";//headers for sending messages
  11. Objmailmessage. Body = "Mail send content: Test if the send is successful!" ";//Send the contents of the message
  12. OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attaching attachments to the mail message object
  13. Then using Sina smtp to send mail, you need to use the Microsoft. NET Framework SDK v1.1 and more than its version
  14. Basic permissions
  15. OBJMAILMESSAGE.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
  16. User name
  17. OBJMAILMESSAGE.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendusername", "Mysina");
  18. Password
  19. OBJMAILMESSAGE.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", "Chenjie");
  20. If you do not have the 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
  21. SMTP address
  22. Smtpmail.smtpserver = "smtp.sina.com.cn";
  23. Start sending mail
  24. Smtpmail.send (Objmailmessage);
  25. }

Second, use SMTP of native SMTP virtual server to send mail--the virtual sender sends the message.
(1) Configure the SMTP configuration first.

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

(2) The code is as follows:

[C-sharp]View Plaincopy
  1. Using System.Web.Mail;
  2. Private void SendMail ()
  3. {
  4. Create an Attachment object
  5. MailAttachment objmailattachment = new MailAttachment ("D://test.txt");//e-mail attachments
  6. Create a mail message
  7. MailMessage objmailmessage = new MailMessage ();
  8. Objmailmessage. from = "[email protected]";//Source Email address
  9. Objmailmessage. to = "[email protected]";//Destination Email address
  10. Objmailmessage. Subject = "Mail send title: Hello";//headers for sending messages
  11. Objmailmessage. Body = "Mail send content: Test if the send is successful!" ";//Send the contents of the message
  12. OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attaching attachments to the mail message object
  13. SMTP address
  14. Smtpmail.smtpserver = "192.168.1.100"; Native IP Address
  15. Start sending mail
  16. Smtpmail.send (Objmailmessage);
  17. }

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

System.Web.mail sending a message----a virtual sender

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.