无法生成收到错误的电子邮件

时间:2015-08-12 09:54:23

标签: wpf api web

  

System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException:无法解析远程名称:' mailrelay'           在System.Net.ServicePoint.GetConnection(PooledStream PooledStream,Object owner,Boolean async,IPAddress& address,Socket& abortSocket,Socket& abortSocket6)            在System.Net.PooledStream.Activate(Object owningObject,Boolean async,GeneralAsyncDelegate asyncCallback)      在System.Net.PooledStream.Activate(Object owningObject,GeneralAsyncDelegate asyncCallback)          在System.Net.ConnectionPool.GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)          在System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)           在System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)             在System.Net.Mail.SmtpClient.GetConnection()                       在System.Net.Mail.SmtpClient.Send(MailMessage消息)      ---内部异常堆栈跟踪结束---      在System.Net.Mail.SmtpClient.Send(MailMessage消息)      在EmailSender.PerformDelivery(IMessage消息)

下面是我的代码

    mail = new MailMessage();

      string[] toAddresses = email.To.EmailAddress.Split(new char[] { ';' });
      foreach (string toAddress in toAddresses)
      {
          if (!string.IsNullOrEmpty(toAddress.Trim()))
          {
              mail.To.Add(new MailAddress(toAddress));
          }
      }

      string[] ccAddresses = email.CcEmail.Split(new char[] { ';' });
      foreach (string ccAddress in ccAddresses)
      {
          if (!string.IsNullOrEmpty(ccAddress.Trim()))
          {
              mail.CC.Add(new MailAddress(ccAddress));
          }
      }

      string[] bccAddresses = email.BccEmail.Split(new char[] { ';' });
      foreach (string bccAddress in bccAddresses)
      {
          if (!string.IsNullOrEmpty(bccAddress.Trim()))
          {
              mail.Bcc.Add(new MailAddress(bccAddress));
          }
      }

      mail.From = new MailAddress(email.From.EmailAddress,email.From.FullName);

      mail.Subject = email.Subject;
      mail.Body = email.Body;

      if (email.Body.Contains("<html>"))
      {
          mail.IsBodyHtml = true;
      }

      if (!email.ExcludeAttachment && !string.IsNullOrEmpty(email.AttachmentPath))
          mail.Attachments.Add(new Attachment(email.AttachmentPath));

      client = new SmtpClient();
      client.Host = ConfigurationSettings.AppSettings["SmtpServer"].ToString();
      client.Send(mail);

1 个答案:

答案 0 :(得分:0)

看起来像ConfigurationSettings.AppSettings [“SmtpServer”]。ToString()返回“mailrelay”

检查您的AppSettings(通常在web.config中),找到SmtpServer设置并将其更改为机器名,IP地址或域名,这是一个邮件中继,它将接受来自您应用的电子邮件(大多数邮件中继被保护到防止垃圾邮件发送者使用它们。)

如果“mailrelay”在您的开发机器上运行,请ping它并找出它要解析的IP地址。它需要解析为您的其他环境也可以看到的计算机。如果它是本地测试机器,那么托管环境中的生产服务器可能无法访问它。