BugTracker部署好之后,发现增加bug不能mail提醒。于是补上这个功能记录在此,方法是次要的,主要是找到地方。需要3步。吐槽下Asp的代码风格看的真心蛋疼....

一、发送mail(主要是找到位置)

    1.在App_Code中加入MailHelper.cs .

using System;
using System.Net.Mail;

public class MailHelper
{
    /// <summary>
    /// 异步发送邮件
    /// </summary>
    /// <param name="toMails"></param>
    /// <param name="subject"></param>
    /// <param name="content"></param>
    public static void SystemSendMail(string toMails, string subject, string content)
    {
        
            Action invokeAction = () => WebMailTo(toMails, subject, content);
            invokeAction.BeginInvoke(Callback, invokeAction);
        
    }

    private static void Callback(IAsyncResult ar)
    {
        var action = ar.AsyncState as Action;
        if (action != null) action.EndInvoke(ar);
    }


    /// <summary>
    /// Webs the mail to.
    /// </summary>
    /// <param name="toEmails">To emails.</param>
    /// <param name="subject">The subject.</param>
    /// <param name="emailText">The email text.</param>
    /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
    public static bool WebMailTo(string toEmails, string subject, string emailText)
    {
        #region 此处参数使用时根据需要替换成自己的
        const string server = "172.17.xxx.95"; //此处代表Mail Server地址
        const string formEmail = xxxxx.xxx@xxx.com.cn"; //此处代表系统发邮件的时候的发件人地址
        const string formDispayName = "BugTracker"; //系统发件人的显示名称
        const string formPassword = "xxxxx"; //此处代表系统发邮件的时候的发件人的密码
        const string formDomain = "xxxwj"; //域名
        #endregion

        const string systermtxt = "<br/>该邮件为系统自动发送,请勿回复!详情请点击" + "<a href='http://cnwj6iapc016:84/'>这里~</a>";
        var mailMessage = new MailMessage { IsBodyHtml = true };
        mailMessage.To.Add(toEmails);
        mailMessage.From = new MailAddress(formEmail, formDispayName);
        mailMessage.Subject = subject;
        mailMessage.Body = emailText + systermtxt;//此处可以传递一个html
        mailMessage.Priority = MailPriority.High;
        var client = new SmtpClient
        {
            Host = server,
            UseDefaultCredentials = false,
            Credentials = new System.Net.NetworkCredential(formEmail, formPassword, formDomain),
            DeliveryMethod = SmtpDeliveryMethod.Network,
            EnableSsl = false
        };
        bool isSendOk;
        try
        {
            client.Send(mailMessage);//发送Mail
            isSendOk = true;

        }
        catch (Exception)
        {

            isSendOk = false;
        }
        return isSendOk;
    }
}
View Code

相关文章:

  • 2021-07-08
  • 2022-02-15
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
  • 2021-06-29
  • 2021-07-19
  • 2021-08-22
猜你喜欢
  • 2021-08-16
  • 2021-09-26
  • 2021-05-03
  • 2021-08-05
  • 2021-06-28
  • 2022-01-05
  • 2021-11-29
相关资源
相似解决方案