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 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 is included in Windows 2000 and Windows Server 2003) and is located behind the firewall blocking any direct SMTP traffic through 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 provided by IIS but use other real email servers, such as IMail and Exchange, ESMTP is often encountered when the server requires Sender authentication ). 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 CDONTS's parent component ):

 conf.Fields[CdoConfiguration.cdoSMTPAuthenticate].Value=CdoProtocolsA
uthentication.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", "walkor"); //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!

  1. Use the Treeview control and XML in ASP. NET
  2. Nine code writing specifications that ASP. NET should comply
  3. ASP. NET learning Roadmap
  4. Improvements in visual aspects of ASP. net mvc instances and new RC versions

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.