【问题标题】:Getting smtpException when attemping to send mail C#尝试发送邮件 C# 时出现 smtp 异常
【发布时间】:2014-05-16 10:25:24
【问题描述】:

我正在尝试使用此代码通过 SmtpClient 发送电子邮件

var client = new SmtpClient("smtp.gmail.com", 465)
        {
            Credentials = new NetworkCredential("***@gmail.com", "password"),
            EnableSsl = true,
        };
        client.Send("***@gmail.com", "***@gmail.com", "test", "testbody");

我得到的是 smtpException "无法发送消息"。

System.Net.Mail.SmtpException: Message could not be sent. ---> System.Exception:Connectionclosed at System.Net.Mail.SmtpClient.Read () [0x000f9] in /private/tmp/source/bockbuild-mono-

我在 Mac (OSX 10.9.2) 上使用 Mono

我的凭据和主机/端口是正确的。 也许我需要以某种方式通过我的 gmail 帐户启用它? 谢谢!

【问题讨论】:

  • 您确定端口吗?我正在使用 gmail 发送电子邮件,并且使用端口 587。
  • 相同的 smtpException 使用端口 587
  • 你的代码看起来不错;可能值得检查您的防火墙是否阻止连接。您说您的凭据绝对正确,但您可能已经通过网络登录 gmail 进行了检查;这显然不使用与以这种方式通过 SMTP 连接相同的端口。

标签: c# mono smtpclient


【解决方案1】:

Use:

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

【讨论】:

  • 不工作。同样的 smtpException。问题可能出在 Mono SDK 上吗?
  • @NivNavick 你的例外是什么?
  • 似乎适用于视觉工作室。问题是 MONO SDK。谢谢!
  • 您可以尝试改用MailKit - 它比 Mono 的 System.Net.Mail 实现更强大,并且可以在 Windows、Mac 和 Linux 上运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2015-11-20
相关资源
最近更新 更多