• 0

VB.Net service to send xml email from MSMQ


Question

I have created a MSMQ server and written VB.NET code to drop an XML file into a private queue in the following format:

<emailRequest>

<senderName>Tester</senderName>

<senderAddr>sender@thedomain.com</senderAddr>

<subject>Test Subject</subject>

<body>This is a test</body>

<recipients>receiver@domain.com</recipients>

</emailRequest>

I need some guidance on creating a VB.NET windows service that checks the private queue for messages, processes them out sends them out via our SMTP server. I've had a good look around the Internet but can't find a definitive guide. Can you point me in the right direction, or even provide some code examples?

6 answers to this question

Recommended Posts

  • 0

Have a look at NServiceBus - http://www.nservicebus.com/

We use this, pain in the ass to debug but will do what you need MSMQ wise but without having to interact with the MSMQ, NSB does it all for you.

GE

  • 0

We use MSMQ together with C# to do something similar. I've never used NServiceBus, but it sounds a bit complicated for what you're trying to do.

Writing my own handler for writing and reading for the MSMQ took about 100 lines of code. The handler will fetch an item from the queue send it for processing.

Since my code is C# I won't share it, but there's some basic useage info using Visual Basic on this page: http://www.microsoft.com/msj/0599/msmq/msmq.aspx

Hope that helps you out.

  • 0

I don't know MSMQ, but since you've written the code to put the XML message into the queue, you should be able to write the code to get it out and process it.

Once you have your batch of XML messages, you will want to loop through them and use the System.Net.Mail.SmtpClient class to do the actual heavy lifting.

Here is a snippet from MSDN:

Public Shared Sub CreateTestMessage2(ByVal server As String)

Dim [to] As String = "jane@contoso.com"

Dim [from] As String = "ben@contoso.com"

Dim message As New MailMessage([from], [to])

message.Subject = "Using the new SMTP client."

message.Body = "Using this new feature, you can send an e-mail message from an application very easily."

Dim client As New SmtpClient(server)

' Credentials are necessary if the server requires the client

' to authenticate before it will send e-mail on the client's behalf.

client.UseDefaultCredentials = True

Try

client.Send(message)

Catch ex As Exception

Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString())

End Try

End Sub

I imagine you would call this Sub from inside your loop.

Question, with MSMQ can you queue up objects or is it limited to XML? If it can handle objects, maybe adding MailMessage objects directly into the queue might make the service a bit simpler. You would then only have to pull out all the MailMessage objects and fire them at .Send() method. Either way, you are going to have to create MailMessage objects on one side of MSMQ.

  • 0

With MSMQ you can queue anything as long as you know how you serialized it. You can even put objects of different types into the same queue, and the queue also has a priority system you can take advantage of (urgent mails can get high pri and will be withdrawn from the queue before other mail).

If more than one program will access the queue, raw objects are perhaps not the best way to go. You'll be better off with XML then, since the job of actually setting up the Message for sending is not the hard part of this program.

This topic is now closed to further replies.
  • Posts

    • Rumor: Apple eyeing over 5 million Samsung displays for '2026' foldable iPhone by David Uzondu You've probably been hearing rumors about Apple foldables for a while now, and in fact, this is perhaps the umpteenth article you've read about the topic. Still, new information from analyst Ming-Chi Kuo, claims that production of the first foldable iPhone could finally kick off later this year, pointing toward a launch in 2026. This news centers on a major component order, with Samsung being the exclusive supplier for the initial run, which neatly corroborates previous supply chain reports we have seen over the years. Apple and Samsung are supposedly working together to solve the one thing that has bugged people since foldables became a thing: the crease. Samsung has improved its panels over time, but Apple is reportedly pushing to have it nearly invisible before any phone ships to customers. This obsession with getting the hardware perfect feels very on-brand. After all, this is the company that is usually late to the party with new product categories, perfecting its take on devices like iPhones, iPads, the Apple Watch, and also AI. Okay, that last one isn't going smoothly for the company, but its track record with hardware is generally a different story. And the evidence for that hardware push is now getting much more specific. The new report from Kuo (translated) claims that Samsung Display is setting up a production line with the capacity of close to 8 million units. For context, the original Galaxy Fold sold less than a million units, and Samsung has been notoriously cagey about the exact number ever since. Despite their growing popularity, foldables are still a pretty niche category, but an initial order of this size, if true, suggests Apple thinks it can be the company to finally push them into the mainstream. Then again, Apple has not had a ton of success with its recent foray into the niche mixed-reality segment with the Apple Vision Pro, so maybe its magic touch has its limits. Of course, you should always take this sort of information with a grain of salt until you hear an official word from Apple. The specs are not finalized, and tech timelines can change depending on production issues or last-minute tweaks.
    • Cheaper maybe; effective, based on past experience, not likely. And yes, companies are on the market to generate profits but there is a huge difference between long terms plans and short ones generating profits but compromising the long term health, and consequently profits, of companies.
    • Lightweight = 100 MB, really? IrfanView is 4 MB!
    • Cool! Something no one wants that eats up storage space and battery life for free! /s
    • GT games are boring compared to Forza (Horizon or Motorsport)
  • Recent Achievements

    • First Post
      MikeK13 earned a badge
      First Post
    • One Month Later
      OHI Accounting earned a badge
      One Month Later
    • Week One Done
      OHI Accounting earned a badge
      Week One Done
    • First Post
      Thornskade earned a badge
      First Post
    • Week One Done
      Higante88 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      699
    2. 2
      ATLien_0
      267
    3. 3
      Michael Scrip
      202
    4. 4
      +FloatingFatMan
      171
    5. 5
      Steven P.
      134
  • Tell a friend

    Love Neowin? Tell a friend!