Search code examples
visual-studioemailsmtpsendgrid

Visual Basic (VB.NET) example code to send SendGrid email by a VB.NET windows app


I just got my SendGrid account installed on my AWS EC2 Windows server 2019 my VS 2019 Pro for my VB.NET windows app.

But all the examples I can find are in C#.


Solution

  • I got same problem same as you.
    I already developed and tested for both C#.NET and VB.NET.

    C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Configuration;
    using SendGrid;
    using SendGrid.Helpers.Mail;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                TestSendGrid().Wait();
            }
    
            static async Task TestSendGrid()
            {
                try
                {
                    var apiKey = ConfigurationManager.AppSettings["SENDGRID_APIKEY"];
                    var client = new SendGridClient(apiKey);
                    var msg = new SendGridMessage()
                    {
                        From = new EmailAddress("test@example.com", "Test User"),
                        Subject = "Hello World from the SendGrid C#.NET SDK!",
                        PlainTextContent = "Hello, Email!",
                        HtmlContent = "<strong>Hello, Email!</strong>"
                    };
                    msg.AddTo(new EmailAddress("test@example.com", "Test User"));
                    var response = await client.SendEmailAsync(msg);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
    

    VB.NET

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Threading.Tasks
    Imports System.Configuration
    Imports SendGrid
    Imports SendGrid.Helpers.Mail
    
    Module Module1
    
        Sub Main()
            TestSendGrid().Wait()
        End Sub
    
        Private Async Function TestSendGrid() As Task
            Try
                Dim apiKey = ConfigurationManager.AppSettings("SENDGRID_APIKEY")
                Dim client = New SendGridClient(apiKey)
                Dim msg = New SendGridMessage() With {
                    .From = New EmailAddress("test@example.com", "Test User"),
                    .Subject = "Hello World from the SendGrid VB.NET SDK!",
                    .PlainTextContent = "Hello, Email!",
                    .HtmlContent = "<strong>Hello, Email!</strong>"
                }
                msg.AddTo(New EmailAddress("test@example.com", "Test User"))
                Dim response = Await client.SendEmailAsync(msg)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Function
    End Module
    

    Reference: https://learn.microsoft.com/en-us/azure/sendgrid-dotnet-how-to-send-email