想要发送来自asp.net网站的自动邮件

时间:2021-02-25 18:16:42

I am trying to send an automatic mail from my asp.net website whenever a person will click submit button to enter some value. I have created some codes in C#. It's not showing any error, but also not sending any mail. I am using ip address of the mail server of the organization in the network credential. I am not sure how to provide password for the email address I am using to send mail. I am sharing my code with you.

每当有人点击提交按钮输入某个值时,我就会尝试从我的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");
    }
}

2 个解决方案

#1


2  

You probably want to stop using default credentials

您可能希望停止使用默认凭据

emailClient.UseDefaultCredentials = false;

and add

并添加

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

to use your own credentials as most SMTP servers now block anonymous relay in order to stop SPAM.

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

#2


1  

Please put the configuration in your application’s web.config file. Here is an example of how to configure it:

请将配置放在应用程序的web中。配置文件。这里有一个如何配置它的例子:

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

I have already answered a similar query at Unable to send email from asp.net form

我已经回答过类似的查询,无法从asp.net表单发送电子邮件

#1


2  

You probably want to stop using default credentials

您可能希望停止使用默认凭据

emailClient.UseDefaultCredentials = false;

and add

并添加

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

to use your own credentials as most SMTP servers now block anonymous relay in order to stop SPAM.

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

#2


1  

Please put the configuration in your application’s web.config file. Here is an example of how to configure it:

请将配置放在应用程序的web中。配置文件。这里有一个如何配置它的例子:

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

I have already answered a similar query at Unable to send email from asp.net form

我已经回答过类似的查询,无法从asp.net表单发送电子邮件