[英]C#: Failure Sending Mail
I've tried multiple thing like: 我已经尝试了多种方法,例如:
Question: 题:
Error: 错误:
Exception thrown: 'System.IO.IOException' in System.dll
Exception thrown: 'System.Net.Mail.SmtpException' in System.dll
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at TreeView.TreeView.<Email>d__28.MoveNext() in C:\Users\Alexander\Documents\Visual Studio 2015\Projects\TreeView\TreeView\TreeView\TreeView.cs:line 365
Code: 码:
public void Email()
{
try
{
SmtpClient client = new SmtpClient()
{
Host = @"smtp.gmail.com",
Port = 465,
Credentials = new NetworkCredential(@"mailid@gmail.com", @"Password"),
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true
};
MailMessage message = new MailMessage()
{
From = new MailAddress(@"mailid@gmail.com"),
Subject = @"Test",
Body = @"Test",
Priority = MailPriority.Normal
};
message.To.Add(@"mailid@example.com");
Attachment file = new Attachment(@"C:\Users\Alexander\Documents\File.txt");
message.Attachments.Add(file);
client.Send(message);
}
catch { }
}
For gmail I have 对于gmail我有
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("mygmailusername@gmail.com", "mygmailpassword");
////email.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network ;
PS You may need to enable this: PS您可能需要启用此功能:
https://support.google.com/accounts/answer/6010255?hl=en https://support.google.com/accounts/answer/6010255?hl=zh_CN
APPEND: 附加:
Download this tool: 下载此工具:
https://www.microsoft.com/en-us/download/details.aspx?id=24009 https://www.microsoft.com/zh-cn/download/details.aspx?id=24009
And see if you can query that port/address 并查看是否可以查询该端口/地址
I get the below results: 我得到以下结果:
=============================================
*Starting portqry.exe -n smtp.gmail.com -e 587 -p TCP ...
Querying target system called:
smtp.gmail.com
Attempting to resolve name to IP address...
Name resolved to 74.125.29.109
querying...
TCP port 587 (unknown service): LISTENING
portqry.exe -n smtp.gmail.com -e 587 -p TCP exits with return code 0x00000000.*
Setting the UseDefaultCredentials
will always set the Credentials
to null
. 设置
UseDefaultCredentials
将始终将Credentials
设置为null
。
So you have to set the UseDefaultCredentials = true/false
before you set the Credentials
. 因此,必须先设置
UseDefaultCredentials = true/false
然后再设置Credentials
。
Reference: https://stackoverflow.com/a/27896975/5075227 参考: https : //stackoverflow.com/a/27896975/5075227
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.