2014-08-28 4 views
0

다음 코드는 고객에게 전자 메일을 보낼 수없고 예외를 throw하지 않습니다. 코드는 이메일이나 예외를 보내지 않지만 실행되었습니다. 나는 asp.net에 관해 완전히 새로운 것입니다. 어떤 사람들은 문제를 해결하는 방법을 도울 수 있습니다.asp.net에서 mailmessage를 보낼 수 없습니다.

코드 :

try 
{ 
    String userName = "ramesh"; 
    String passWord = "123456"; 
    String sendr = "[email protected]"; 
    String recer = "[email protected]"; 
    String subject = "Comformation "; 
    String body = "Dear Customer"; 

    MailMessage msgMail = new MailMessage(sendr, recer, subject, body); 

    int PortNumber = 25; 
    SmtpClient smtp = new SmtpClient("smtp.test.com", PortNumber); 
    msgMail.IsBodyHtml = true;          
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
    smtp.Credentials = new System.Net.NetworkCredential(userName, passWord); 

    smtp.Send(msgMail); 

    MsgLP.Text = "Emailed to Customer.."; 
    LogInLink.Visible = true; 
} 
catch (Exception ex){ 
    AuditLog.LogError("ErrorE-mail " + ex.Message); 
} 
+0

스팸을 보았습니까? 메일 서버에 보안 설정이 있습니까? – Mairaj

+0

메일 서버의 스팸 보안 설정을 확인하는 방법을 알려주십시오. – Prathyush

+0

이메일을 보내지 않으면 어떻게 알 수 있습니까? 나는 당신의 코드가 잘 작동한다고 제안하지만, SMTP 서버는 이메일을 전달하지 않는다. –

답변

1

당신은 smtp.EnableSsl=true을 설정하고 포트 번호 587를 사용해야합니다. 최종 코드는 다음과 같습니다.

try 
{ 
String userName = "ramesh"; 
String passWord = "123456"; 
String sendr = "[email protected]"; 
String recer = "[email protected]"; 
String subject = "Comformation "; 
String body = "Dear Customer"; 

MailMessage msgMail = new MailMessage(sendr, recer, subject, body); 

int PortNumber = 587; //change port number to 587 
SmtpClient smtp = new SmtpClient("smtp.gmail.com", PortNumber); //change from test to gmail 
smtp.EnableSsl = true; //set EnableSsl to true 
msgMail.IsBodyHtml = true;          
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
smtp.Credentials = new System.Net.NetworkCredential(userName, passWord); 
smtp.Send(msgMail); 
MsgLP.Text = "Emailed to Customer.."; 
LogInLink.Visible = true; 
} 
catch (Exception ex){ 
AuditLog.LogError("ErrorE-mail " + ex.Message); 
} 

이 코드를 내 자격 증명으로 테스트했는데 정상적으로 작동합니다.

+0

죄송합니다. 메모장의 코드를 편집했는데 쉼표로 표시 될 수 있으며 "smtp.EnableSsl = true;"라는 의미는 무엇입니까? 너 나 좀 도와 줄래? – Prathyush

+0

SSL (** Secure Sockets Layer **)은 웹 서버와 브라우저간에 암호화 된 링크를 설정하기위한 표준 보안 기술입니다. 자세한 내용은 [MSDN] (http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl (v = vs.110) .aspx) –

+0

"웹. config "파일의 네트워크 호스트 포트가 25 일 때, 이제는 게시물 587을 사용할 것인가? – Prathyush

0
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(); 
     mm.From = new MailAddress("[email protected]"); 
     mm.To.Add("[email protected]"); 
     System.Net.Mail.Attachment attachment; 
     string strFileName; 
     strFileName = "Uploadfile/" + "200814062455PM_Admin_Screenshot (10).JPEG"; 
     attachment = new System.Net.Mail.Attachment(Server.MapPath(strFileName)); 
     mm.Attachments.Add(attachment); 
     mm.Body = ("<html><head><body><table><tr><td>Hi</td></tr></table></body></html><br/>"); ; 

     mm.IsBodyHtml = true; 
     mm.Subject = "Candidate " + Name + " for your Requirement " + Jobtt + " "; 
     System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587); 
     client.UseDefaultCredentials = false; 
     client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); 
     client.Port = 587; 
     client.Host = "smtp.gmail.com"; 
     client.EnableSsl = true; 
     object userstate = mm; 
     client.Send(mm); 
관련 문제