如何使用c#在asp.net中使用gmail帐户向任何电子邮件地址发送电子邮件 [英] How to send email in asp.net using c# to any email address using gmail account

查看:81
本文介绍了如何使用c#在asp.net中使用gmail帐户向任何电子邮件地址发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#通过ASP.NET中的应用程序发送电子邮件。

我搜索了很多,并且大多数发现以下代码发送电子邮件:

< pre lang =c#> MailMessage objEmail = new MailMessage();

objEmail.From = new MailAddress(txtFrom.Text);
objEmail.To.Add(txtTo.Text);
objEmail.CC.Add(txtCC.Text);
objEmail.Bcc.Add(txtBCC.Text);
objEmail.Subject = txtSubject.Text;

尝试
{

SmtpClient mail = new SmtpClient();

mail.EnableSsl = true ;
mail.DeliveryMethod = SmtpDeliveryMethod.Network;
mail.Credentials = new NetworkCredential(txtFrom.Text,txtPassword.Text);
mail.Timeout = 20000 ;

mail.UseDefaultCredentials = false ;

mail.Host = smtp.gmail.com;
mail.Port = 587 ;

mail.Send(objEmail);

Response.Write( 您的电子邮件已成功发送 - 谢谢);

}
catch (Exception exc)
{
Response.Write( 发送失败原因:< br /> + exc.ToString());
}



但我经常收到以下错误:

 System.Net.Mail.SmtpException:发送邮件失败.--> System.IO.IOException:无法从传输连接读取数据:远程主机强行关闭现有连接.-->系统。 Net.Sockets.SocketException:System.Net.Sockets.NetworkStream上的System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32大小,SocketFlags socketFlags)上的远程主机强制关闭现有连接。读取(Byte []缓冲区,Int32偏移量,Int32大小)---内部异常堆栈跟踪的结束---在System的System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)处。 System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader调用者)的System.Net.BufferedReadStream.Read(Byte []缓冲区,Int32偏移量,Int32计数)中的Net.DelegatedStream.Read(Byte []缓冲区,Int32偏移量,Int32计数) ,System.Net.Mail.SmtpRe中的布尔值oneLine System.Net.Mail.CheckCommand.Send中的plyReaderFactory.ReadLine(SmtpReplyReader调用者)(SmtpConnection conn,String& System.Net.Mail上的System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)处的System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn)处的System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) System.Net.Mail.SmtpClient.Send上的.SmtpClient.GetConnection()(MailMessage消息)





请帮帮我,我是卡在这里。

不知道该怎么办。



提前致谢:)

解决方案

目前还不清楚你是想通过gmail发送还是想要使用另一个smtp服务器。



要发送到任何服务器你必须匹配服务器期望的协议。如果你不匹配它然后它将失败。



如果你想使用gmail然后谷歌搜索建议您的代码不正确(不完整。)


检查一下。很久以前它对我很有用。我的应用程序仍在使用相同的代码:)




http://www.codeshode.com/2010/06/send-email-using-gmail-in-aspnet.html [ ^ ]





如果这篇文章对你有帮助,请标记为答案


检查一次

http://csharpdotnetfreak.blogspot.com/2009/08 /send-email-using-gmail-in-aspnet.html [ ^ ]


I am trying to send email through an application in ASP.NET using C#.
I searched a lot and mostly found the following code to send email:

MailMessage objEmail = new MailMessage();

objEmail.From = new MailAddress(txtFrom.Text);
objEmail.To.Add(txtTo.Text);
objEmail.CC.Add(txtCC.Text);
objEmail.Bcc.Add(txtBCC.Text);
objEmail.Subject = txtSubject.Text;

try
{

    SmtpClient mail = new SmtpClient();

    mail.EnableSsl = true;
    mail.DeliveryMethod = SmtpDeliveryMethod.Network;
    mail.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text);
    mail.Timeout = 20000;

    mail.UseDefaultCredentials = false;

    mail.Host = "smtp.gmail.com";
    mail.Port = 587;

    mail.Send(objEmail);

    Response.Write("Your Email has been sent sucessfully - Thank You");

}
catch (Exception exc)
{
    Response.Write("Send failure due to : <br />" + exc.ToString());
}


But constantly I am receiving the following error:

"System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)"



Please help me, I am stuck here.
Don''t know what to do further.

Thanks in advance :)

解决方案

It isn''t clear if you want to send via gmail or you want to use another smtp server.

To send to any server you must match the protocol expected by the server. If you don''t match it then it will fail.

If you want to use gmail then googling suggests that your code is incorrect (incomplete.)


Check this one. It worked for me long time ago. My application still working with same code :)


http://www.codeshode.com/2010/06/send-email-using-gmail-in-aspnet.html[^]


Please Mark as Answer if this post helps you!


check this once
http://csharpdotnetfreak.blogspot.com/2009/08/send-email-using-gmail-in-aspnet.html[^]


这篇关于如何使用c#在asp.net中使用gmail帐户向任何电子邮件地址发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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