Problem
How do I send an email using the outMail API in Microsoft Visual Basic .NET?
How do I send an email in VB .NET with a RESTful API?
Solution
The following example of code assumes you have already got a fully functional development environment, and you have working knowledge of Visual Basic and the .NET Libraries.
In your VS Solution you will need to add the NuGet package Newtonsoft.JSON.
Example code outmail-send.vb
'
' outmail-send.vb
'
' This example will send an email using the outmail RESTful API.
'
'
Imports Newtonsoft.Json
Imports System
Imports System.Collections.Generic
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Text
Module Program
Private Const apiSecretKey As String = "YOUR_SECRET_API_KEY"
Private Const apiUrl As String = "https://api.smtp-engine.com/outmail/v1/email/send"
Sub Main(args As String())
Dim toAddress As List(Of String) = New List(Of String)
toAddress.Add("someone@example.com")
toAddress.Add("someone-else@example.com")
' Create the New Email Message
Dim newMessage As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
newMessage.Add("api_key", apiSecretKey)
newMessage.Add("to", toAddress)
newMessage.Add("sender", "me@example.com")
newMessage.Add("subject", "Sending via Outmail API")
newMessage.Add("text_body", "This is the text message body")
newMessage.Add("html_body", "<body><h1>This is the HTML message</h1></body>")
' Convert the Dictionary to Json
Dim newMessageJsonStr As String = JsonConvert.SerializeObject(newMessage, Formatting.Indented)
' Create HTTP resource
Dim client As New HttpClient()
' Add an Accept header for JSON format.
client.DefaultRequestHeaders.Add("ContentType", "application/json")
' Create the content
Dim content = New StringContent(newMessageJsonStr, Encoding.UTF8, "application/json")
' Do the POST
Dim response As HttpResponseMessage = client.PostAsync(apiUrl, content).Result
' Check the result for a valid HTTP 200 success And process the returned result
' Or handler the failure.
If (response.IsSuccessStatusCode) Then
Dim jsonResponseStr = response.Content.ReadAsStringAsync().Result
Dim jsonResponse As Object = JsonConvert.DeserializeObject(jsonResponseStr)
If jsonResponse.ContainsKey("data") Then
Console.WriteLine("status = {0}", jsonResponse("status"))
Console.WriteLine("message id = {0}", jsonResponse("data")("email_id"))
Else
Console.WriteLine("response = {0}", response)
Console.WriteLine("json response = {0}", jsonResponse)
End If
Else
' Get the Json Error response And convert it
Dim jsonResponseStr = response.Content.ReadAsStringAsync().Result
Dim jsonResponse As Object = JsonConvert.DeserializeObject(jsonResponseStr)
Console.WriteLine("status = {0}", jsonResponse("status"))
Console.WriteLine("message = {0}", jsonResponse("message"))
Console.WriteLine("error = {0}", jsonResponse("errors"))
Console.WriteLine("{0} ({1})", response.StatusCode, response.ReasonPhrase)
End If
'Dispose once all HttpClient calls are complete.
client.Dispose()
End Sub
End Module
Summary of server details
Outgoing server |
mxXXXXXX.smtp-engine.com As provided in your signup email. |
Outgoing server protocol |
SMTP |
Outgoing server port |
25, 465, 587, 2525 or 8025 |
Authentication Type |
Basic Authentication, SSL and TLS supported |
Username |
As provided |
Password |
As provided |