In Asp. Net, use SmtpMail to send mails. asp. netsmtpmail

Source: Internet
Author: User
Tags mailmessage

In Asp. Net, use SmtpMail to send mails. asp. netsmtpmail

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 sending program 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? ";
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 relay for brookes@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>

 

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.