• 通过smtp直接发送邮件


    /// <summary>
    /// SMTP发送邮件
    /// </summary>
    /// <param name="fromEmail">发送邮件地址</param>
    /// <param name="toEmail">收件箱</param>
    /// <param name="subject">邮箱主题</param>

    /// <param name="contentAttachment"></param>
    /// <param name="fromEmailPwd">发送邮件密码</param>

    /// <returns></returns>


    public static bool SendMailBySMTP(string fromEmail, string toEmail, string subject, Attachment contentAttachment,
    string fromEmailPwd)
    {
    bool rr = true;
    if (ConfigurationManager.AppSettings["CreditoEmailSwitch"].ToString() == "true")
    {

    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(fromEmail, toEmail);


    mail.SubjectEncoding = Encoding.UTF8;
    mail.Subject = subject;
    mail.IsBodyHtml = true; //是否允许内容为 HTML 格式
    mail.BodyEncoding = Encoding.UTF8;
    string emailContent = "邮件内容";

    mail.Body = emailContent;

    mail.Attachments.Add(contentAttachment); //添加一个附件
    int defaltFlag = 0;

    SmtpClient smtp = new SmtpClient("fastsmtp.qiye.163.com");//serviceFlag > 14 ? "smtp.qiye.163.com" :
    smtp.Port = 25;
    smtp.EnableSsl = true;
    smtp.Credentials = new NetworkCredential(fromEmail, fromEmailPwd); //SMTP 验证
    //smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

    try
    {
    smtp.Send(mail);

    }
    catch (Exception ex)
    {
    rr = false;
    SysLog.Write("发送邮件异常", ex.Message + "时间:" + DateTime.Now.ToString());
    }
    finally
    {
    mail.Attachments.Dispose(); //邮件发送完毕,释放对附件的锁定

    }
    }
    else
    {
    rr = false;
    }
    return rr;
    }

  • 相关阅读:
    【NYOJ】[22]素数求和问题
    【NYOJ】[13]Fibonacci数
    【NYOJ】[13]Fibonacci数
    【CodeForces】[606A]Magic Spheres
    【CodeForces】[606A]Magic Spheres
    【杭电】[1201]18岁生日
    【杭电】[1201]18岁生日
    【杭电】[1994]利息计算
    【杭电】[1994]利息计算
    新型量子云装置问世 让你免费体验量子计算机
  • 原文地址:https://www.cnblogs.com/CSYgo/p/8549094.html
Copyright © 2020-2023  润新知