• 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

    • I have mixed feelings about this. This is not going to be a budget CPU; it will likely cost $350+. I don't think you should pay that much for 6-cores in 2025. I am sure it will perform great on some games for a reasonable discount compared to the 9800X3D, and even in games optimized for 8-cores, these 6 will be so fast that it will likely be respectable. I just feel like for (probably) less money, the 9700X would be a better well rounded CPU. I'm only guessing until we see benchmarks. If the 9600X3D beats the 9700X across the board in gaming performance, then I guess it is a good product. However, I more expect it to be a mixed bag of trading blows, and I can't help but think games that show an advantage to more cores are only going to become more common going forward.
    • No, you have no idea, if there is a cable.it is prone to that unless it is shielded.
    • Would be cool if the user can drag it around like a widget. Set a boundary so that it cannot go outside the monitor.
    • missing feew things like CPU temp, hopefully they add an option to customize what would i want displayed or not, but as a default and easy msi afterburner alternative its great and it even work on destiny 2
    • Microsoft Visual Studio Professional 2022 for Windows reaches all-time low price by Steven Parker Today's highlighted deal comes via our Neowin Deals store, where you can save 94% on a copy of Microsoft Visual Studio Professional 2022 for Windows. Visual Studio Professional 2022 is a fully featured development environment that developers around the world know and love. Our first 64-bit IDE makes it easier to work with even bigger projects and more complex workloads. Enhance your productivity, write high-quality code, and re-imagine collaboration with an advanced suite of tools and built-in integrations to tackle the most challenging development workflows and deliver innovative apps. Build across languages & platforms Craft cross-platform mobile and desktop apps with .NET MAUI Build responsive Web UIs in C# with Blazor Build, debug, & test .NET and C++ apps in Linux Use hot reload capabilities across .NET and C++ apps Edit running ASP.NET pages in the web designer view Type less, code more with IntelliCode Understand your code context: variable names, functions, & the type of code you’re writing Complete a line or block of code Get a list of next best options, helping you code more rapidly & accurately Gain deep insights into your code with CodeLens Reveal crucial information like recent changes, authors, tests, and commit history Make informed decisions with a comprehensive overview of your codebase Collaborate seamlessly with Live Share's real-time collaboration sessions Speed up your team's edit & debugging cycles with personalized sessions, access controls, and custom editor settings Ensures everyone's code stays consistent Good to Know ONE-TIME PURCHASE INSTALLED ON 1 DEVICE Redemption deadline: redeem your code within 30 days of purchase Access options: desktop & mobile Version: Professional 2022 Supported languages: Visual Studio is available in English, Chinese (Simplified), Chinese (Traditional), Czech, French, German, Italian, Japanese, Korean, Polish, Portuguese (Brazil), Russian, Spanish, & Turkish Updates included Microsoft Visual Studio Professional 2022 for Windows normally costs $499, but this deal can be yours for just $27.97, that's a saving of $471. For full terms, specifications, and license info, click the link below. Use MSO8 when checking out for additional $8 off. Coupon Expires June 29. Get Microsoft Visual Studio Professional 2022 for Windows for just $19.97, or learn more Although priced in U.S. dollars, this deal is available for digital purchase worldwide. We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
  • Recent Achievements

    • Week One Done
      Helen Shafer earned a badge
      Week One Done
    • First Post
      emptyother earned a badge
      First Post
    • Week One Done
      Crunchy6 earned a badge
      Week One Done
    • One Month Later
      KynanSEIT earned a badge
      One Month Later
    • One Month Later
      gowtham07 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      666
    2. 2
      ATLien_0
      271
    3. 3
      Michael Scrip
      220
    4. 4
      Steven P.
      166
    5. 5
      +FloatingFatMan
      162
  • Tell a friend

    Love Neowin? Tell a friend!