Asp.net email sending code!

Source: Internet
Author: User
Tags mailmessage

Asp.net email sending code
When it comes to sending emails, first mention SMTP. 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. SMTP
The 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
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 can set various attribute values of the MailMessage object.
If we use ASP. NET to write a mail sending program, how should we first obtain SMTP. There are two methods: the first method calls the SMTP of a well-known mail service provider, such as the new
Lang, Sohu, and Netease's free email SMTP; the second method is to install an SMTP virtual server, this 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. Assume that
A free email is registered on the Sina mail site (mail.sina.com.cn). The user name is fei_880221 and the password is 520520.
The SMTP address is smtp.sina.com.cn. We need to send an email to 56381234 @ qq.com. The core code for sending emails using ASP. NET (C #) is as follows:

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 = "mailto: fei_880221.com % 22; // source email address
ObjMailMessage. To = "mailto: 563812344@qq.com"; // destination email address
ObjMailMessage. Subject = "mail title: Hello! "; // The title of the sent Email
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", "fei_880221 ");
// Password
ObjMailMessage. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", "520520 ");
/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 );

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)". I set 192.168.1.1.
(2) Select the "access" tab, click "relay", select "only the following list" (selected by default), and click "add ", add 192.168.1.1 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 for 563812344@qq.com
 
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 = "mailto: fei_880221@sina.com; // source email address
Objmailmessage. To = "mailto: 563812344@qq.com"; // destination email address
Objmailmessage. Subject = "mail title: Hello! "; // The title of the sent Email
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.1 ";
// Start sending emails
Smtpmail. Send (objmailmessage );

The following is a real example of my own project, which is equivalent to the second method above! I also saw a blog and tampered with it... Haha !~

1. First import to the namespace
/// <Summary>
/// Send an email after Register
/// </Summary>
/// <Param name = "email"> </param>
/// <Param name = "customer"> </param>
Public void RegisterSend (string emailAddress, string userName, string userPass)
{
MailMessage objMailMessage;

// Create an email message
ObjMailMessage = new MailMessage ();
// Sending Address
ObjMailMessage. From = "xinuo_keji@163.com ";
// Receiving address
ObjMailMessage. To = emailAddress;
// Set the mail body content type. In this example, you need to write a hyperlink in the content, so the HTML format is required.
Objmailmessage. bodyformat = mailformat. html;
// Email Subject
Objmailmessage. Subject = "Thank you for using Sino technology! ";
// Email content

Objmailmessage. Body = "click the following link to confirm registration! <Br> <
Href = 'HTTP: // localhost: 1055/CMS/registorreturn. aspx? UN = "+ username + "&
Up = "+ userpass +" '> click here </a> ";
// The IP address of the server. Because we use a local virtual SMTP server, you only need to enter the local IP address.
Smtpmail. smtpserver = "192.168.0.114 ";
// Send
SmtpMail. Send (objMailMessage );
}

Method 2: I succeeded, but he always sends it to the spam in the fuel tank!

 

 

Bytes ------------------------------------------------------------------------------------------------------

Full text

URL: http://blog.csdn.net/lanlan520/archive/2007/07/27/1710859.aspx

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.