2014-08-28 61 views
0

以下代碼無法發送電子郵件給客戶,並且不會引發任何異常。它的代碼不發送任何電子郵件或例外,但執行。我對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 2014-08-28 07:41:07

+0

您能否告訴我如何檢查郵件服務器上的垃圾郵件安全設置? – Prathyush 2014-08-28 07:44:03

+0

你怎麼知道它不發送電子郵件,如果它不投擲?我建議你的代碼工作正常,但一路上的smtp服務器沒有發送電子郵件。 – 2014-08-28 07:44:09

回答

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 2014-08-28 08:06:55

+0

SSL(**安全套接字層**)是用於在Web服務器和瀏覽器之間建立加密鏈接的標準安全技術。有關更多詳細信息,請參閱[MSDN](http://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient.enablessl(v = vs.110).aspx) – 2014-08-28 08:12:52

+0

「Web中的一個說明。配置「文件的網絡主機端口是25,現在我將使用郵政587它將工作與否? – Prathyush 2014-08-28 08:42:16

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); 
相關問題