邮件已发送但未收到

时间:2018-06-27 12:54:39

标签: c# email iis asp.net-web-api smtp

这个问题我几乎要花一个星期的时间。这是我的代码

public string GenerateEmail(EmailConfigDetail emailConfig)
    {
        MailMessage mailMessage = new MailMessage();
        try
        {
            if (emailConfig != null)
            {
                fromMailId = emailConfig.fromMailId;
                toMailId = emailConfig.toMailId;
                hostAddress = emailConfig.hostAddress;
                Body = emailConfig.Body;
                subject = emailConfig.subject;
                attachmentPath = emailConfig.AttachmentFilePath;
            }


            mailMessage.To.Add(toMailId);
            mailMessage.From = new MailAddress(fromMailId);
            mailMessage.Subject = subject;
            mailMessage.Body = Body;
            mailMessage.IsBodyHtml = true;
            if (!string.IsNullOrWhiteSpace(attachmentPath))
            {
                Attachment attachment = new Attachment(attachmentPath, MediaTypeNames.Application.Pdf);
                //ContentDisposition disposition = attachment.ContentDisposition;
                string guid = attachment.Name.Split('_')[2];
                attachment.Name = attachment.Name.Replace('_' + guid, string.Empty);
                mailMessage.Attachments.Add(attachment);
            }

            SmtpClient smtp = new SmtpClient();
            smtp.Host = hostAddress;
            smtp.Credentials = new System.Net.NetworkCredential();
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.EnableSsl = true;
            ServicePointManager.ServerCertificateValidationCallback =
              delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
              { return true; };

            smtp.Send(mailMessage);
            DisposeMailAttachments(mailMessage);
            retVal = "Email sent successfully";
            return retVal;
        }
        catch (Exception ex)
        {
            DisposeMailAttachments(mailMessage);
            throw ex;
        }
        finally
        {
            DisposeMailAttachments(mailMessage);
        }

    }

我上面的代码在发送邮件时工作正常。而且我得到了准确的消息"Email sent successfully";,没有任何错误。但我无法在我的收件地址中收到任何邮件。我已经问过我的管理团队和支持团队。他们没有告诉他们任何问题。如果我想念什么,请您建议我。

From address: no-reply@mydomain.com
To Address: ramesh.rajendran@mydomain.com

1 个答案:

答案 0 :(得分:0)

如果我禁用SSL可以正常工作

smtp.EnableSsl = false;