【问题标题】:Want to send automatic mail from asp.net website想从asp.net网站发送自动邮件
【发布时间】:2012-07-04 08:09:30
【问题描述】:

每当有人单击提交按钮输入一些值时,我都会尝试从我的 asp.net 网站发送一封自动邮件。我在 C# 中创建了一些代码。它没有显示任何错误,也没有发送任何邮件。我在网络凭据中使用组织的邮件服务器的 IP 地址。我不知道如何为我用来发送邮件的电子邮件地址提供密码。我正在与您分享我的代码。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;
using System.ComponentModel;// for backgroundworker class
using System.Net;
using System.Net.Mail;
using System.Threading;

public partial class Contact : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void LoginView1_ViewChanged(object sender, EventArgs e)
    {

    }

    public void Button3_Click(object sender, EventArgs e)
    {
        using (SqlConnection connn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {

            SqlCommand command = new SqlCommand();
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "dbo.Procedure";

            command.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value = TextBox1.Text;
            command.Parameters.Add("email", System.Data.SqlDbType.VarChar).Value = email.Text;
            command.Parameters.Add("sub", System.Data.SqlDbType.VarChar).Value = sub.Text;
            command.Parameters.Add("message", System.Data.SqlDbType.VarChar).Value = message.Text;

            string from = "info@xxx.com";
            string to = "abc@xxx.com";
            string mailSubject = sub.Text.ToString();
            string mailBody = message.Text.ToString(); 
            MailMessage mess = new MailMessage(from, to, mailSubject, mailBody);
            mess.IsBodyHtml = true;
            SmtpClient emailClient = new SmtpClient("//xxx.xxx.x.x/", 25); //Server ip & port
            emailClient.UseDefaultCredentials = true;
            //System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("", "");
            //emailClient.Credentials = SMTPUserInfo;
            //emailClient.Send(mess);

           // emailClient.Credentials = CredentialCache.DefaultNetworkCredentials;

            try
            {
                emailClient.Send(mess);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage1(): {0}",
                      ex.ToString());
            }

            command.Connection = connn;
            connn.Open();
            command.ExecuteNonQuery();
            connn.Close();
        }
        TextBox1.Text = "";
        email.Text = "";
        sub.Text = "";
        message.Text = "";
        lblmsg.Text = "Data entered successfully!!! Thank You for contacting us! We will get back to you as soon as possible.";
        //Response.Write("Submitted Succesfully");
        Response.Redirect("~/XX.aspx");
    }
}

【问题讨论】:

标签: c# asp.net


【解决方案1】:

您可能想停止使用默认凭据

emailClient.UseDefaultCredentials = false;

并添加

emailClient.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");

使用您自己的凭据,因为大多数 SMTP 服务器现在会阻止匿名中继以阻止垃圾邮件。

【讨论】:

  • 我会特别犹豫是否将我的密码放入代码中。我同意不使用 DefaultCredentials,但会指定 emailClient.Credentials=System.Net.CredentialCache.DefaultNetworkCredentials,然后确保将应用程序池的身份配置为可以针对 SMTP 服务器进行身份验证的用户。
  • @DuncanHowe 这是一个很好的观点,或者如果您无法将应用程序池详细信息设置为匹配(即它在共享平台上),我建议将其放入 web.config 然后加密 web.config visualstudiomagazine.com/articles/2009/06/11/…
  • 我不想在代码中输入密码。您能否建议我如何在 web.config 中执行此操作?我在那里提到了我的网络主机。我必须在那里提供我的网络用户名和密码,对吗?我应该把我的电子邮件密码放在哪里?我是新手,所以可能会问一些愚蠢的问题。谢谢大家。
  • 谢谢。我按照你说的试过了。但仍然没有成功。我不需要为我尝试发送邮件的特定电子邮件地址输入用户名和密码吗?
  • @MSI 是 - 将您的用户名和密码替换为您的特定 SMTP 登录凭据。
【解决方案2】:

请将配置放入应用程序的 web.config 文件中。以下是如何配置它的示例:

  <system.net>
    <mailSettings>
      <smtp from="test@test.com">
        <network host="smtpserver1" port="25" userName="StackOverFlow" password="HatSoft" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

我已经在Unable to send email from asp.net form回答了类似的问题

【讨论】:

  • 除了服务器名之外,我把服务器的ip地址放在网络主机中可以吗?
  • 是的,你可以,我看不出有任何失败的原因
  • 这里的用户名和密码,我应该输入服务器用户名和密码还是我的电子邮件地址用户名和密码。对这些东西真的很陌生,感谢您的帮助。
  • 用户名和密码必须是用于验证 SMTP 服务器并由管理员提供给您的用户名和密码
  • email 可以去这里 并且用户名和密码可以去 userName="StackOverFlow" password="HatSoft" 请试试这个,让我知道结果
猜你喜欢
  • 1970-01-01
  • 2018-12-14
  • 2011-03-27
  • 2011-12-22
  • 2021-12-27
  • 2014-07-24
  • 2012-07-09
  • 2013-06-20
  • 1970-01-01
相关资源
最近更新 更多