asp.net中的邮件功能无法正常工作 [英] Mail function in asp.net not working properly

查看:71
本文介绍了asp.net中的邮件功能无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//我的邮件功能始终显示异常消息:发送邮件失败...... //

使用系统;

使用System.Collections .Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Configuration;

使用System.Data.SqlClient;

使用System。数据;

使用System.Net.Mail;

使用System.Net;

命名空间housing1

{

public partial class Ucomplain:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{



SqlConnection conn = new SqlConnection(ConfigurationManager) .ConnectionStrings [HousingConnectionString] .ConnectionString);

SqlDataAdapter da = new SqlDataAdapter(Select Name f rom Standard_Complain,conn);

DataTable dt = new DataTable();

da.Fill(dt);

DropDownListTOC.DataSource = dt;

DropDownListTOC.DataTextField =名称;

DataBind();

}

}



protected void Button1_Click(object sender,EventArgs e)

{

try

{



MailMessage Msg = new MailMessage();

Msg.From = new MailAddress(TextBoxPME.Text);

Msg.To.Add(sunita@indweb.com);

Msg.Body = TextBoxD.Text;

Msg.IsBodyHtml = true;



SmtpClient客户端=新的SmtpClient(smtp.gmail.com,587);

client.Credentials = new NetworkCredential(Te xtBoxPME.Text,nid100);



client.EnableSsl = true;



client.Send (消息);

Response.Write(邮件发送);

}



catch(异常) ex)

{

Response.Write(ex.Message);

}





}



protected void Button2_Click(object sender,EventArgs e)

{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [HousingConnectionString]。ConnectionString);

conn.Open();



String str =从Primary_Member选择电子邮件,其中Flat ='+ TextBoxF.Text.Trim()+';

SqlCommand comm = new SqlCommand(str,conn);

SqlDataReader reader = comm.ExecuteReader();

if(reader.Read())

{

TextBoxPME.Text = reader [Email ] .ToString();

reader.Close();

}

}







}

}

//My mail function is always showing the exception message of: sending mail failure......//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;
using System.Net;
namespace housing1
{
public partial class Ucomplain : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HousingConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("Select Name from Standard_Complain", conn);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownListTOC.DataSource = dt;
DropDownListTOC.DataTextField = "Name";
DataBind();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
try
{

MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(TextBoxPME.Text);
Msg.To.Add("sunita@indweb.com");
Msg.Body = TextBoxD.Text;
Msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = new NetworkCredential(TextBoxPME.Text , "nid100");

client.EnableSsl = true;

client.Send(Msg);
Response.Write("mail send");
}

catch(Exception ex)
{
Response.Write(ex.Message);
}


}

protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HousingConnectionString"].ConnectionString);
conn.Open();

String str = "select Email from Primary_Member where Flat='" + TextBoxF.Text.Trim() + "'";
SqlCommand comm = new SqlCommand(str, conn);
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
TextBoxPME.Text = reader["Email"].ToString();
reader.Close();
}
}



}
}

推荐答案

System.Net .Mail.MailMessage message = new System.Net.Mail.MailMessage();



//从注册表中读取电子邮件

string fromEmail = Registery.fromAddress;



//从registery读取密码

string fromPassword = Registery.password;



//从注册表中读取地址

string toAddress = Registery.ToAddress;



//读取传出服务器名称来自registery

string smtpSer ver = Registery.smtpServer;



//从注册表中读取传出服务器的端口

int port = Convert.ToInt32(Registery.port) ;



//读取来自registery的enableSSL是真还是假

bool enableSSL = Convert.ToBoolean(Registery.enableSSL);



message.From = new MailAddress(fromEmail.ToString());

message.To.Add(toAddress.ToString());

message.Subject = ServiceName ++ status.ToString();

message.Body =服务名称:+ ServiceName + Environment.NewLine +状态: + status.ToString();

message.IsBodyHtml = true;

message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;



/// smtp在注册表配置的服务器和端口

SmtpClient smtpClient =新的SmtpClient(smtpServer,端口);



///启用安全连接需要ssl。对于gmail服务器必须为true,对于其他服务器必须为false服务器。

smtpClient.EnableSsl = enableSSL;



smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

smtpClient。 UseDefaultCredentials = false;

smtpClient.Credentials = new NetworkCredential(fromEmail,fromPassword);

smtpClient.Send(message);

}



catch(例外情况)

{



Logger.Log(错误在发送邮件。 + ex.Message +,+ ex.InnerException,3);

}
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

//Read fromEmail from registery
string fromEmail = Registery.fromAddress;

//Read fromPassword from registery
string fromPassword = Registery.password;

//Read toAddress from registery
string toAddress = Registery.ToAddress;

//Read outgoing server name from registery
string smtpServer = Registery.smtpServer;

//Read port of outgoing server from registery
int port = Convert.ToInt32(Registery.port);

//Read whether enableSSL is true or false from registery
bool enableSSL = Convert.ToBoolean(Registery.enableSSL);

message.From = new MailAddress(fromEmail.ToString());
message.To.Add(toAddress.ToString());
message.Subject = ServiceName + " " + status.ToString();
message.Body = "Service Name :" + ServiceName + Environment.NewLine + "Status :" + status.ToString();
message.IsBodyHtml = true;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

///smtp server and port configured at registry
SmtpClient smtpClient = new SmtpClient(smtpServer, port);

///enable ssl is required for secure connection.It is must be true for gmail server and false for other servers.
smtpClient.EnableSsl = enableSSL;

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail,fromPassword);
smtpClient.Send(message);
}

catch (Exception ex)
{

Logger.Log("Error In Sending Mail. " + ex.Message + " , " + ex.InnerException, 3);
}


这篇关于asp.net中的邮件功能无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆