Ways to send mail using smtpmail in asp.net

Source: Internet
Author: User
Tags iis mail net mailmessage

In ASP, you can send a simple message by calling the CDONTS component, which is natural in asp.net. The difference is that, in the. Net framework, this component is encapsulated into the System.Web.Mail namespace.

A typical mail-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>

Typically, the system calls the default SMTP virtual server with IIS to deliver mail. However, you will often encounter such error prompts:

The server rejected one or more recipient addresses. The server response was:550 5.7.1 Unable to relay for brookes@brookes.com

The reason for this error, in addition to the possibility of address errors, there is another important reason. As mentioned above, IIS does not carry true messaging functionality, but simply borrows an "SMTP virtual server" to enable forwarding of messages. In MSDN, there are hints that if the local SMTP server, including in Windows 2000 and Windows Server 2003, is behind a firewall that blocks any direct SMTP traffic (via port 25), you will need to find out if there are any smart hosts available on the network SMTP messages that can be used to relay to the Internet.

A smart host is an SMTP server that can relay outgoing e-mail messages sent directly to the Internet from an internal SMTP server. A smart host should be able to connect to both the internal network and the Internet for use as an e-mail gateway.

Open the Default SMTP virtual Server-Properties-access-relay restrictions, and you can see that this forwarding or relaying function is limited. In the Limit list, add the IP addresses of the hosts that need to use this server to resolve the issues mentioned above.

If you do not use an SMTP virtual server with IIS and use other real mail servers, such as Imail,exchange, you often encounter problems with the server needing to send a sender authentication (ESMTP). An error occurs when you use a server that needs to verify the sender identity:

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

Previously in ASP, there was no solution to this problem, and only the CDO component (CDONTS's parent component) could be used directly:

Conf. Fields[cdoconfiguration.cdosmtpauthenticate]. Value=cdoprotocolsauthentication.cdobasic;
Conf. Fields[cdoconfiguration.cdosendusername]. Value= "Brookes";
Conf. Fields[cdoconfiguration.cdosendpassword]. Value= "XXXXXXX";
This requirement is clearly taken into account in the. Net Framework 1.1, which adds a fields set to the MailMessage component that increases the issue of sender authentication in the ESMTP mail server. However, this method applies only to the. NET Framework 1.1 and does not apply to the. NET Framework version 1.0. The mail-sending program 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"," Walkor "); Set your password here
Smtpmail.smtpserver= "Lsg.moon.net";
Smtpmail.send (mail);
</script>

With this method, you can finally do not need to rely on JMail, Easymail, and other third-party components, but simply use Smtpmai can be completed mail sent.



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.