Question

Can't send mail. Here is my C# source:

  var to = "me@mycompany.com";
  var subject = "test";
  var body = "test mail";
  var message = new MailMessage(from, to, subject, body);
  var client = new SmtpClient { Credentials = new NetworkCredential("me@mycompany.com", "mypassword") };
  client.Send(message);

here is the App.config:

  <system.net>
    <mailSettings>
      <smtp from="me@mycompany.com">
        <network
          host="mail.mycompany.com"
          port="25"
          userName="me@mycompany.com"
          password="mypassword"
        />
      </smtp>
    </mailSettings>
  </system.net>

So far, if the host/port in App.config are wrong an exception is thrown, but not if user/pass are wrong (obvious security reasons). However, I've succeeded to log-in from Microsof Outlook with just the same user/pass as in the source.

The email is not received, nor in Inbox, neither in Junk folder. How can I validate the server-side (considering it's a part of microsoftonline.com)? What am I missing? What am I doing wrong, please?

Was it helpful?

Solution 4

Got an answer from the IT team - it was a configuration/security issue. Solved with IT. Thanks a lot, everyone.

OTHER TIPS

The link below has an example of a very simple send email function that can be called from a console application to test. I know this code works, so it could isolate any issue with your code and maybe shed a little more light on where the issue is.

SendEmail() – Create and Send Email Messages in C#

I am pretty sure that if server rejects the email for any reason your code would throw an exception. If it is not then that implies that the server is accepting the emails with the supplied user name and password and technically the mails are "sending" successfully. However this is no guarantee that anyone will receive any emails. You need to find out what the email server is doing with these emails and why.

I was having firewall issues trying to use smtp as well. This is a work-around if you don't want to work with IT and do have outlook installed. This method will use your default email to send though. You will also need to add a reference (I'm using under COM 'Microsoft Outlook 14.0 Object Library')

     using Outlook = Microsoft.Office.Interop.Outlook;

     private void sendEmail(string DistributionList, string AttachmentDestination)
      {
        //new outlook instance
        Outlook.Application app = new Outlook.Application();

        //new mail object
        Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem);

        mail.Subject = "This is coming from a C# script";
        mail.To = DistributionList; //your distribution list "email@something.com"
        mail.Body = "This is the body of an email from a C# script";
        mail.Attachments.Add(AttachmentDestination); //location of attachment (can be ommitted)
        mail.Send();
        app.Quit();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top