In ASP, you can send simple emails by calling the CDONTS component. In ASP.Net, it is also possible. 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=" [email protected] ";
mail.To=" [email protected] ";
mail.BodyFormat=MailFormat.Text;
mail.Body="a test smtp mail.";
mail.Subject="ru ok?";
SmtpMail.SmtpServer="localhost";
SmtpMail.Send(mail);
</script>
Normally, the system calls the default SMTP virtual server that comes with IIS to send emails. However, we often encounter this error message:
The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for [email protected]
The reason for this error is not only the possibility of wrong address, There is another important reason. As mentioned above, IIS does not have a real email function, but only uses an "SMTP virtual server" to forward emails. In MSDN, there is the following tip:
If your 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 need to find out if there are any smart hosts available on the network Can be used to relay SMTP messages destined for the Internet.
A smart host is an SMTP server that relays outbound email sent directly to the Internet from an internal SMTP server. The smart host should be able to connect to both the internal network and the Internet to serve as an email gateway.
Open the default SMTP virtual server-Properties-Access-Relay restrictions. You can see that this forwarding or relay function is restricted. In the restriction 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 mail servers, such as IMail, Exchange, etc., you often encounter the problem that the server requires sender authentication (ESMTP). When using a server that requires verification of the sender's identity, an error will occur:
The server rejected one or more recipient addresses. The server response was: 550 not local host ckocoo.com, not a gateway
In the past, I encountered this in ASP There is no possibility to solve the problem. We can only use the CDO component (the parent component of CDONTS) directly:
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 has obviously been considered. The Fields collection has been added to the MailMessage component to easily increase the sender identity in the ESMTP mail server. Verification issues. However, this method only applies to .Net Framework 1.1, not .Net Framework 1.0 version. The procedure for sending emails with sender authentication is as follows:
<%@ Import Namespace="System.Web.Mail" %>
<script runat="server">
MailMessage mail=new MailMessage();
mail.From=" [email protected] ";
mail.To=" [email protected] ";
mail.BodyFormat=MailFormat.Text;
mail.Body="a test smtp mail.";
mail.Subject="ru 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 no longer need to rely on third-party components such as Jmail and EasyMail, but simply use SmtpMai to complete sending emails!