尝试发送邮件时获取smtpException C#

尝试发送邮件时获取smtpException C#,c#,mono,smtpclient,C#,Mono,Smtpclient,我正在尝试使用此代码通过SmtpClient发送电子邮件 var client = new SmtpClient("smtp.gmail.com", 465) { Credentials = new NetworkCredential("***@gmail.com", "password"), EnableSsl = true, }; client.Send("***@gmail.com", "***

我正在尝试使用此代码通过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“无法发送消息”

我在Mac上使用Mono(OSX 10.9.2)

我的凭据和主机/端口正确。 也许我需要通过我的gmail帐户启用它? 谢谢


你对港口有把握吗?我使用gmail发送电子邮件,我使用587端口。同样的smtpException使用587端口你的代码看起来不错;检查您的防火墙是否阻止连接可能是值得的。你说你的证件绝对正确,但你可能已经通过网络登录gmail检查过了;这显然与通过SMTP连接时使用的端口不同。不起作用。同样的smtpException。问题可能是Mono SDK吗?@NivNavick您的例外情况是什么?似乎在visual studio上工作。问题是MONO SDK。谢谢您可以尝试使用它,它比Mono的System.Net.Mail实现更健壮,可以在Windows、Mac和Linux上运行。
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-
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);
}