我正在尝试从桌面上运行的C#应用程序发送和发送电子邮件。有没有人看到任何特殊原因,这将导致错误和超时,因此永远不会发送电子邮件。我在网上看了一下,但却找不到实际可行的解决方案。
然而,我确实有一个php邮件直接从发送电子邮件的godaddy服务器运行。所以我知道服务器不是问题。 php版本不需要密码。这是代码。
SmtpClient ss = new SmtpClient();
try
{
ss.Host = "relay-hosting.secureserver.net";
ss.Port = 25;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = false;
ss.Credentials = new NetworkCredential("daemon@johntest.com", "####");
MailMessage mailMsg = new MailMessage("daemon@johntest.com", "testingJohn@gmail.com", "testing email", "my body");
mailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(mailMsg);
Console.WriteLine("Sent email");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
抛出异常:System.dll中的“System.Net.Sockets.SocketException” 抛出异常:System.dll异常中的“System.Net.WebException” 抛出:抛出System.dll异常中的'System.Net.WebException': 抛出System.dll异常中的'System.Net.WebException': 抛出System.dll异常中的“System.Net.Mail.SmtpException”: mscorlib.dll中的“System.InvalidOperationException”
答案 0 :(得分:1)
创建一个这样的方法来测试你的本地邮件主机,然后用godaddy
进行测试public static void SendEmail(string subject, string body)
{
using (var client = new SmtpClient(yourEmailHost, 25)) //"relay-hosting.secureserver.net"
using (var message = new MailMessage()
{
From = new MailAddress(utilities.FromEmail),
Subject = subject,
Body = body
})
{
message.To.Add(address);
//client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.Credentials = new NetworkCredential("nerwork UserName", "Network Password");
client.Send(message);
};
}