SMTP Gmail timing out

前端 未结 4 1574
暖寄归人
暖寄归人 2020-12-17 06:09

Not sure why this is happening. Every where I\'ve search tells me that i\'m doing this right. But every time I try and send the mail, it times out on the smtpserver.Se

4条回答
  •  生来不讨喜
    2020-12-17 06:39

    This thread helped me. I'm not sure why this code worked and mine wasn't.

    Sending email in .NET through Gmail

    using System.Net;
    using System.Net.Mail;
    
    var fromAddress = new MailAddress("from@gmail.com", "From Name");
    var toAddress = new MailAddress("to@example.com", "To Name");
    const string fromPassword = "fromPassword";
    const string subject = "Subject";
    const string body = "Body";
    
    var smtp = new SmtpClient
               {
                   Host = "smtp.gmail.com",
                   Port = 587,
                   EnableSsl = true,
                   DeliveryMethod = SmtpDeliveryMethod.Network,
                   UseDefaultCredentials = false,
                   Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
               };
    using (var message = new MailMessage(fromAddress, toAddress)
                         {
                             Subject = subject,
                             Body = body
                         })
    {
        smtp.Send(message);
    }
    

提交回复
热议问题