2013-10-01 44 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

相关问题