我正在尝试通过我的 Google Apps 电子邮件帐户发送邮件。这是在我自己的域上设置的,并且通过 Web 界面和 Outlook 一切正常。
但是,我尝试使用 C# 从网页发送电子邮件,没有任何异常(exception),一切似乎都很顺利,但电子邮件似乎从未到达:
MailMessage msg = new MailMessage("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a8a9ebb4a3b6aabf86bebebebee8a5a9e8b3ad" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46222728063e3e3e3e68252968332d" rel="noreferrer noopener nofollow">[email protected]</a>");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85ebeaa8f7e0f5e9fcc5fdfdfdfdabe6eaabf0ee" rel="noreferrer noopener nofollow">[email protected]</a>", "xxxxxxxxx");
smtp.EnableSsl = true;
msg.IsBodyHtml = true;
msg.Body = body;
smtp.Send(msg);
正文是我之前在代码中生成的字符串。据我所知,如果实际连接到 Gmail 的 SMTP 服务器出现问题,我会抛出异常。
有什么想法可能会导致此功能不起作用吗?与其他人的示例相比,我能看到的唯一不同之处是我将正文设置为 HTML,并且从 Google Apps 帐户而不是 @gmail.com 帐户发送。
谷歌应用程序本身已完全配置并正常工作,我已经设置了很多次,所以我知道这不太可能成为问题。
我还尝试在端口 25 上发送,因为您在将 Outlook 配置为从 Gmail 帐户发送时使用该端口。
两者的结果相同,没有抛出异常,但电子邮件从未到达。发件人和收件人的电子邮件均位于我使用 Google Apps for Gmail 的域中。
编辑:
此外,我还应该提到,我已使用这两个帐户登录,并且都在其设置等中启用了 IMAP 和 POP。
新发现
这很奇怪
如果我从以下位置手动发送邮件:
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1779783a6572677b6e577a6e73787a767e793974787a" rel="noreferrer noopener nofollow">[email protected]</a> to <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d5d0dff1dcc8d5dedcd0d8df9fd2dedc" rel="noreferrer noopener nofollow">[email protected]</a> - Then it works
But if I send it through code this way...it doesn't work...
如果我通过代码发送以下内容,它确实有效:
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="402e2f6d3225302c39002d39242f2d21292e6e232f2d" rel="noreferrer noopener nofollow">[email protected]</a> to <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb969ebb9c969a9297d5989496" rel="noreferrer noopener nofollow">[email protected]</a> or <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a070f2a041e061d0518060e44090507" rel="noreferrer noopener nofollow">[email protected]</a> - This works through code!
然后我会认为这意味着 [email protected] 有问题正在接收消息...但它会接收从我尝试过的任何 google、hotmail 或 ntlworld 电子邮件地址手动发送的任何消息。
因此,要么 Google Apps 帐户无法接收通过代码发送的电子邮件(不太可能),要么有其他原因在起作用
最佳答案
如果有问题,服务器可能会默默地丢弃您的消息。 - 尽管任何规范另有说明。
尽管已“传递”消息,但消息可能会在以太中丢失。
SMTP 服务器可能正在应用严格的过滤,并且可能还要求您的发件人和目标电子邮件地址“正确”匹配
我建议尝试使用不同的 SMTP 主机进行检查。 :)
关于c# - 什么可能导致使用 C# 从 Gmail SMTP 发送的邮件无法到达 - 不会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5023156/