Simulate and test smtp mail in asp.net, asp. netsmtp
I recently wrote the following code to test the function of a member email in my programming life.
In asp.net, sometimes the SMTP test is required. However, in unit testing
What to do with smtp? In fact, there is still a way to simulate it. The following explains:
Set in web. config
<System.net>
<MailSettings>
<Smtp deliveryMethod = "SpecifiedPickupDirectory"> <specifiedPickupDirectory pickupDirectoryLocation = "C: \ Mail \"/> </smtp>
</MailSettings>
</System.net>
In this case, the specified c: \ mail is the directory of the specified inbox,
The code can be written as follows:
Protected void btnMail_Click (object sender, EventArgs e) {MailMessage message = new MailMessage ("abc@somedomain.com", "abc@abcdefgh.com", Newsletter "," This is a test mail ");
SmtpClient client = new SmtpClient ("localhost ");
Client. Send (message );}
}
In this case, you will find that your email in SMTP format will be sent in c: \ mail after running.
This test method is actually very useful, because sometimes it involves the use of SMTP Information Exchange Platform packets,
This test method is good. You can clearly see the content of the packet header and the packet body.
For emails sent by aspnet, it is verified only by smtp and email name. The Code is as follows:
If the host provider is not allowed to send emails from the web server (localhost), you need to set smtp authentication. For smtp authentication, you need to set the smtp host and smtp User (that is, the email address ), smtp password, and the sender's mailbox must be the same as the smtp User. In this case, the host provider needs to prevent the use of their servers to send emails. If smtp authentication is set to be sent from the mail server, it is better to find out who is the culprit if someone sends the email.
Use your own SMTP service in IIS to send emails
The built-in IIS is only lightweight. You need to have a Domain Name and configure the MX record for sending the Internet. Otherwise, the recipient's server may reject the invalid email. (otherwise, you can send an email without forging an address)
Your domain name is not on the white list of the other party, so it is rejected to prevent spam ....