【问题标题】:sending mail in asp.net-also using web config在 asp.net 中发送邮件-也使用 web config
【发布时间】:2014-02-07 03:49:24
【问题描述】:

我正在尝试创建一个联系我们页面,用户单击提交并向我发送电子邮件,我查看了一些示例,但他们似乎将他们的电子邮件凭据硬编码到代码中,我发现安全 m 你可以将用户名和密码存储在 webconfig 文件中,下面是我的 web 配置代码和我的默认 aspx.cs 代码,谁能帮我解决问题,这是我得到的错误

无法解析远程名称:'smtp.gmail.com,587' 第 45 行:mailClient.Send(message);

这是我的应用设置和代码:

        <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();

    }
}

}

【问题讨论】:

标签: c# asp.net email


【解决方案1】:

您需要像这样在 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 使用的端口。所以把这个值放在主机属性中。

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 您不需要在代码上发送这些值。正如您已经在 web.config 的 SMTP 部分中给出的那样。只需尝试运行上面的代码。
  • @user3141831 您无需更改代码中的任何内容即可引用 web.config。只需查看我的 c# 代码即可发送邮件。
  • 我收到此错误 SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是: 5.7.0 必须首先发出 STARTTLS 命令。 j9sm10355414wjz.13 - gsmtp
  • 再次运行并得到这个远程名称无法解析:'smtp.gmail.com'
【解决方案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);

用于发送。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      你为什么不编译一个类来制作一个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;
      }
      

      【讨论】:

        【解决方案5】:
        <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的详细信息

        【讨论】:

          猜你喜欢
          • 2011-06-26
          • 1970-01-01
          • 2011-09-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-18
          • 1970-01-01
          相关资源
          最近更新 更多