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

System.net发邮件解决方法

热度:210   发布时间:2016-05-02 02:52:57.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);
  }

  }
  #endregion

------解决方案--------------------
这个发邮件不稳定就是,应该是没什么问题,看看是不是进了垃圾邮件。。。

下面是一种发邮件的方式,以前用过的,建议用用Jmail
C# code
  #region                public static void Sends(string email)         {            string formto = "发件箱地址";            string to = email;   //接收邮箱            string content = "";            string body = "";            string name = "发件箱";            string upass = "密码";            string smtp = "smtp地址";            SmtpClient _smtpClient = new SmtpClient();                        _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式            _smtpClient.Host = smtp; //指定SMTP服务器            _smtpClient.Credentials = new System.Net.NetworkCredential(name, upass);//用户名和密码            MailMessage _mailMessage = new MailMessage();            //发件人,发件人名             _mailMessage.From = new MailAddress(formto, "别名");            //收件人             _mailMessage.To.Add(to);            _mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");            _mailMessage.Subject = content;//主题            _mailMessage.Body = body;//内容            _mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");//正文编码            _mailMessage.IsBodyHtml = true;//设置为HTML格式            _mailMessage.Priority = MailPriority.High;//优先级               try            {                _smtpClient.Send(_mailMessage);            }            catch (Exception)            {                                            }                    }        #endregion
  相关解决方案