//一:用自己的服务器作为邮件服务器时:
//如出现:邮箱不可用。 服务器响应为: 5.7.1 Unable to relay for
//解决办法如下:在IIS中,右击“默认SMTP虚拟服务器”,选择“属性”,切换到“访问”页,点击“中继”按钮,在弹出框中选择“仅以下列表除外”,确定。
//二:用其它的邮件服务器发送,经测试大多数免费邮箱都不提供SMTP服务,用qq邮箱的就可以,方法:进入qq邮箱-->设置-->帐户-->开通POP3/IMAP/SMTP服务,即可用smtp.qq.com作为邮件服务器了,可用它来发送邮件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
//第一个参数是邮箱服务器
//第二个参数发件人的帐号
//第三个参数发件人密码
//第四个参数收件人帐号
//第五个参数主题
//第六个参数内容.
protected void Button1_Click(object sender, EventArgs e)
{//发送邮件
try
{
//SendSMTPEMail("nz.oicp.net", "", "123", "", "123", "用asp.net发送邮件,用自己的邮件服务器,测试成功");
SendSMTPEMail("smtp.qq.com", "", "123xxxxxxx", "", "123", "用asp.net发送邮件,用qq的smtp.qq.com服务器,测试成功");
}
catch (Exception er)
{
Label1.Text = er.Message;
}
}
}