How to Use smtpmail to send emails in ASP. NET

Source: Internet
Author: User
Tags mailmessage

In ASP, you can call the cdonts component to send a simple email. in ASP. NET, you can also. The difference is that in. NET Framework, this component is encapsulated into the system. Web. Mail namespace.

A typical email sendingProgramAs follows:
<% @ Import namespace = "system. Web. Mail" %>
<SCRIPT runat = "server">
Mailmessage mail = new mailmessage ();
Mail. From = "service@brookes.com ";
Mail. To = "Brookes @ brookes..com ";
Mail. bodyformat = mailformat. text;
Mail. Body = "A test SMTP mail .";
Mail. Subject = "r u OK? ";
Smtpmail. smtpserver = "localhost ";
Smtpmail. Send (Mail );
</SCRIPT>

Generally, the system calls the default SMTP virtual server provided by IIS to send emails. However, the following error message is often displayed:

The server rejected one or more recipient addresses. The server response was: 550 5.7.1 unable to relayBrookes@brookes.com

The cause of this error is not only an address error, but also an important cause. As mentioned above, IIS does not provide the real mail function, but simply uses an "SMTP virtual server" to forward emails. In msdn, the following prompt is displayed:

If the local SMTP server (including in Windows 2000 and Windows Server 2003) is located after blocking any direct SMTP traffic (through the firewall on port 25, you need to check whether there are available smart hosts on the network that can be used to send SMTP messages to the Internet.
A smart host is an SMTP server that can send outgoing emails directly from the internal SMTP server to the Internet. A smart host should be able to connect to both the internal network and the Internet for use as an email gateway.

Open the default SMTP virtual server-properties-access-relay restriction. You can see that this forwarding or relay function is restricted. In the limit list, add the IP address of the host that needs to use this server to solve the problem mentioned above.

If you do not use the SMTP virtual server that comes with IIS but use other real email servers, such as IMail and exchange, you will often encounter the server's need for Sender authentication (ESMTP ). When using a server that requires Sender authentication, an error occurs:

The server rejected one or more recipient addresses. The server response was: 550 not local host ckocoo.com, not a gateway

In the past, there was no solution to this problem in ASP. You can only directly use the CDO component (the parent component of cdonts ):
Conf. Fields [cdoconfiguration. cdosmtpauthenticate]. value = cdoprotocolsauthentication. cdobasic;
Conf. Fields [cdoconfiguration. cdosendusername]. value = "Brookes ";
Conf. Fields [cdoconfiguration. cdosendpassword]. value = "xxxxxxx ";

In. NET Framework 1.1, this requirement is clearly taken into account. The fields set is added to the mailmessage component to easily increase the sender authentication in the ESMTP email server. However, this method applies only to. NET Framework 1.1 and not to. NET Framework 1.0. The mail sender with sender authentication is as follows:

<% @ Import namespace = "system. Web. Mail" %>
<SCRIPT runat = "server">
Mailmessage mail = new mailmessage ();
Mail. From = "service@brookes.com ";
Mail. To = "brookes@brookes.com ";
Mail. bodyformat = mailformat. text;
Mail. Body = "A test SMTP mail .";
Mail. Subject = "r u OK? ";
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // Basic Authentication
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "Brookes"); // set your username here
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", "Either or"); // set your password here
Smtpmail. smtpserver = "lsg.moon.net ";
Smtpmail. Send (Mail );
</SCRIPT>

With this method, you do not have to use third-party components such as jmail and easymail, but simply use smtpmai to send emails!

Reference: How do I authenticate to send an email?

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.