ASP.NET发送电子邮件

/// <summary>
/// 发送EMAIL
/// </summary>
/// <param name="sRecipientEmail">收件人地址</param>
/// <param name="sSubject">主题</param>
/// <param name="sMessage">内容</param>
/// <param name="sSendName">发件人名称</param>
/// <returns>发送是否成功</returns>
public static bool SendMail(string sRecipientEmail, string sSubject, string sMessage, string sSendName)
{
    //邮件对象
    MailMessage emailMessage;
   
    // 初始化邮件对象
    if (string.IsNullOrEmpty(sSendName))
    {
        sSendName = "客户服务中心";
    }

    //发件人
    string sSenderEmail = "" + sSendName + "<" + ConfigurationManager.AppSettings["EmailAddress"] + ">";
    emailMessage = new MailMessage(sSenderEmail, sRecipientEmail, sSubject, sMessage);
    emailMessage.IsBodyHtml = true;
    emailMessage.SubjectEncoding = System.Text.Encoding.Default;
    emailMessage.BodyEncoding = System.Text.Encoding.Default;

    //加入
    emailMessage.Headers.Add("X-Priority", "3");
    emailMessage.Headers.Add("X-MSMail-Priority", "Normal");
    emailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
    emailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
    emailMessage.Headers.Add("ReturnReceipt", "1");

    //邮件发送客户端
    SmtpClient client = new SmtpClient();

    //邮件服务器及帐户信息
    client.Host = ConfigurationManager.AppSettings["EmailServer"];
    client.Credentials = new System.Net.NetworkCredential("邮箱账号", "邮箱密码");
    try
    {
        client.Send(emailMessage);
    }
    catch
    {
        return false;
    }
    return true;
}

 

在web.config文件中的<appSettings></appSettings>中间添加如下几项:

<add key="EmailAddress" value="abc@163.com"/>
<add key="EmailServer" value="smtp.163.com"/>

                                                                                                                                                              --摘自互联网并修改

阅读更多
个人分类: ASP.NET
下一篇浮动QQ咨询
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭