嗨,
我正在尝试使用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");
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
UPDATE
更多细节.
也许我应该说我做出的其他尝试给了我同样的错误:(注意当我没有指定端口时它尝试端口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);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码:
SmtpClient sc = new SmtpClient("smtp.gmail.com");
NetworkCredential nc = new NetworkCredential("username", "password");//username doesn't include @gmail.com
sc.UseDefaultCredentials = false;
sc.Credentials = nc;
sc.EnableSsl = true;
sc.Port = 587;
try {
sc.Send(mm);
} catch (Exception ex) {
EventLog.WriteEntry("Error Sending", EventLogEntryType.Error);
}
Run Code Online (Sandbox Code Playgroud)