Ways to send mail using smtpmail in asp.net

Source: Internet
Author: User
Tags iis mailmessage

In the ASP, you can send a simple message by calling the CDONTS component, which is natural in the 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

There is an important reason for this error, in addition to the possibility of an incorrect address. As mentioned above, IIS does not carry true messaging functionality, but simply borrows an "SMTP virtual server" to enable forwarding of messages. In MSDN, you have the following tips:

If the local SMTP server (included 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 that can be used to transfer to the Internet SMTP message.

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 no longer need to rely on JMail, easymail and other third-party components, and only simple use of Smtpmai can l complete the delivery of mail!

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.