Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's an example of an email queue I use... I think you need to add a formatter.</p> <p>Here are the DTOs:</p> <pre><code>[Serializable] public class EmailOut { private readonly string _fromEmail; private readonly string _toEmail; private readonly string _subject; private readonly string _body; private readonly string _cc; private readonly IList&lt;EmailOutAttachment&gt; _emailOutAttachments; public EmailOut(string fromEmail, string toEmail, string subject, string body, string cc, IList&lt;EmailOutAttachment&gt; emailOutAttachments) { _fromEmail = fromEmail; _cc = cc; _emailOutAttachments = emailOutAttachments; _toEmail = toEmail; _subject = subject; _body = body; } public string FromEmail { get { return _fromEmail; } } public string ToEmail { get { return _toEmail; } } public string Subject { get { return _subject; } } public string Body { get { return _body; } } public string Cc { get { return _cc; } } public IList&lt;EmailOutAttachment&gt; EmailOutAttachments { get { return _emailOutAttachments; } } public override string ToString() { return string.Format("FromEmail: {0}, ToEmail: {1}, Subject: {2}, Body: {3}, Cc: {4}", _fromEmail, _toEmail, _subject, _body, _cc); } } [Serializable] public class EmailOutAttachment { private readonly string _name; private readonly byte[] _bytes; private readonly int _size; //Null ok for emailer private readonly string _mimeEncodingType; /// &lt;summary&gt; /// Null ok for mime type. /// &lt;/summary&gt; /// &lt;param name="name"&gt;&lt;/param&gt; /// &lt;param name="bytes"&gt;&lt;/param&gt; /// &lt;param name="size"&gt;&lt;/param&gt; /// &lt;param name="mimeEncodingType"&gt;Null ok for mime type.&lt;/param&gt; public EmailOutAttachment( string name, byte[] bytes, int size, string mimeEncodingType) { _bytes = bytes; _name = name; _size = size; _mimeEncodingType = mimeEncodingType; } public byte[] Bytes { get { return _bytes; } } public int Size { get { return _size; } } public string MimeEncodingType { get { return _mimeEncodingType; } } public string Name { get { return _name; } } </code></pre> <p>}</p> <p>Here is an extension you need.</p> <pre><code>public static Byte[] GetBytes(this Stream input) { if (null == input || 0 == input.Length) return new Byte[0]; byte[] bytes = new byte[input.Length]; int numBytesToRead = (int)input.Length; int numBytesRead = 0; while (numBytesToRead &gt; 0) { // Read may return anything from 0 to numBytesToRead. int n = input.Read(bytes, numBytesRead, numBytesToRead); // Break when the end of the file is reached. if (n == 0) break; numBytesRead += n; numBytesToRead -= n; } return bytes; } </code></pre> <p>This is the sending mechanism:</p> <pre><code> using (Message msg = new Message()) { msg.Body = new EmailOut(from, to, title, body, cc, attachments); msg.Recoverable = true; msg.Formatter = new BinaryMessageFormatter(); string queuePath = config.MsmqEmailBox; //if this queue doesn't exist we will create it //if (!MessageQueue.Exists(queuePath)) // MessageQueue.Create(queuePath); using (MessageQueue messageQueue = new MessageQueue(queuePath)) { messageQueue.Formatter = new BinaryMessageFormatter(); messageQueue.Send(msg); } } </code></pre> <p>This is the receiving end of it...</p> <pre><code> using (var messageQueue = new MessageQueue(_config.MsmqEmailBox)) try { _log.Debug("Retrieving queue"); messageQueue.Formatter = new BinaryMessageFormatter(); messageQueue.MessageReadPropertyFilter.SetAll(); List&lt;Message&gt; allMessages = messageQueue.GetAllMessages().ToList(); if(0 &lt; allMessages.Count) SendEmailNoMsmq.Instance.SendDotNetEmail(_config.DevRecipient,string.Format("Sending queued messages, Count: {0}, Time: {1}",allMessages.Count,DateTime.Now),"MSMQ MAIL SEND", _config, _log); foreach (Message message in allMessages) { EmailOut msg = (EmailOut)message.Body; _log.Info(string.Format("Sending email id:{0} to:{1} ", message.Id, msg.ToEmail)); SendEmailNoMsmq.Instance.SendDotNetEmail(msg, _config, _log); messageQueue.ReceiveById(message.Id); } } catch (Exception ex) { _log.Error("Error ex:" + ex); throw; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload