2013-10-01 88 views
0

我想發一封電子郵件,但它顯示了以下錯誤:無法在asp.net應用程序發送

try 
{ 
    string filename = Server.MapPath("~/App_Data/FeedBack.txt"); 
    string mailbody = System.IO.File.ReadAllText(filename); 
    mailbody = mailbody.Replace("##Name##", txtName.Text); 
    mailbody = mailbody.Replace("##Email##",txtEmail.Text); 
    mailbody = mailbody.Replace("##contact##",txtContact.Text); 
    mailbody = mailbody.Replace("##Commnect##",txtSuggestion.Text); 
    MailMessage myMessage = new MailMessage(); 
    myMessage.Subject = "Responce from website"; 
    myMessage.Body = mailbody; 
    myMessage.From = new MailAddress("[email protected]","Amit Kumar"); 
    myMessage.To.Add(new MailAddress("[email protected]", "Amit Verma")); 
    SmtpClient mysmptclient = new SmtpClient("localhost"); 
    mysmptclient.Host = "smtp.gmail.com"; 
    mysmptclient.Port = 587; 
    mysmptclient.Credentials = new System.Net.NetworkCredential("[email protected]", "Myemailpassword"); 

    mysmptclient.Send(myMessage); 
    formatTable.Visible = false; 
    lblMail.Text = "Your Feedback has been sent successfully"; 
    lblMail.ForeColor = Color.Green; 
} 
catch(Exception expt) 
{ 
    lblMail.Text = "unable to sent the feedback .Check your smpt server"+expt.Message; 
    lblMail.ForeColor = Color.Red; 
} 

我的web.config文件看起來是這樣的:

<system.net> 
    <mailSettings> 
    <smtp from="[email protected]"> 
     <network host="smtp.gmail.com" port='587' password="myemailpass" userName="[email protected]"/> 
    </smtp> 
    </mailSettings> 
</system.net> 
+0

你得到什麼錯誤? – SANDEEP

+0

我的例外信息是發送郵件失敗 –

+0

你有沒有調試你的代碼? – SANDEEP

回答

0

據我所知,你通過使用本地主機(你的IIS)作爲SMTP服務器來覆蓋你的web.config設置。 如果你還沒有配置你的IIS作爲SMTP服務器,這自然不會起作用。 但是,如果你想使用本地主機,而不是谷歌的服務器只使用3條線:

SmtpClient MySmtpClient = new SmtpClient(); 
MySmtpClient.UseDefaultCredentials = true; 
MySmtpClient.Send(mailMessage); 

你應該從SmtpClient mysmptclient = new SmtpClient("localhost");去除串localhost,它應該工作,如果所有其他設置都正確。

可以找到更多信息here

相關問題