可能的精确副本: 在C#.NET发送电子邮件通过Gmail
嗨,
我想使用Gmail发送电子邮件:
我想,我在这个网站和其他网站,但我总是得到同样的错误中发现的各种例子:
无法连接到远程服务器 - > System.net.Sockets.SocketException:可以进行无连接,因为目标积极地拒绝它209.85.147.109:587
public static void Attempt1()
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("MyEmailAddress@gmail.com", "MyPassWord"),
EnableSsl = true
};
client.Send("MyEmailAddress@gmail.com", "some.email@some.com", "test", "testbody");
}
有任何想法吗?
UPDATE
更多细节。
也许我应该说我做了什么其他的尝试,给了我同样的错误:(注意:当我没有具体说明它tryed端口25的端口)
public static void Attempt2()
{
var fromAddress = new MailAddress("MyEmailAddy@gmail.com", "From Name");
var toAddress = new MailAddress("MyEmailAddy@dfdf.com", "To Name");
const string fromPassword = "pass";
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); }
}
public static void Attempt3()
{
MailMessage mail = new MailMessage();
mail.To.Add("MyEmailAddy@dfdf.com");
mail.From = new MailAddress("MyEmailAddy@gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
("MyEmailAddy@gmail.com", "pass");
smtp.EnableSsl = true;
smtp.Send(mail);
}