我正在尝试创建一个“联系我们”页面,用户单击提交后会向我发送电子邮件。我查看了一些示例,但它们似乎在将其电子邮件凭据硬编码到代码中,我发现为安全起见可以将用户名和密码存储在webconfig文件中。下面是我的webconfig代码和默认的aspx.cs代码,请问有谁能帮我解决这个问题?以下是我收到的错误信息:
“远程名称无法解析:'smtp.gmail.com,587' 第45行:mailClient.Send(message);”
以下是我的appsettings和代码:
“远程名称无法解析:'smtp.gmail.com,587' 第45行:mailClient.Send(message);”
以下是我的appsettings和代码:
<appSettings>
<add key="PFUserName" value="myemail@gmail.com"/>
<add key="PFPassWord" value="mypassword"/>
<add key="MailServerName" value="smtp.gmail.com,587"/>
</appSettings>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Web.Configuration;
using System.Net;
namespace WebApplication2
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
SendMail(txtEmail.Text, txtComments.Text);
}
private void SendMail(string from, string body)
{
string Username = WebConfigurationManager.AppSettings["PFUserName"].ToString();
string Password = WebConfigurationManager.AppSettings["PFPassWord"].ToString();
string MailServer = WebConfigurationManager.AppSettings["MailServerName"].ToString();
NetworkCredential cred = new NetworkCredential(Username, Password);
string mailServerName = ("smtp.gmail.com,587");
MailMessage message = new MailMessage(from, Username, "feedback", body);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com,587");
mailClient.EnableSsl = true;
mailClient.Host = mailServerName;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = cred;
mailClient.Send(message);
message.Dispose();
}
}
}