我尝试使用此代码通过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上使用Mono(OSX 10.9.2)
我的凭据和主机/端口是正确的。 也许我需要以某种方式通过我的Gmail帐户启用它? 谢谢!
答案 0 :(得分:0)
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);
}