2011-10-08 9 views
0

좋아,이 질문을 입력하자마자 동일한 질문을 다룰 수있는 변형 된 항목을 발견했습니다. 나는 그들 중 대부분을 방문했고 내가 구하는 것에 직접적인 관계가 없다는 것을 발견했다. 그래서 나는 인내한다.ASP.NET Exchange Server 전자 메일 보내기 C#

어쨌든 VS2010을 사용하여 ASP.NET 웹 응용 프로그램을 만들고 있습니다. 내가 코드를 사용하여 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은 Web.config의에서 다음 {"Unable to connect to the remote server"}

가 나는 또한 정의 말한다 :

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

내가하려는 것은 다른 페이지 (메일을 제외한 모든 작업)를 통해 액세스 할 수 있도록 양식이 요청 ID와 함께 제출되면 이메일을 보내는 것입니다. 우리가 사용하는 회사 Exchange 서버와 내가 공동 작업 할 때 ntact 속성 내가 얻을 smtp:[email protected]

그래서 여기서 무엇을 할 것인가? 나는 Web Services ExchangeServiceBinding 확인했지만 정말 직접 (그래서 어떤 링크 감사)

감사합니다 많이 도와 뭔가를 발견하고

답변

1

토크 시스템 관리자에 응답을 :) 읽기를 기대하고 가져올 수 없습니다 구성해야하는 Exchange Server의 이름입니다.

+0

확인을 내가 할 때, 구성하는 방법의 모든 샘플 코드? –

+0

이 설정을 확인하고 호스트 이름에 올바른 값을 제공해야합니다. -

+0

소스 코드의 모든 설정을 찾았습니다. 아래 쪽은 Vb로 작성되었으며 C#로 작성되지 않았습니다. ((( –

1

간혹 smtp 서버가 당신과 다른 경우가 있습니다. 예를 들어 대한 : 내 이메일은 [email protected], 내 실제 SMTP 서버입니다 server1.mail.mycompany.com, server2.mail.mycompany.com 당신이 당신의 관리자에게 문의해야이 서버 이름

그런 다음 AD에 정의 된 경우 사용자에게 물어보십시오. 그러면 각 SMTP 전송에 대해 인증이 필요합니까?

교환 호스트가 TLS를 통한 SMTP를 사용하고 있습니까? 일부 교환 관리자는 SSL 또는 TLS를 통한 SMTP를 사용하여 신고합니다. 해당 전자 메일에 대해 현재 Exchange/Windows 인증서를 가져 와서 SMTP 또는 SSL을 통해 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# .NET을 사용하여 이메일을 보내는 가장 좋은 방법이며,이 방법을 사용할 수있는 모든 등 Exchange 서버, POP3, SMTP, Gmail은, 핫메일 야후를 포함한 이메일 시스템을가는