类型的未处理的异常'System.Net.Mail.SmtpException“在System.dll中发生 [英] An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
问题描述
我通过 .NET
试图就如何发送电子邮件非常简单的教程和 C#
。然而,当我尝试执行code,我得到了以下异常:
I'm trying out very simple tutorial on how to send e-mail via .NET
and C#
. However when I try to execute the code I got the following exception:
键入System.Net.Mail.SmtpException未处理的异常在System.dll中其他信息:该操作已超时 出去。
An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The operation has timed out.
因此,我所做的是找到我要发送邮件到该服务器的设置。这是很受欢迎,在我的国家:
So what I have done is to find the server settings to which I want to send mails to. This is very popular in my country:
Incoming settings
Protocol -> POP
Email address -> username@abv.bg
Username -> username@abv.bg
Password -> password
POP server -> pop3.abv.bg
Security type -> SSL
Server port -> 995
Outgoing server settings
Username -> username@abv.bg
Password -> password
SMTP server -> smtp.abv.bg
Security type -> SSL
Server port -> 465
然后,我创建了一个控制台项目
和我的主要方法,我有这个code:
then I created a Console project
and in my main method I have this code:
SmtpClient client = new SmtpClient("smtp.abv.bg");
client.Port = 465;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(
"username@abv.bg", "password");
MailMessage msg = new MailMessage();
msg.To.Add("username@abv.bg");
msg.From = new MailAddress("username@abv.bg");
msg.Subject = "Test subject";
msg.Body = "Test test test...";
client.Send(msg);
由于我没有经验,这个我只是尝试了code,你看到它。无需额外设置的任何地方。我认为唯一不应该是问题,但我觉得值得一提的是,在这里:
Since I have no experience with this I just try the code as you see it. No additional settings anywhere. The only thing that I think should not be problem but I think worth mentioning that here:
client.Credentials = new NetworkCredential(
"username@abv.bg", "password");
这里:
msg.To.Add("username@abv.bg");
我使用的是相同的电子邮件。但我认为这不应该是一个问题。任何想法,我究竟做错了什么?
I'm using the same e-mail. But I think this shouldn't be a problem. Any idea what am I doing wrong here?
推荐答案
下面是一个工作示例(使用Gmail)我写了几年前的测试。
Here is a working example (with Gmail) I wrote years ago for testing.
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("xxx@gmail.com");
mail.From = new MailAddress("yyy@gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yyy@gmail.com", "pw"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}
我会尝试这个功能与Gmail只是为了消除网络相关的问题,如防火墙。如果,将工作,剩下的只是找到了SMPT服务器的权限settigs
I would try this function with Gmail just to eliminate network related issues like firewall. When that will work, the rest is just to find the right settigs for the SMPT server
这篇关于类型的未处理的异常'System.Net.Mail.SmtpException“在System.dll中发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!