简体   繁体   中英

C# asp.net System.Net.Mail outgoing emails not recieved

I'm using System.Net.Mail and I'm trying to send an email (to myself actually) to some address. I belive I'm setting it up correctly and I'm getting no exceptions thrown but the mail isn't delivered?! I have no clue as to what's going on. I have no messages, errors, no indication of a problem. Here's my code:

using System.Net.Mail;

EmailSender email = new EmailSender();

email.From = "steve_kershaw@yahoo.com";
email.Subject = "Test email!";
email.To = "Steve.Kershaw@securitynational.com";
email.Body = "An event just occured.";
email.SendMail();

And in a separate class I have:

mMailMessage.Subject = strSubject;
mMailMessage.Body = strBody;
mMailMessage.IsBodyHtml = true;
mMailMessage.Priority = MailPriority.Normal;

// Send the message.
SmtpClient mSmtpClient = new SmtpClient(strHost);
mSmtpClient.Send(mMailMessage);

I've stepped through this code countless times but everything seems normal. Can anybody help?!

Your code looks fine to me. Check out your SmtpClient configuration.

There is a cool blog post about Sending Email with System.Net.Mail . Your SmtpClient should look like this;

<system.net>
    <mailSettings>
      <smtp from="steve_kershaw@yahoo.com">
        <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

This may be elementary, but also ensure the smtp service is running. Had several occasions where the code was correct but emails did not deliver because of that very simple thing.

May also be worthwhile to restart the smtp service

I had an issue where everything looked correct but the mail wasn't sending. No errors or exceptions. The problem was my 'From address'. I had picked a made up address like: 'do-not-reply@ThisAppsName.com' Turns out, the server realised that wasn't an address and didn't send the mail. I changed it to an email address of the App's owners dis list and all went fine.

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