简体   繁体   中英

C# Smtp.Send not working for specific email address

I haven't had any problems with this code except for one user's email address (everyone has the same "@OurCompany" domain name).

I have sent an email to him through Outlook and it went through fine. There are no exceptions being thrown when the code is run, but our SysAdmin says the emails I tried to send aren't even hitting email the server.

public static void SendEmail(string sTo, string sSubject, string sBody)
{
    using (MailMessage message = new MailMessage(new MailAddress(ConfigurationManager.AppSettings["FromUser"], "User"), new MailAddress(sTo))
    {
        Subject = sSubject,
        Body = sBody
    })
    {
        using (var client = new SmtpClient(ConfigurationManager.AppSettings["SMTPGridName"]))
        {
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
            client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["EmailCredential"],
                ConfigurationManager.AppSettings["EmailPassword"]);
            client.EnableSsl = true;
            client.Send(message);
        }
    }
}

here is a simple method that can save you a lot of headache

public static void SendEmail(string sTo, string subject, string body)
{
    var Port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
    using (var client = new SmtpClient(Your EmailHost, Port))
    using (var message = new MailMessage()
    {
        From = new MailAddress(FromEmail),
        Subject = subject,
        Body = body
    })
    {
        message.To.Add(sTo);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["EmailCredential"],
                ConfigurationManager.AppSettings["EmailPassword"]);
        client.EnableSsl = true;
        client.Send(message);
    };
}

It turned out the emails were being dropped by SendGrid before it got to our email server for reason "Bounced Address." The SysAdmin thinks the email server might have been down at one point which caused the email address to be added to a "does not exist" list. The address was removed from the list and it works now.

Thanks for the suggestions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM