2011-10-08 82 views
0

好的,只要我輸入這個,我注意到可能涵蓋相同問題的變體主題。我拜訪了他們中的大多數,發現與我所要求的沒有直接關係,所以我要求耐心。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但不能真正直接找東西幫我(所以任何鏈接讚賞)

非常感謝,並期待着閱讀您的答覆:)

回答

1

談談您的系統管理員,並得到您需要配置的Exchange Server的名稱。

+0

OK當我這樣做時,如何配置任何示例代碼? –

+0

您應該檢查這些設置併爲主機名提供正確的值 -

+0

我實際上在源代碼中找到所有設置。 (但()((((( –

1

有時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

試過了上面的解決方案,但它沒有編譯一些尷尬的錯誤......我剛剛收到我們的源代碼的訪問權限,所以我會去那裏查看詳細信息關於交換服務器 –

2

試試這個獨立的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; 

     } 

    } 
0
 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的雅虎等

相關問題