Possible Duplicate:
GMail SMTP via C# .Net errors on all ports
当我尝试在 C# 程序中发送电子邮件时,出现以下错误。我在 Windows 7 上使用 Visual Studio 2008。我会先粘贴我的代码,然后粘贴错误:
class email_log_files
{
private string login_username = "my_gmail_id";
private string login_password = "my_gmail_password";
public void send_email()
{
string src_address = "my_gmail_id@gmail.com";
string dest_address = "my_destination_id@xyz.edu";
try
{
MailMessage email_msg = new MailMessage();
SmtpClient email_client = new SmtpClient();
email_msg.From = new MailAddress(src_address);
email_msg.Sender = new MailAddress(src_address);
email_msg.ReplyTo = new MailAddress(src_address);
email_msg.To.Add(dest_address);
email_msg.Subject = "Test";
email_msg.Body = "Body of the message";
NetworkCredential credentials = new NetworkCredential(login_username, login_password);
email_client.Credentials = credentials;
email_client.Host = "smtp.gmail.com";
email_client.Port = 465;
email_client.EnableSsl = true;
email_client.Send(email_msg);
Console.WriteLine("Message Sent Successfully!!");
Console.ReadLine();
}
}
}
错误信息如下:
The operation has timed out.
为什么总是超时?我确信我有正确的 Gmail 服务器地址和端口号,因为我已经配置了相同的 Outlook。有什么帮助或想法吗?
将端口改为587后,错误如下。我刚刚禁用了防火墙,看看这是否是问题所在,但事实并非如此。新错误(针对端口 587):
这就是我将端口更改为 587 时出现的错误:
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.113.109:587
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
et 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 ow
ner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket
6, 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, GeneralAsyncD
elegate 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)
System
System.Collections.ListDictionaryInternal
谢谢, 副总裁
最佳答案
这是您的端口...Gmail 需要端口 587。
如果这不起作用,我可以为您提供我的电子邮件类(class)。
编辑:
private static void SendEmailMessageGmail(System.Net.Mail.MailMessage message)
{
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
System.Net.NetworkCredential cred = new System.Net.NetworkCredential(EmailUsername, EmailPassword);
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = cred;
smtp.Port = 587;
smtp.Send(message);
}
编辑:将此添加到您的代码中:email_client.UseDefaultCredentials = false;
关于.net - 在 .NET 中通过 GMail 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2640507/