2013-02-04 36 views
2

我找到了關於這個問題的帖子數量在stackoverflow,但仍然我的問題沒有解決。我試着用下面的代碼來發送郵件:在asp.net mvc4發送郵件失敗smtp.Send(郵件)

在控制器:

try 
     { 
     MailMessage mail = new MailMessage(); 
     mail.To.Add("[email protected]"); 
     mail.From = new MailAddress("[email protected]"); 
     mail.Subject = "Feedback for Website"; 
     string Body = "Name:"; 
     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]", "topassword"); 
     smtp.Send(mail); 

    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 

    } 

的web.config:

<system.net> 
<mailSettings> 
    <smtp deliveryMethod="PickupDirectoryFromIis"> 
    <network defaultCredentials="true" host="localhost" port="25"/> 
    </smtp> 
</mailSettings> 

我想這也:

<smtp from="[email protected]"> 
<network enableSsl="true" host="smtp.gmail.com" port="587" userName="[email protected]" password="mypassword" /> 

調試時在我的輸出:

Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.MailMessage' 
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.To.get' 
Step into: Stepping over non-user code 'System.Net.Mail.MailAddress.MailAddress' 
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.Subject.set' 
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.Body.set' 
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.IsBodyHtml.set' 
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.SmtpClient' 
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.Host.set' 
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.EnableSsl.set' 
The thread '<Thread Ended>' (0x1f38) has exited with code 0 (0x0). 
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll' 
Step into: Stepping over non-user code 'System.Net.NetworkCredential.NetworkCredential' 
The thread '<No Name>' (0x23d8) has exited with code 0 (0x0). 
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.Send' 
A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll 
The thread '<Thread Ended>' (0x1474) has exited with code 0 (0x0). 

如何解決這個問題。請幫助我..

回答

2

最後長的搜索後,我解決了這個問題:

Host = "172.**.*.***", 
Port = 25, 
EnableSsl = false, 

Changed host from smtp.gmail.com to "172.**.*.***"(my mail server IP) 
port changed from 587 to 25 
and set false to EnableSsl 

這是工作的地方。 。通過使用本地服務器主機。

1

嘗試下一個設置。

啓用SSL代碼:

smtp.EnableSsl = true; 

設置配置如下:

<mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
     <network defaultCredentials="false" host="smtp.gmail.com" port="587" userName="[email protected]" password="mypassword" /> 
     </smtp> 
</mailSettings> 
+0

謝謝迪馬的回覆..我嘗試了你的建議..但是它仍然是一樣的.. – Lokesh

+0

爲什麼你從一開始就沒有說過要使用個人smtp服務器? :) – Dima