ASP. netf send mail

Source: Internet
Author: User

 

When it comes to sending emails, I 'd like to mention SMTP first (Oh, you can skip this section !). 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.
Next, let's briefly introduce the objects, attributes, and methods for sending emails in the namespace system. Web. Mail class library.
(1) It has three classes: smtpmail, mailmessage, and mailattachment. These three objects are applied in the sample code in this article!
1. mailmessage: Provides attributes and methods to create an email message object. (Provides properties and methods for constructing an e-mail message .)
2. mailattachments-Provides attributes and methods to create an email attachment object. (Provides properties and methods for constructing an E-Mail Attachment .)

3. smtpmail-Provides attributes and methods for sending mail messages by using the combined data object of the message component of Windows 2000 cdosys ). (Provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (cdosys) Message component)

(2) attributes of each class.
1. Briefly introduce the attributes of smtpmail: smtpserver -- SMTP address.
2. Describes attributes of the mailmessage object.
From -- email sending Address
To -- Address of the email recipient
Subject -- mail title
Priority -- mail priority (valid values: High, low, normal)
Attachments -- returns a set that represents an attachment.
BCC-BCC address
CC-CC address
Body -- get or set the content of the email message
Bodyformat -- gets or sets the enumerated value of mailformat. This value specifies the message body email format (HTML format and text format)
Bodyencoding -- specify the message encoding method (mainly including base64 and uencode)
The other few are not important. The difference between BCC and CC is that BCC means that the recipient who receives emails in your group cannot see the number of people you sent and their email addresses, CC refers to the people who receive emails when sending emails to the group. You can view the number of people you sent and their email addresses.

(3) The send method of the smtpmail class, which is used to send emails. There are two overload methods.
1. smtpmail. send ("email sending Address", "email Receiving address", "email title", and "email message content") is very simple, it is not suitable for sending emails with attachments.
2. smtpmail. Send (mailmessage) This method is complex and flexible. It is suitable for sending attachments, and various attribute values of mailmessage objects can be set. If we use ASP. NET to write a mail sending program, how should we first obtain SMTP. There are two methods: the first method is to call the SMTP of well-known mail service providers, such as Sina, Sohu, and Netease free email; the second method is to install an SMTP virtual server, which is installed together When IIS is installed (the installation process is omitted :-)).

1. Use SMTP of well-known mail service providers to send emails in ASP. NET
First, you need to register a free email address on their mail site. Because you want to use SMTP from the mail service provider, they need to verify their identity, so as to avoid a large amount of spam. Suppose we have registered a free email on the Sina mail site (mail.sina.com.cn). The user name is mysina and the password is chenjie. this account is fictitious. 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 mail to scucj@126.com (my mailbox address.
The core code for sending emails using ASP. NET (C #) is as follows:
// Start with the core code
Using system. Web. mail;
Mailmessage objmailmessage;
Mailattachment objmailattachment;
// Create an attachment object
Objmailattachment = new mailattachment ("D: \ test.txt"); // attachments for sending emails
// Create an email message
Objmailmessage = new mailmessage ();
Objmailmessage. From = "mysina@sina.com"; // source email address

Objmailmessage. To = "scucj@126.com"; // destination email address, that is, send it to me

Objmailmessage. Subject = "mail title: Hello"; // The Mail title
Objmailmessage. Body = "mail sender content: test whether the email is successfully sent! "; // Content of the email
Objmailmessage. attachments. Add (objmailattachment); // attaches the attachment to the mail message object.
// Use Sina's SMTP to send emails. You need to use Microsoft. NET Framework SDK V1.1 and later 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 the preceding three lines of code are not available, the following error occurs: the server rejects one or more recipient addresses. Server Response: 554: client host rejected: Access Denied

// SMTP address
Smtpmail. smtpserver = "smtp.sina.com.cn ";
// Start sending emails
Smtpmail. Send (objmailmessage );
// The core code ends.
2. Use SMTP of the local SMTP virtual server in ASP. NET to send emails
First, let's talk about SMTP configuration.
(1) Right-click "SMTP virtual server" and choose "properties". On the "General" tab, set "IP address (p)". What I set is 192.168.1.100.
(2) Select the "access" tab, click "relay", select "only the following list" (selected by default), and click "add ", add 192.168.1.100 to "single computer.
Tip: if it is not completed (2), a common error message is displayed: the server rejects one or more recipient addresses. Server Response: 550 5.7.1 unable to relay
Scucj@126.com (friendly tip: the mail address in the error is different) and then start the core code, in fact, and the method (a) is similar. The main difference with (1) is: 1. SMTP differences, 2. objmailmessage. in the from statement, you can enter the method as needed, but in (1) do not enter the method using ASP. net (C #) Core code for sending emails is as follows:

/Start with the core code
Using system. Web. mail;
Mailmessage objmailmessage;
Mailattachment objmailattachment;
// Create an attachment object
Objmailattachment = new mailattachment ("D: \ test.txt"); // attachments for sending emails
// Create an email message
Objmailmessage = new mailmessage ();
Objmailmessage. From = "mysina@sina.com"; // source email address

Objmailmessage. To = "scucj@126.com"; // destination email address, that is, send it to me

Objmailmessage. Subject = "mail title: Hello"; // The Mail title
Objmailmessage. Body = "mail sender content: test whether the email is successfully sent! "; // Content of the email
Objmailmessage. attachments. Add (objmailattachment); // attaches the attachment to the mail message object.
// SMTP address
Smtpmail. smtpserver = "192.168.1.100 ";
// Start sending emails
Smtpmail. Send (objmailmessage );
The above two methods are introduced here. The simplest way to use the preceding method is to add a server button on the page and put the referenced statement in addition to the button and click the event. Of course, do not forget to put the referenced statement at the top.
Method 1 is completely correct and there is no problem, but method 2 causes the email to be incorrectly received (*** @ Sina.com ), or, after receiving it, it is also put in a spam (to the scucj@126.com ).

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.