ASP.NET交换服务器发送电子邮件C#

问题描述:

好的,只要我输入这个,我注意到可能涵盖相同问题的变体主题。我拜访了他们中的大多数,发现与我所要求的没有直接关系,所以我要求耐心。ASP.NET交换服务器发送电子邮件C#

无论如何,我正在使用VS2010创建一个ASP.NET Web应用程序。我想使用的代码通过SMTP发送电子邮件:

 MailMessage mailMsg = new MailMessage(); 

     mailMsg.From = new MailAddress(fromEmail); 
     mailMsg.To.Add(toEmail); 
     mailMsg.Subject = emailSubj.ToString().Trim(); 
     mailMsg.Body = msgBody.ToString().Trim(); 
     SmtpClient smtpClient = new SmtpClient(); 
     smtpClient.Send(mailMsg); 

但每次我得到下面的异常(SMTPException和的InnerException说{"Unable to connect to the remote server"}

我也定义在web.config如下:

<system.net> 
    <mailSettings> 
    <smtp> 
     <network host="company.com"/> 
    </smtp> 
    </mailSettings> 
</system.net> 

我想要做的是发送一封电子邮件,一旦提交表单与请求ID,以便它可以通过其他页面访问(除邮件以外的所有作品)在我们使用的公司交换服务器,当我去我的合作ntact属性我得到smtp:[email protected]

那么这里要做什么?我检查Web Services ExchangeServiceBinding但不能真正直接找东西帮我(所以任何链接赞赏)

非常感谢,并期待着阅读您的答复:)

谈谈您的系统管理员,并得到您需要配置的Exchange Server的名称。

+0

OK当我这样做时,如何配置任何示例代码? –

+0

您应该检查这些设置并为主机名提供正确的值 -

+0

我实际上在源代码中找到所有设置。 (但()((((( –

有时smtp服务器是不同的,你正在看。 为如: 我的电子邮件是[email protected], 我的实际SMTP服务器 server1.mail.mycompany.com,server2.mail.mycompany.com 你要问您的管理该服务器名称

之后,要求你的用户是否在AD上定义,是否需要验证每个smtp发送?

您的交换主机是否使用基于TLS的SMTP? AFAIK某些交换管理员使用SMTP over SSL或TLS声明。您可以通过为其电子邮件获取当前交换/窗口证书来查看有关通过SSL或TLS使用SMTP发送的MSDN文档。

+0

试过了上面的解决方案,但它没有编译一些尴尬的错误......我刚刚收到我们的源代码的访问权限,所以我会去那里查看详细信息关于交换服务器 –

试试这个独立的C#应用​​程序,看看主机名是否工作。否则,您需要联系管理员以获取正确的地址。

 /// <summary> 
     ///Method to Send an Email informing interested parties about the status of data extraction. 
     /// INPUTS : int iProcessIsSuccess : int informing about the success of the process. -1 means failure, 0 means partial success, 1 means success. 
     ///   string szLogDataToBeSent : Log data to be sent incase process not successful. 
     /// OUTPUTS : bool. True if success, else false. 
     /// </summary> 
     public bool SendEmailNotification(string szEmailAddressFileName, int iProcessIsSuccess, string szLogDataToBeSent) 
     { 
     bool bSuccess = false; 

     //the the SMTP host. 
     SmtpClient client = new SmtpClient(); 

     //SMTP Server 
     client.Host = CommonVariables.EMAIL_SMTP_SERVER; 

     //SMTP Credentials 
     client.Credentials = new NetworkCredential(CommonVariables.EMAIL_USERNAME, CommonVariables.EMAIL_PASSWORD); 

     //Creating a new mail. 
     MailMessage mail = new MailMessage(); 

     //Filling 'From' Tab. 
     mail.From = new MailAddress(CommonVariables.EMAIL_SENDERS_ADDRESS, CommonVariables.EMAIL_SENDERS_NAME); 

     //Filling 'To' tab. 
     List<EmailAddress> EmailAddressList = new List<EmailAddress>(); 
     try 
     { 
      using (System.IO.FileStream fs = new FileStream(szEmailAddressFileName, FileMode.Open)) 
      { 
       XmlSerializer xs = new XmlSerializer(typeof(List<EmailAddress>)); 
       EmailAddressList = xs.Deserialize(fs) as List<EmailAddress>; 
      } 

      foreach (EmailAddress addr in EmailAddressList) 
      { 
       mail.To.Add(addr.RecepientEmailAddress); 
      } 
     } 
     catch(Exception Ex) 
     { 
      mail.To.Add("[email protected]"); 
     } 

     //Filling mail body. 
     string szMailBody = ""; 
     string szMailSubject = ""; 

     if (1 == iProcessIsSuccess) //Success 
     { 
      szMailSubject = String.Format(CommonVariables.EMAIL_SUBJECT_BOILER_PLATE, "a SUCCESS"); 
      szMailBody = String.Format(CommonVariables.EMAIL_BODY_BOILER_PLATE, DateTime.UtcNow.ToString(), Environment.MachineName); 
      szMailBody += "\r\n" + szMailSubject + "\r\n"; 

     } 
     else if (0 == iProcessIsSuccess) //Partially Success 
     { 
      szMailSubject = String.Format(CommonVariables.EMAIL_SUBJECT_BOILER_PLATE, "a PARTIAL SUCCESS"); ; 
      szMailBody = String.Format(CommonVariables.EMAIL_BODY_BOILER_PLATE, DateTime.UtcNow.ToString(), Environment.MachineName); 
      szMailBody += "\r\n"+ szMailSubject + "\r\n"; 
      szMailBody += "\r\n" + "The Log data is as follows:\r\n"; 
      szMailBody += szLogDataToBeSent; 
      mail.Priority = MailPriority.High; 
     } 
     else //Failed 
     { 
      szMailSubject = String.Format(CommonVariables.EMAIL_SUBJECT_BOILER_PLATE, "a FAILURE"); ; 
      szMailBody = String.Format(CommonVariables.EMAIL_BODY_BOILER_PLATE, DateTime.UtcNow.ToString(), Environment.MachineName); 
      szMailBody += "\r\n" + szMailSubject + "\r\n"; 
      szMailBody += "\r\n" + "The Log data is as follows:\r\n"; 
      szMailBody += szLogDataToBeSent; 
      mail.Priority = MailPriority.High; 
     } 

     mail.Body = szMailBody; 

     mail.Subject = szMailSubject; 

     //Send Email. 
     try 
     { 
      client.Send(mail); 
      bSuccess = true; 
     } 
     catch (Exception Ex) 
     { 
      bSuccess = false; 
     } 

     // Clean up. 
     mail.Dispose(); 


     return bSuccess; 

     } 

    } 

 string _SMTP = WebConfigurationManager.AppSettings["SMTP"]; 
     Int32 _Port = Convert.ToInt16(WebConfigurationManager.AppSettings["Port"]); 
     string _SMTPCredentialName = WebConfigurationManager.AppSettings["SMTPCredentialName"]; 
     string _SMTPCredentialPassword = WebConfigurationManager.AppSettings["SMTPCredentialPassword"]; 
     string _Body = null; 

     System.Net.Mail.MailMessage _MailMessage = new System.Net.Mail.MailMessage(); 
     try 
     { 
      _MailMessage.To.Add(_RegUserEmail); 
      _MailMessage.From = new System.Net.Mail.MailAddress(_FromEmail, _FromName); 
      _MailMessage.Subject = _Subject; 
      _Body = ReadTemplateRegistration(_RegisterName, _RegUserName, _RegUserEmail, _Pass, _Path); 
      _MailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8"); 

      AlternateView plainView = AlternateView.CreateAlternateViewFromString(System.Text.RegularExpressions.Regex.Replace(_Body, "<(.|\\n)*?>", string.Empty), null, "text/plain"); 
      AlternateView htmlView = AlternateView.CreateAlternateViewFromString(_Body, null, "text/html"); 

      _MailMessage.AlternateViews.Add(plainView); 
      _MailMessage.AlternateViews.Add(htmlView); 

      System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient(_SMTP, _Port); 

      System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(_SMTPCredentialName, _SMTPCredentialPassword); 

      mailClient.UseDefaultCredentials = false; 

      mailClient.Credentials = basicAuthenticationInfo; 

      _MailMessage.IsBodyHtml = true; 

      mailClient.Send(_MailMessage); 
     } 
     catch (Exception ex) 
     { 

      return "ERROR" + ex.ToString(); 
     } 

这是使用C#发送邮件的最佳方法,您可以使用此方法的所有要去的电子邮件系统,包括交换服务器,POP3,SMTP,Gmail时,Hotmail的雅虎等