2011-09-09 2 views
0

여기에 내가 코드를 실행 해요 것 :System.Net.Mail을 사용하여 전자 메일을 보내는 동안 오류가 발생했습니다. 디버깅 할 때 무엇을 찾아야할까요?

try 
{ 
    var fromAddress = new MailAddress("[email protected]", "foo"); 
    var toAddress = new MailAddress("[email protected]", String.Empty); 
    const string fromPassword = "password"; 
    const string subject = "Welcome!"; 
    const string body = "This is a test message!"; 

    var smtp = new SmtpClient 
    { 
     Host = "mail.foo.com", 
     Port = 143, 
     EnableSsl = false, 
     DeliveryMethod = SmtpDeliveryMethod.Network, 
     UseDefaultCredentials = false, 
     Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 
    }; 
    using (var message = new MailMessage(fromAddress, toAddress) 
             { 
              Subject = subject, 
              Body = body 
             }) 
    { 
     smtp.Send(message); 
    } 
} 
catch (Exception e) 
{ 
    Debug.WriteLine(e.InnerException.ToString()); 
    //Add logging here. 
} 

나는 다음과 같은 오류가 해고 smtp.Send(message)를 실행하려고 :

A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll 
    System.FormatException: Smtp server returned an invalid response. 
    at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) 
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) 
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) 
    at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) 
    at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) 

무엇입니까 아마이 오류의 원인은? 나는 무엇을 찾고 있어야 하는가?

답변

2

SMTP 포트가 143입니까? 기본값은 25입니다. 오류가 GetConnection에 있기 때문에 먼저 서버와 올바르게 대화 할 수없는 것 같습니다.

+0

안녕하세요. 그것이 열쇠였습니다. 그러나 더 중요한 것은 오류가 스택에 정확히 어디에 있는지 알려주는 것입니다. +1하고 정확한 점수를줍니다. –

0

SMTP 스크립트에 문제가 없지만 위에서 언급 한 메시지 메시지가 계속 표시되면 Gmail에서 처음으로 Gmail 계정에 로그인 한 것을 감지하여 Gmail이 Google 서버의 인증을 차단했기 때문입니다 다른 국가 또는 위치. 인증을 승인하려면 Gmail 보안 센터에 로그인해야합니다. 승인을 받으면

스크립트를 다시 보내고 몇 분 후에 다시 이메일을 보내주십시오. 다음은 Gmail 보안 센터에서 '비정상적인 활동 알림'을 승인하는 단계입니다.

A) https://accounts.google.com/ServiceLogin?elo=1

B "를 Gmail은 보안"에 대한 링크 타격 또는 Google 검색을 통해 보안 센터를 Gmail 및 Gmail 계정과 로그인으로 이동) 다음의 "보안"/ "최근 활동"을 클릭합니다 "모든 이벤트보기"

c) "비정상적인 활동"을 볼 수 있으며 모든 비정상적인 활동 이벤트를 표시하고 관련 이벤트를 선택하고 "예, 저였습니다!"를 클릭하여 승인합니다.

+0

내 대답에 -1을 준 사람은 왜 나를 위해 빼기를했는지 설명 할 수 있습니까?이 잘못된 대답입니까? – Ananth

0
try 
{ 
    MailMessage mail = new MailMessage(); 
    mail.To.Add("[email protected]"); 
    mail.CC.Add("[email protected]"); 
    mail.From = new MailAddress("[email protected]"); 
    mail.Subject = "Feedback for Website"; 
    string Body = "Name:"+TextBoxName.Text+" Phone Number: "+TextPhone.Text; 
    mail.Body = Body; 
    mail.IsBodyHtml = true; 
    SmtpClient smtp = new SmtpClient(); 
    smtp.Host = "smtp.gmail.com"; 
    smtp.EnableSsl = true; 
    smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "pass"); 
    smtp.Send(mail); 
    lblStatus.Text = "Message send successfully."; 
    txtMessage.Text = ""; 
} 
catch (Exception ex) 
{ 
    lblStatus.Text = ex.ToString(); 
} 
관련 문제