Simulate and test smtp mail in asp.net, asp. netsmtp

Source: Internet
Author: User

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 ....

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.