Я отправляю электронное письмо через smtp компании, у которого нет учетных данных, только порт 25, но я получаю эту ошибку System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: The connection was closed.
, обратите внимание, что я пытался отправить электронное письмо с сервера hotmail, и это сработало (host: smtp .live.com, порт 587 и мои учетные данные электронной почты)
это мой код:
public static void SendEmail(EmailObject email)
{
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = email.Host;
smtp.EnableSsl = false; // I Tried to set it true as well
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // i tried to remove it
if (!string.IsNullOrEmpty(email.Username) && !string.IsNullOrEmpty(email.Password))
smtp.Credentials = new NetworkCredential(email.Username, email.Password);
if (email.Port != 0)
smtp.Port = email.Port;
MailMessage mailMessage = new MailMessage();
mailMessage = new MailMessage();
mailMessage.Subject = email.Subject;
if (email.Attachments != null)
{
foreach (EmailAttachment emailAttachment in email.Attachments)
{
Attachment attachment = new Attachment(emailAttachment.AttachmentStream, emailAttachment.AttachmentName, emailAttachment.ContentType);
mailMessage.Attachments.Add(attachment);
}
}
System.Net.Mail.MailAddress mailAddress = new System.Net.Mail.MailAddress(email.MailAddress, email.DisplayName);
mailMessage.From = mailAddress;
foreach (var address in email.Recepients.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries))
{
mailMessage.To.Add(address);
}
mailMessage.IsBodyHtml = true;
mailMessage.Body = email.Body.ToString();
smtp.Send(mailMessage);
}