簡體   English   中英

使用gmail發送電子郵件:操作已超時

[英]Send email using gmail: The operation has timed out

我正在嘗試將控制台應用程序配置為向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應用程序(從本地主機)發送電子郵件?

檢查一下,我認為這是您的問題

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);

讓我知道它是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM