ASP.NET发送邮件的代码

Keywords:

使用asp.net发送邮件非常简单。

代码如下,注释的很清楚就不详细说了。


    #region
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="mailTo">要发送的邮箱</param>
    /// <param name="mailSubject">邮箱主题</param>
    /// <param name="mailContent">邮箱内容</param>
    /// <returns>返回发送邮箱的结果</returns>
    public static bool SendEmail(string mailTo, string mailSubject, string mailContent)
    {

        //声明一个Mail对象
        MailMessage mymail = new MailMessage();
        //发件人地址
        //如是自己,在此输入自己的邮箱
        mymail.From = new MailAddress("songshizhao@qq.com");
        //收件人地址
        mymail.To.Add(new MailAddress(mailTo));
        //邮件主题
        mymail.Subject = mailSubject;
        //邮件标题编码
        mymail.SubjectEncoding = System.Text.Encoding.UTF8;
        //发送邮件的内容
        mymail.Body = mailContent;
        //邮件内容编码
        mymail.BodyEncoding = System.Text.Encoding.UTF8;

        ////添加附件
        //Attachment myfiles = new Attachment(tb_Attachment.PostedFile.FileName);
        //mymail.Attachments.Add(myfiles);
        ////抄送到其他邮箱
        //mymail.CC.Add(new MailAddress(tb_cc.Text));

        //是否是HTML邮件
        mymail.IsBodyHtml = true;
        //邮件优先级
        mymail.Priority = MailPriority.High;
        //创建一个邮件服务器类
        SmtpClient myclient = new SmtpClient();
        myclient.Host = "smtp-mail.outlook.com";
        //SMTP服务端口
        myclient.Port = 587;
        //验证登录
        myclient.EnableSsl = true;
        myclient.UseDefaultCredentials = false;
        myclient.Credentials = new NetworkCredential("songshizhao@live.com", "******");//"@"输入有效的邮件名, "*"输入有效的密码

        try
        {
            myclient.Send(mymail); // 发送邮件
            return true;
        }
        catch (SmtpException ex)
        {
            return false;
        }
    }
    #endregion


作者:songshizhao 发表于:2017-02-20T09:02:47 访问量:514