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 made because the target machine actively refused it 74.125.141.109:587 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)
答案 0 :(得分:0)
尝试添加这两个设置
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
还要检查服务器上是否有阻止发送电子邮件请求的防病毒软件
答案 1 :(得分:0)
你可以这样使用它在下面
public static void SendMail(string Message, string Subject)
{
bool retVal;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddres = new MailAddress(FromMail, "Test");
message.From = fromAddres;
// To address collection of MailAddress
message.To.Add(ToMail);
message.Subject = Subject;
smtpClient.Host = HotName;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential(SmtpUser, SmtpPassword);
message.IsBodyHtml = true;
// Message body content
message.Body = Message;
smtpClient.Send(message);
retVal = true;
message.Dispose();
}
我认为这会对你有帮助........
答案 2 :(得分:0)
很抱歉发布它作为答案,我打算回复原帖。
SMTP将需要凭据,我没有找到提供的凭据。
尝试telnet
连接到服务器。如果端口可访问且您拥有SMTP凭据,则可以从命令行本身发送电子邮件。
试试这个,让我知道这是否适合你。