在ASP.NET中发送邮件-同时使用Web配置文件

4
我正在尝试创建一个“联系我们”页面,用户单击提交后会向我发送电子邮件。我查看了一些示例,但它们似乎在将其电子邮件凭据硬编码到代码中,我发现为安全起见可以将用户名和密码存储在webconfig文件中。下面是我的webconfig代码和默认的aspx.cs代码,请问有谁能帮我解决这个问题?以下是我收到的错误信息:
“远程名称无法解析:'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();

    }
}

}


可能是 http://stackoverflow.com/questions/9313047/error-sending-email-using-asp-net-web-app 的重复问题。 - Silvestre
5个回答

9
您需要在web.config文件中的mailSettings配置中设置SMTP设置,例如:
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="my@mail.com">
        <network host="smtp.gmail.com" port="587" userName="myemail@gmail.com" password="mypassword" />
      </smtp>
    </mailSettings>
  </system.net>

您的服务器名称是smtp.gmail.com(将其中的587删除)。587是smtp使用的端口号。因此,请在host属性中放置此值。

C#代码:

SmtpClient smtpClient = new SmtpClient();
MailMessage mailMessage = new MailMessage();

mailMessage.To.Add(new MailAddress("sender@mail.com"));
mailMessage.Subject = "mailSubject";
mailMessage.Body = "mailBody";

smtpClient.Send(mailMessage);

我该如何更改aspx.cs页面中的代码以引用webconfig? - user3141831
@user3141831,你不需要在代码中发送这些值。因为你已经在web.config的SMTP部分中给出了它们。只需尝试运行上面的代码即可。 - Sachin
@user3141831,你不需要更改代码来引用web.config。只需查看我的C#代码以发送邮件即可。 - Sachin
我收到了这个错误:SMTP服务器需要安全连接或客户端未经身份验证。服务器响应为:5.7.0 必须首先发出STARTTLS命令。j9sm10355414wjz.13 - gsmtp。 - user3141831
再次运行它,得到了这个错误信息:“无法解析远程名称:'smtp.gmail.com'”。 - user3141831

2
这是我目前在Web Config中使用的内容,经过一些明显的编辑。
<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="username@gmail.com">
        <network defaultCredentials="false" host="smtp.gmail.com" port="587" userName=username" password="xxxxxxxxxxxx" />
      </smtp>
    </mailSettings>
  </system.net>

在CS文件中。
using System.Net.Mail;

并且
    MailMessage myMessage = new MailMessage();
    //subject line
    myMessage.Subject = Your Subject;
    //whats going to be in the body
    myMessage.Body = Your Body Info;
    //who the message is from
    myMessage.From = (new MailAddress("Mail@Mail.com"));
    //who the message is to

    myMessage.To.Add(new MailAddress("Mail@Mail.com"));

    //sends the message
    SmtpClient mySmtpClient = new SmtpClient();
    mySmtpClient.Send(myMessage);

用于发送。


0
为什么你不把代码编译成类,生成一个dll文件呢?
好的,我用这段代码,享受吧 :)
MailMessage mail = new MailMessage();
try
{
mail.To.Add(destinatario); // where will send
mail.From = new MailAddress("email that will send", "how the email will be displayed");
mail.Subject = "";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;

mail.Priority = MailPriority.High;

// mail body
mail.Body = "email body";

 // send email, dont change //
SmtpClient client = new SmtpClient();

client.Credentials = new System.Net.NetworkCredential("gmail account", "gmail pass"); // set 1 email of gmail and password
client.Port = 587; // gmail use this port
client.Host = "smtp.gmail.com"; //define the server that will send email
client.EnableSsl = true; //gmail work with Ssl

client.Send(mail);
mail.Dispose();
return true;
}
catch
{
mail.Dispose();
return false;
}

0

您的主机名应该是 "smtp.gmail.com",然后将 mailClient.Port 设置为 587。


0
<configuration>
  <!-- Add the email settings to the <system.net> element -->
  <system.net>
    <mailSettings>
      <smtp>
        <network
             host="relayServerHostname"
             port="portNumber"
             userName="username"
             password="password" />
      </smtp>
    </mailSettings>
  </system.net>

  <system.web>
    ...
  </system.web>
</configuration>

上面是 web.config,这里是后端代码:

using System.Net;
using System.Net.Mail;
using System.Drawing;
using System.Configuration;
using System.Data.SqlClient;
using System.Net.Configuration;

private void Email(string email, string pass, string customername)
{
     SmtpSection smtp = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
     MailMessage mm = new MailMessage(smtp.Network.UserName,email);

     mm.Subject = "Thank you for Registering with us";
     mm.Body = string.Format("Dear {0},<br /><br />Thank you for Registering with <b> Us. </b><br /> Your UserID is <b>{1}</b> and Password is <b> {2} </b> <br /><br /><br /><br /><br /><b>Thanks, <br />The Ismara Team </b>", customername, email, pass);            
     mm.IsBodyHtml = true;

   try
   {            
     using (SmtpClient client = new SmtpClient())
   {
      client.Send(mm);
   }


  }
catch (Exception ex)
  {
     throw new Exception("Something went wrong while sending email !");                
   }
}

系统将自动从web.config获取SMTP的详细信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接