title: Setting up MSMQ in a WCF application to ensure reliable mail delivery
body: <p>So I have been tasked with setting up MSMQ so that if our mail server goes down (which is seems to often) the messages just end up in the Queue and will be delivered when they come back up. With that said I have to say I don't know much about this except what I have learned in the past 24 hours however I believe I know enough to take the right approach but I wanted to ask someone in the community because there is some confusion amongst my colleagues given some existing setup in our WCF application.</p>
  
  <p>Currently we have some services that use msmq as the protocol for the endpoint. the endpoint looks like this</p>
  
  <pre><code>&lt;endpoint address="net.msmq://localhost/private/Publisher"
  behaviorConfiguration="BatchBehaviour" 
  binding="netMsmqBinding"
  bindingConfiguration="MSMQNoSecurity"
  contract="HumanArc.Compass.Shared.Publisher.Interfaces.Service.IPublisherSubscriber"
  name="PublishSubscriber"/&gt;
  </code></pre>
  
  <p>This of course lets the client make a service call and if for some reason the service wasn't up it will ensure that when the service comes back up the call will be processed. What I don't think that it will do is if you have something like the following in you service method. </p>
  
  <pre><code>try
  {
      smtp.Send(mail);
      return true;
  }
  catch (System.Net.Mail.SmtpFailedRecipientException ex)
  {
       throw new Exception("User Credentials for sending the Email are Invalid",ex);
  }
  catch (System.Net.Mail.SmtpException smtpEx)
  {
     throw new Exception(string.Format("Application encountered a problem send a mail message to {0} ", smtpHostName),smtpEx);
  }
  </code></pre>
  
  <p>WCF isn't going to retry and send the message again somehow, am I correct about this assumption? </p>
  
  <p>What I think we should have is something that looks like the following in place of the call to smtp.send() above. (from <a href="http://www.bowu.org/it/microsoft/net/email-asp-net-mvc-msmq-2.html" rel="nofollow">http://www.bowu.org/it/microsoft/net/email-asp-net-mvc-msmq-2.html</a>)</p>
  
  <pre><code>  string queuePath = @".\private$\WebsiteEmails";
    MessageQueue msgQ;
    //if this queue doesn't exist we will create it
    if(!MessageQueue.Exists(queuePath))
         MessageQueue.Create(queuePath);
    msgQ = new MessageQueue(queuePath);
    msgQ.Formatter = new BinaryMessageFormatter();
    msgQ.Send(msg);
  </code></pre>
  
  <p>Then somewhere in the startup of the service (I am not sure where yet) we set up an event handler that will actually call send() on the SmtpClient object. Something like this</p>
  
  <pre><code> msgQ.ReceiveCompleted += new ReceiveCompletedEventHandler(msgQ_ReceiveCompleted)
  </code></pre>
  
  <p>So to sum it all up my first question is which way is better? Create a service that uses net:msmq as the protocol or just change the email method to put messages in the queue and set up a handler for it? The next question, if my assumption about changing the method that calls SmtpClient.Send() is correct then where in the program should I wire up ReceiveCompleted? Out WCF service is hosted in a windows service, meaning there is actually a call to ServiceBase.Run(servicesToRun). Is there a place I could wire it up there? My experience with WCF is with much simpler IIS hosted services so I am not 100% sure.</p>
  
  <p>Thanks - I realize this is a long question but I have been trying to research it and there is a lot of information and I can't seem to find a clear explanation of the benefits of doing things one way vs another.  </p>