我想從我的asp.net網站發送自動郵件,只要有人點擊提交按鈕輸入一些值。我用C#創建了一些代碼。它沒有顯示任何錯誤,但也沒有發送任何郵件。我正在使用網絡憑證中組織的郵件服務器的IP地址。我不確定如何爲我用來發送郵件的電子郵件地址提供密碼。我正在與你分享我的代碼。想要從asp.net網站發送自動郵件
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 = "[email protected]";
string to = "[email protected]";
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");
}
}
你可以嘗試一些低級別的調試用telnet來驗證您的SMTP設置:http:// damonparker。 org/blog/2005/09/12/debugging-popimapsmtp-with-telnet /?utm_source = rss&utm_medium = rss&utm_campaign = debugging-popimapsmtp -with-telnet – mellamokb