MailSlurp logo

guides

Send Email in Visual Basic .NET with SMTP

Send email from Visual Basic .NET with authenticated SMTP, then verify the delivered message with a MailSlurp inbox.

View MarkdownAgent setup
Send Email in Visual Basic .NET with SMTP article preview

This guide uses Visual Basic .NET and System.Net.Mail, not Excel VBA. It shows how to configure authenticated SMTP, send a message, and capture the delivered email in MailSlurp for verification.

If you need to send from Office VBA, use an Office-compatible mail library or call a server-side API without placing your MailSlurp API key in the workbook.

Get SMTP server credentials

To send email from Visual Basic .NET, you need access to an SMTP server. This example uses MailSlurp SMTP credentials to configure the client. If you already have SMTP credentials, skip to the next step.

Dim webClient As New Net.WebClient
Dim jsonClient As New Net.WebClient
Dim apiKey As String = Environment.GetEnvironmentVariable("API_KEY")
Assert.IsNotEmpty(apiKey)
Assert.IsNotNull(apiKey)
webClient.Headers.Add("x-api-key", apiKey)
Dim imapSmtpAccessJson = webClient.DownloadString("https://api.mailslurp.com/inboxes/imap-smtp-access")
jsonClient.Headers.Add("Content-Type", "application/json")
Dim username = jsonClient.UploadString("https://api.mailslurp.com/user/json/pluck?property=smtpUsername", imapSmtpAccessJson)
jsonClient.Headers.Add("Content-Type", "application/json")
Dim password = jsonClient.UploadString("https://api.mailslurp.com/user/json/pluck?property=smtpPassword", imapSmtpAccessJson)
jsonClient.Headers.Add("Content-Type", "application/json")
Dim port = jsonClient.UploadString("https://api.mailslurp.com/user/json/pluck?property=smtpServerPort", imapSmtpAccessJson)
jsonClient.Headers.Add("Content-Type", "application/json")
Dim host = jsonClient.UploadString("https://api.mailslurp.com/user/json/pluck?property=smtpServerHost", imapSmtpAccessJson)

Create an SMTP client

Next, take the password, username, host, and port and configure the SMTP client:

Dim Smtp_Server As New Net.Mail.SmtpClient
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential(username, password)
Smtp_Server.EnableSsl = False
Smtp_Server.Port = Integer.Parse(port)
Smtp_Server.Host = host

Now send an email

Once configured, use the Net.Mail.SmtpClient instance to send an email:

Dim email As New Net.Mail.MailMessage()
email = New Net.Mail.MailMessage()
email.From = New Net.Mail.MailAddress(fromAddress)
email.To.Add(toAddress)
email.Subject = "Send email with VB"
email.IsBodyHtml = False
email.Body = "Hello this is me"
Smtp_Server.Send(email)

Next steps

You can also use MailSlurp directly in Excel using Power Query. Check out the guide here.