使用gmail发送电子邮件:操作已超时

时间:2014-06-16 09:52:41

标签: asp.net .net email smtp

我正在尝试配置我的控制台应用程序以向gmail中继服务器发送电子邮件。

我正在使用的代码是下一个:

class Program
    {
        static void Main(string[] args) {

            string header;
            string body;
            string emailTo;
            string emailFrom = "anton.selin@inbox.com";

            Console.WriteLine("Enter the header : ");
            header = Console.ReadLine();
            Console.WriteLine("Enter the message body : ");
            body = Console.ReadLine();
            Console.WriteLine("Enter your email : ");
            emailTo = Console.ReadLine();

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 465);
            smtpClient.Credentials = new System.Net.NetworkCredential("anton.selin1@gmail.com", "**********");
            smtpClient.UseDefaultCredentials = true;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = true;
            MailMessage mail = new MailMessage();

            mail.From = new MailAddress(emailFrom,"anton selin");
            mail.To.Add(new MailAddress(emailTo));
            mail.Body = body;
            mail.Subject = header;

            Console.WriteLine("Sending email...");
            smtpClient.Send(mail);

            Console.WriteLine("Email sent...");
            Console.ReadKey();

        }
    }

当我运行代码时,它会给我一个超时错误:

{"The operation has timed out."}

如何配置我的应用程序以便能够从控制台应用程序或Web应用程序(从localhost)发送电子邮件?

1 个答案:

答案 0 :(得分:2)

检查这个,我认为这是你的问题

string emailFrom = "anton.selin@inbox.com";
smtpClient.Credentials = new System.Net.NetworkCredential("anton.selin1@gmail.com", "**********");
mail.From = new MailAddress(emailFrom,"anton selin");

您使用gmail发送邮件,而您应使用anton.selin@inbox.com凭据发送邮件,而不是Gmail

尝试使用此代码邮寄:

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(Username);
mail.To.Add(emailTo);
mail.Subject = Subject;
mail.Body = Body;

SmtpServer.Port = 587; // Also Add the port number to send it, its default for Gmail
SmtpServer.Credentials = new System.Net.NetworkCredential(Username, Password);
SmtpServer.EnableSsl = true;
SmtpServer.Timeout = 20000; // Add Timeout property
SmtpServer.Send(mail);

让我知道它是否有效。