如何使用SMTP使用ASP.net发送邮件 [英] how to send a mail using ASP.net using SMTP
本文介绍了如何使用SMTP使用ASP.net发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试使用带有gmail的SMTP发送邮件时遇到此错误.
无法发送电子邮件System.Net.Mail.SmtpException:发送邮件失败. ---> System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:无法建立连接,因为目标计算机在System.Net.Sockets.Socket的System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)处主动拒绝了它74.125.127.108:25 System.Net.ServicePoint.ConnectSocketInternal上的.InternalConnect(Endpoint remoteEP)(布尔connectFailure,Socket s4,Socket s6,Socket&套接字,IPAddress&地址,ConnectSocketState状态,IAsyncResult asyncResult,Int32超时,Exception&异常)-结束内部异常堆栈跟踪---在System.Net.PooledStream.Activate(Object System.Net.PooledStream.Activate处的owningObject,布尔异步,Int32超时,GeneralAsyncDelegate asyncCallback)(System.Net.ConnectionPool.GetCo处的ObjectowningObject,GeneralAsyncDelegate asyncCallback) System.Net.Mail.SmtpConnection.GetConnection(字符串主机,Int32端口)的System.Net.Mail.SmtpTransport.GetConnection(字符串主机,Int32端口)的nnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout) System.Net.Mail.SmtpClient.Send(MailMessage消息)的Mail.SmtpClient.GetConnection()---内部异常堆栈跟踪的末尾--- WebApplication3的System.Net.Mail.SmtpClient.Send(MailMessage消息) C:\ Documents and Settings \ Noah Soft \ My Documents \ Visual Studio 2008 \ Projects \ WebApplication3 \ WebApplication3 \ WebForm1.aspx.cs:line 42中的WebForm1.Page_Load(对象发送者,EventArgs e):第42
CAN ANY1可以帮忙吗
解决方案
阅读此线程,
http://stackoverflow.com/questions/6611324/send-mail-using-gmail-smtp [^ ]
并参考这篇文章
http://www. shabdar.org/asp-net/111-send-email-from-your-gmail-account-using-aspnet-and-c.html [
Im getting this error on trying to send mail using SMTP with gmail.
The email cannot be sent System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 74.125.127.108:25 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at WebApplication3.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\Noah Soft\My Documents\Visual Studio 2008\Projects\WebApplication3\WebApplication3\WebForm1.aspx.cs:line 42
CAN ANY1 HELP THIS
解决方案
Read this thread,
http://stackoverflow.com/questions/6611324/send-mail-using-gmail-smtp[^]
and refer this article
http://www.shabdar.org/asp-net/111-send-email-from-your-gmail-account-using-aspnet-and-c.html[^]
SmtpClient client = new SmtpClient(); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = true; client.Host = "smtp.gmail.com"; client.Port = 587; // setup Smtp authentication System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("gmailusername@gmail.com", "gmailpassword"); client.UseDefaultCredentials = false; client.Credentials = credentials; MailMessage msg = new MailMessage(); msg.From = new MailAddress("gmailusername@gmail.com"); msg.To.Add(new MailAddress("xyz@gmail.com")); msg.Subject = "This is a test Email subject"; msg.IsBodyHtml = true; msg.Body = string.Format("<html><head></head><body>Test HTML Email</body>"); try { client.Send(msg); //lblMsg.Text = "Your message has been successfully sent."; } catch (Exception ex) { //lblMsg.ForeColor = Color.Red; //lblMsg.Text = "Error occured while sending your message." + ex.Message; }
这篇关于如何使用SMTP使用ASP.net发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文