2010-08-16 2 views
1

전자 메일 구현의 완온성 검사를하시기 바랍니다 .--) 내가 놓친 부분이 있습니까? C# 이메일 보내기 - 정상 성 체크

string supplierOfThisMaterialEmailAddress = "[email protected]"; // TODO 


string htmlBodyIncludingReplacements = "<html><head><title>E-mail</title></head><body><div>. There has been a request on the hello.co.nz website for: " + txtMaterialDescription.Text + 
      "<br />Full Name: <b>" + fullName + "</b><br />" + 
      etc.."; 

     string textOnlyBodyIncludingReplacements = "E-mail. There has been a request on the freematerials.co.nz website for: " + txtMaterialDescription.Text + 
      "Full Name: " + fullName + 
      "etc.."; 

     string subject = "Someone has contacted you"; 

     CustomMailer mailer = new CustomMailer(); 
     string result = mailer.SendEmail(subject, htmlBodyIncludingReplacements, supplierOfThisMaterialEmailAddress, textOnlyBodyIncludingReplacements); 
     if (result != null) 
      lblMessage.Text = result; 
     else 
      lblMessage.Text = "Thank you - email has been sent"; 

그리고 클래스 : 언뜻

public class CustomMailer 
    { 
     public string SendEmail(string subject, string htmlBodyIncludingReplacements, string emailTo, string textOnlyBodyIncludingReplacements) 
     { 
      try 
      { 
       MailAddress sender = new MailAddress("[email protected]", "Dave Mateer"); 
       emailTo = "[email protected]"; // testing 
       MailAddress recipient = new MailAddress(emailTo, null); 

       MailMessage message = new MailMessage(sender, recipient); 
       message.Subject = subject; 

       AlternateView textView = AlternateView.CreateAlternateViewFromString(textOnlyBodyIncludingReplacements, null, "text/plain"); 
       AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBodyIncludingReplacements, null, MediaTypeNames.Text.Html); 
       message.AlternateViews.Add(textView); 
       message.AlternateViews.Add(htmlView); 

       SmtpClient client = new SmtpClient(); 
       client.Send(message); 
      } 

      catch (Exception ex) 
      { 
       throw new Exception(); 
      } 
      return null; 
     } 
    } 
+0

문제는 무엇을 던져 된 원래 excepion와 함께 제공된 모든 데이터가 손실? 아무것도 일어나지 않는다, 오류 메시지? – Benjol

답변

3

, 일반적인 예외 객체를 잡기하고 새가 SendEmail에 의해 던져진 예외를 먹는 효과를 갖게하는 것입니다 던지고.

나머지는 괜찮아 보입니다. 당신은 변경해야

+0

많은 분들께 감사 드리고 싶습니다 - 제멋대로하는 것을 놓치지 않았 음을 확인하고 싶습니다. 감사합니다. –

0

catch (Exception ex) { throw new Exception(); } 에 :

catch (Exception ex) 
      { 
       throw; 
      } 

때문에 그렇지 않으면 당신은

+1

... 또는 catch 블록을 제거하십시오. – Foole