Alexander Abramovich
Alexander Abramovich

Reputation: 11458

Can't send mail with SmtpClient

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

  var to = "[email protected]";
  var subject = "test";
  var body = "test mail";
  var message = new MailMessage(from, to, subject, body);
  var client = new SmtpClient { Credentials = new NetworkCredential("[email protected]", "mypassword") };
  client.Send(message);

here is the App.config:

  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network
          host="mail.mycompany.com"
          port="25"
          userName="[email protected]"
          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?

Upvotes: 4

Views: 6192

Answers (4)

Coding Flow
Coding Flow

Reputation: 21881

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.

Upvotes: 2

Matt Farguson
Matt Farguson

Reputation: 325

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 protected]"
        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();
    }

Upvotes: 1

Alexander Abramovich
Alexander Abramovich

Reputation: 11458

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

Upvotes: -1

Alex
Alex

Reputation: 933

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#

Upvotes: 2

Related Questions