无法从SMTP发送电子邮件

时间:2019-07-04 07:16:51

标签: .net email .net-core smtp

我正在通过公司的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服务器发送电子邮件,并且该电子邮件有效(主机: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);
    }

0 个答案:

没有答案