• 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

    • It's so sensitive to prompts. I asked it to create a video of two friends fighting over a trophy and it blocked it.
    • i think the above says it all about whats wrong with nutrek
    • Google Chrome 137.0.7151.69 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Google Chrome 137.0.7151.69 changelog: [N/A][420636529] High CVE-2025-5419: Out of bounds read and write in V8. Reported by Clement Lecigne and Benoît Sevens of Google Threat Analysis Group on 2025-05-27. This issue was mitigated on 2025-05-28 by a configuration change pushed out to Stable across all Chrome platforms. [$1000][409059706] Medium CVE-2025-5068: Use after free in Blink. Reported by Walkman on 2025-04-07 Google is aware that an exploit for CVE-2025-5419 exists in the wild. Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | 128.0 MB Download: Google Chrome Offline Installer 32-bit | 115.0 MB Download page: Google Chrome Portable Download: Google Chrome MSI Installers for Windows (automatic update) View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Last night I watched "I, Robot" movie: https://www.youtube.com/watch?v=7Dlo-VB0-HI The day they put AI into Robots is our END... I really scare from that day!!
    • This site is just old men ranting at clouds. Neowin knows its audience.
  • Recent Achievements

    • First Post
      nothin earned a badge
      First Post
    • Enthusiast
      Epaminombas went up a rank
      Enthusiast
    • Posting Machine
      Fiza Ali earned a badge
      Posting Machine
    • One Year In
      WaynesWorld earned a badge
      One Year In
    • First Post
      chriskinney317 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      188
    2. 2
      snowy owl
      134
    3. 3
      ATLien_0
      130
    4. 4
      Xenon
      119
    5. 5
      +FloatingFatMan
      97
  • Tell a friend

    Love Neowin? Tell a friend!