当前位置: 代码迷 >> Web Service >> System.net发邮件解决办法
  详细解决方案

System.net发邮件解决办法

热度:250   发布时间:2016-05-02 02:38:08.0
System.net发邮件
公司要求在程序中出现某个错误时,发邮件给管理员,以下是我写的发邮件方法(以QQ邮箱为例吧),运行时不报错,但是邮件没发出去。请各位高手帮忙看下,本人菜鸟,第一次接触.net。

  #region  发邮件
        public static void sendMail(ReturnEntity re)
        {
            AdminEmail email = new AdminEmail();
            email.To = ConfigurationManager.AppSettings["to"];[email protected]
            email.From = ConfigurationManager.AppSettings["from"];[email protected]
            email.UserName = ConfigurationManager.AppSettings["userName"];[email protected]
            email.Password= ConfigurationManager.AppSettings["password"];//123456
            email.DisplayName = ConfigurationManager.AppSettings["displayName"];
            email.Subject = "FORM_NUM的单据同步EFT状态出错";
            email.Message = re.ErrorDesc;
            
            try
            {
                MailMessage msg = new MailMessage();
                msg.Body = email.Message;//邮件的内容
                msg.Subject = email.Subject;//邮件的主题
                msg.From = new MailAddress(email.From,"",Encoding.Default);//发件人得地址
                msg.To.Add(email.To);//收件人的地址
                SmtpClient client = new SmtpClient();
                client.UseDefaultCredentials = true;
                client.Port = 25;
                client.Credentials = new NetworkCredential(email.UserName,email.Password);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Host = "smtp.qq.com";
                object obj = msg;
                client.SendAsync(msg,msg);
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
  相关解决方案