Я пробовал несколько вещь, как:C#: Ошибка отправки почты
- использовать различные вариации, порты, SSL включен или выключен, ...
- Другой поставщик услуг электронной почты
- Настройка «Разрешить менее безопасные приложения» в настройках аккаунта GMAIL
Вопрос:
- Как исправить эту ошибку и отправить вложения успешно?
- В чем причина этой ошибки?
- Это потому, что порт может быть заблокирован?
Ошибка:
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
Код:
public void Email()
{
try
{
SmtpClient client = new SmtpClient()
{
Host = @"smtp.gmail.com",
Port = 465,
Credentials = new NetworkCredential(@"[email protected]", @"Password"),
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true
};
MailMessage message = new MailMessage()
{
From = new MailAddress(@"[email protected]"),
Subject = @"Test",
Body = @"Test",
Priority = MailPriority.Normal
};
message.To.Add(@"[email protected]");
Attachment file = new Attachment(@"C:\Users\Alexander\Documents\File.txt");
message.Attachments.Add(file);
client.Send(message);
}
catch { }
}
Вы можете попробовать порт 587 –
... и попробуйте закомментировать 'DeliveryMethod = SmtpDeliveryMethod.Network' линии. –
Я пробовал это, все еще не работает. – HovyTech