我使用我的namecheap域设置了一封私人电子邮件,但我无法发送电子邮件。以下是我的代码。我错过了什么吗?我每次都收到一条超时消息。
//Send email to end user
MailMessage mm = new MailMessage();
foreach(string to in toList)
{
mm.To.Add(to);
}
mm.From = new System.Net.Mail.MailAddress("fromaddress");
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
var smtp = new SmtpClient
{
Host = "mail.privateemail.com",
Port = 465,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("username", "pw"),
Timeout = 20000
};
smtp.Send(mm);
答案 0 :(得分:4)
端口号可能有误。我尝试使用587并且它在gmail上工作。
并考虑暂时删除超时以获取异常并找出超时的详细原因。
答案 1 :(得分:0)
这段代码是我成功使用的代码。
string to = "yo@yo.com";
string from = "help@help.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient("mail.privateemail.com");
client.Credentials = new System.Net.NetworkCredential("username", "pass");
client.Port = 587;
client.EnableSsl = true;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}",
ex.ToString());
}
答案 2 :(得分:0)
Namecheap现在仅在端口465上支持SMTP,在最近的udpate之后支持隐式SSL。不幸的是,System.Net.Mail不支持这一点(见here)。解决方法可能是使用System.Web.Mail命名空间(只是它已经过时了)但它确实可以像this SO post中看到的那样工作。
答案 3 :(得分:0)
解决方案是: