string from = "myemail@gmail.com";
string to = "other.mail.123@gmail.com";
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new System.Net.Mail.MailAddress(from, "One Ghost", System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = System.Net.Mail.MailPriority.High;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Credentials = new System.Net.NetworkCredential(from, "XXXXX");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
HttpContext.Current.Response.Write("errroorr " + ex.Message.ToString());
}
它在 client.send() 方法中抛出异常。它说消息发送失败。可能是什么问题 内部异常是这个
内部异常显示 System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be because the target machine actively refused it 74.125.141.109: 587 在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress 套接字地址)套接字和套接字、IPAddress 和地址、ConnectSocketState 状态、IAsyncResult asyncResult、Int32 超时、Exception 和异常)---内部异常堆栈跟踪结束---在 System.Net.ServicePoint.GetConnection(PooledStream PooledStream、对象所有者、 bool 异步、IPAddress 和地址, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) 在 System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) 在 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) 在 System.Net System.Net.Mail.SmtpConnection.GetConnection(字符串主机,Int32 端口)在 System.Net.Mail.SmtpTransport.GetConnection(字符串主机,Int32 端口)在系统.Net.Mail.SmtpClient.GetConnection() 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)
请您参考如下方法:
尝试添加这两个设置
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
还要检查您的服务器上是否有阻止发送电子邮件请求的防病毒软件
