Send email with asp.net implementation __.net

Source: Internet
Author: User
Tags mailmessage

When it comes to email, let's start by mentioning SMTP (oh, the master will skip this section bar.) )。 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. (Reference 1)

A brief introduction to the objects, properties, and methods of the mail sent by the namespace (NameSpace) System.Web.Mail class library. (Reference 2)

(1) It has three classes: SmtpMail, MailMessage and MailAttachment. These three objects are applied to the sample program code in this article. (Reference 3)

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 introduce the properties of SmtpMail:
SmtpServer-the address of SMTP.

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--Priority of the message (valid value is 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 MailFormat enumeration value 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.
1. Smtpmail.send ("Mailing Address", "Mail Address", "Message title", "Content of mail Message")

This method is simple and is 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 the MailMessage object.


If we use ASP.net to write a mail-sent program, then how do we get SMTP first? There are two ways of doing this: the first method calls the current well-known mail service provider's SMTP, such as Sina, Sohu, NetEase's free e-mail SMTP; the second method is to install an SMTP virtual server, which is installed together when IIS is installed (the installation process omits:-)).


First, in asp.net 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, and the password is Chenjie. The account is fictitious, please use your registered username 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 core code that uses ASP.net (C #) to send messages is as follows:

Core code Start
Using System.Web.Mail;


MailMessage objmailmessage;
MailAttachment objmailattachment;

Create an Attachment object
Objmailattachment = new MailAttachment ("D://test.txt");//Send message attachment
Create a mail message
Objmailmessage = new MailMessage ();
Objmailmessage.from = "mysina@sina.com";//Source Email address
objmailmessage.to = "scucj@126.com"/destination email address, which is sent to me ha
Objmailmessage.subject = "Mail send title: Hello";//Send the title of the message
Objmailmessage.body = "Send the contents of the message: test to see if the send success." ";/Send the contents of the message
OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attach attachment to mail message object

Then use the Sina SMTP to send mail, you need to use the Microsoft. NET Framework SDK v1.1 and 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 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 mail
Smtpmail.send (Objmailmessage);

Core Code End


Second, in asp.net to use the SMTP virtual server of the native SMTP to send mail

First, the SMTP configuration.

(1) Right-click "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" (the default is selected), click "Add", in "Single computer" add 192.168.1.100.

Tip, if not completed (2), there will be common one of the error prompts: The server rejected one or more recipient addresses. Server response: 5.7.1 Unable to relay for scucj@126.com

(Friendly hint: the email address in the error is different)

Then start the core code, in fact, and method (a) almost. The main difference with (a) is: 1. SMTP is different,
2.objmailmessage.from This method can be easily filled in, but (i) don't just fill out the core code that uses ASP.net (C #) to send messages is as follows:

Core code Start
Using System.Web.Mail;


MailMessage objmailmessage;
MailAttachment objmailattachment;

Create an Attachment object
Objmailattachment = new MailAttachment ("D://test.txt");//Send message attachment
Create a mail message
Objmailmessage = new MailMessage ();
Objmailmessage.from = "mysina@sina.com";//Source Email address
objmailmessage.to = "scucj@126.com"/destination email address, which is sent to me ha
Objmailmessage.subject = "Mail send title: Hello";//Send the title of the message
Objmailmessage.body = "Send the contents of the message: test to see if the send success." ";/Send the contents of the message
OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attach attachment to 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 easiest way to use the above method is to add a server button to the page and put the statement except the reference in the button click event. Of course, don't forget to put the quoted statement on top.

The test for method one is completely correct and there is no problem, but method two causes the message not to be received correctly (* * * @sina. com), or it is sent to the spam message (scucj@126.com).

I would like to discuss with you how to improve method two.

The


Reference 1:http://zhidao.baidu.com/question/1494562.html
Reference 2: Partial reference to http://www.tongyi.net/webdevelop/asp.net/ 1011882.html and ASP. NET Technical Insider "page No. 813. The
references 3:http://msdn.microsoft.com/library/default. Asp?url=/library/en-us/cpref/html/frlrfsystemwebmail. ASP  

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.