SMTP Gmail 超时 [英] SMTP Gmail timing out

查看:25
本文介绍了SMTP Gmail 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么会这样.我搜索过的每一个地方都告诉我我做对了.但是每次我尝试发送邮件时,它都会在 smtpserver.Send(mail)

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.Send(mail)

private void emailReport(string email_address,int begDatabaseCount, int endDatabaseCount)
        {
            SmtpClient smtpserver = new SmtpClient();
            MailMessage mail = new MailMessage();
            smtpserver.EnableSsl = true;
            smtpserver.Port = 465;
            smtpserver.Host = "smtp.gmail.com";           
            smtpserver.Credentials = new NetworkCredential("mtaylor@atr.com", "password");
            smtpserver.UseDefaultCredentials = false;
            mail = new MailMessage();
            mail.From = new System.Net.Mail.MailAddress("mtaylor@atr.com", "ATR Reports");
            mail.To.Add(email_address);
            mail.Subject = "FNAS Report - " + DateTime.Now;
            mail.Body += "<u><b>FNAS Report for " + DateTime.Now + "</u></b>" + "
 
";
            mail.Body += "Beginning Database Count - " + begDatabaseCount + "
" + "
";
            mail.Body += "End Database Count - " + endDatabaseCount + "
" + "
";
            mail.Body += "<b>Total Imported Orders = " + (endDatabaseCount - begDatabaseCount) + "<b>" + "
" + "
";
            mail.IsBodyHtml = true;

            smtpserver.Send(mail);
        }

端口 465 = 1 分钟后超时

Port 465 = Time Out after 1 minute

Port 587 = "SMTP 服务器需要安全连接或客户端未通过身份验证.服务器响应为:5.5.1 需要身份验证."

Port 587 = "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. "

推荐答案

这个帖子帮了我.我不确定为什么这段代码有效,而我的无效.

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

通过 Gmail 在 .NET 中发送电子邮件

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

这篇关于SMTP Gmail 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆