当我尝试在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.
为什么总是超时?我确信我有正确的smtp服务器地址和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
谢谢, VP
答案 0 :(得分:5)
它是您的端口...... Gmail需要端口587。
如果这不起作用,我可以给你我的电子邮件类。
修改强>
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;
答案 1 :(得分:2)
由于您正在引用System.Web,我认为这是一个Web应用程序,尽管我从您的描述中并不十分清楚。如果这实际上是一个Web应用程序,请确保您的web.config文件具有以下条目:
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" port="465" userName="<UID>" password="<PW>"/>
</smtp>
</mailSettings>
</system.net>
这可以防止您必须在email_log_files类
中指定此信息答案 2 :(得分:1)
不确定这是否有用,但您是否检查过您的IP没有阻止从您的IP发送电子邮件?我曾经遇到过类似的问题,我整个周末都没有把头发拉出来。
所以,为了消除这一点,值得进行检查。 祝你好运。
答案 3 :(得分:0)
可能是防火墙或其他东西。我发现尝试它的最佳方法是手动连接到服务器。
只需打开命令提示符并写入:
telnet your.mail.server 25
这通常会给你一个类似于“220 your.mail.server ESMTP”的回复。
如果你使用Vista或Win7,你可能必须先启用内置的Telnet客户端(转到“程序和功能”并选择“打开或关闭Windows功能”,然后选择Telnet客户端)
答案 4 :(得分:0)
尝试将完整的电子邮件地址放入NetworkCredential用户名