-
using System.Web.Mail;
private void Button1_Click(object sender, System.EventArgs e)
{
//Instantiate the MailMessage object
System.Web.Mail.MailMessage mail=new System.Web.Mail.MailMessage();
//Define the sending address of the email. You can fill in a non-existent address at will. This is how spam is done. ^_^
mail.From=" [email protected] ";
//Define the email receiving address
//Set a semicolon separated list of recipient email addresses
mail.To=" [email protected] ";
//Define the BCC address of the email
//Set a semicolon separated list of email addresses
//mail.Bcc=" [email protected] ";
//Define the carbon copy address of the email
//Set a semicolon separated list of email addresses
//mail.Cc=" [email protected];[email protected]
//Define the subject of the email
mail.Subject="widebright mail test asp.net send mail learning";
//Set the content type of the email body
//Here we send it in HTML format
mail.BodyFormat=System.Web.Mail.MailFormat.Html;
//Set the body of the email
mail.Body="Test it and see if it works";
//Strings enclosed in @ begin with @ and are also enclosed in double quotes. For example:
//The advantage of quoting @ is that the escape sequence is not processed, so you can easily write out a string, such as a fully qualified file name:
//@"c:DocsSourcea.txt " // rather than "c:\Docs\Source\a.txt"
//To include a double quote within an @-quoted string, use two pairs of double quotes:
// @"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.
// Another use of the @ symbol is with the referenced (/reference) identifier which happens to be a C# keyword.
// http://msdn2.microsoft.com/zh-CN/library/362314fe(VS.80).aspx
//Set an attachment
System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@" F:dddddd.txt");
//Attach an attachment to the email
mail.Attachments.Add(mailAttachment);
//Continue to add multiple attachments here
mailAttachment=new System.Web.Mail.MailAttachment(@"F:asp_net Send Mail Code_Baidu Know.mht");
mail.Attachments.Add(mailAttachment);
//SMTP server, because it is set up on this machine, so write 127.0.0.1. If you are connected to another server, like 163 mailbox, write smpt.163.com
System.Web.Mail.SmtpMail.SmtpServer="127.0.0.1";
//It is said that many SMTP servers require authentication to prevent spam. It seems to be called the extended smpt protocol or something.
//But what is connected here is its own smpt server, simple smpt, so there is no verification.
//As for whether verification is required when sending emails from the local SMPT server to 163 or other mailboxes, I don’t know. During actual testing, emails can be sent to
//@163.com , @eyou.com, @x.cn, no verification is required.
//verify
//mail.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate ", "1");
//Login name
//mail.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendusername ", "widebright");
//Login password
//mail.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendpassword ", "widebright");
//send
System.Web.Mail.SmtpMail.Send(mail);
}
The above code uses the System.Web.Mail.SmtpMail class provided in .Net to send emails and automatically connect
SMPT server to send emails. Some people use TCP classes or sockets to write code to implement the smpt protocol, but that requires a lot of coding, protocols, etc., which is quite troublesome. I have done one before using VB. The smpt protocol is relatively simple, but the encoding of those attachments requires a lot of code.
This is just the code for sending SMTP messages. If you want to receive messages and wait, you must have a pop3 server that supports the pop3 protocol.
The smtp server is already installed after IIS is installed. Pop3 does not come with it, so it may be a bit more troublesome to install. I haven’t tried it yet!
SMTP server configuration
I enabled it myself after installing IIS 5.1. I just configured it in the IIS Information Service Manager.
1. Add several domain names in Domains: right-click Domains in "Default SMTP Virtual Server"
Select "New" to create a new one, ---> Domain Type and select "Remote".
Domain name is filled in 163.com eyou.com x.cn respectively, because I want to send emails to these servers, and the same as in the above code
===============
mail.To=" [email protected] ";
===============
Correspondingly, to whom you want to send an email, you must set the corresponding domain name. If you do not set it, the email cannot be sent.
2. Right-click on a domain name such as 163.com, select "Properties", and check "Allow incoming mail to be relayed to this domain". The email can only be sent out after checking it, otherwise there will still be an error.
For the Route domain below, just select the default Use DNS to route to domain.
The key is to set the corresponding SMTP server domain name in IIS, and then you can send emails. I tested several mailboxes on the asp.net page and they can be sent normally. Like 163 mailbox, click the button to send, and you will receive it.
Just found out you can add it
*.com
*.net
*cn
Domain name to match most email addresses.
If you add the *.com domain name, you can send letters to @163.com and @eyou.com
The test passed under windows xp+visual sutdio 2003+iis5.1 environment.
However, I only found that the letters sent to the Sina mailbox were not received, and I don’t know why.
SMTP typical error 550 5 7 1 Uable to relay occurs
In addition to the domains domain name is not set properly, there may be
The "allow computers which successfully authenticate to relay" check box is not selected in the settings of the SMTP virtual server. As shown below,
First open the Access page of the SMTP Virtual Server property page:
Then click the "Relay" button and see if "Allow all computers which successfully authenticate to relay,regardless of the list above" is selected: