Visual Basic is a powerful scripting language from Microsoft that has many uses. Sending email with VBA is easy using the
Net.Mail.SmtpClient package. In this post we will demonstrate how to setup VBA with SMTP mailserver credentials and then send emails from code or tests.
Configure an SMTP server access
To send email in visual basic you need access to a MailServer (or API such as MailSlurp). In this example we will use a MailServer hosted by MailSlurp to configure our VBA client. If you already have a MailServer you can 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 we can 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)
Test emails safely with no risk of spamming users with real emails
Receive email in DotNET Core using C# and MailSlurp
MailSlurp Java SDK for sending and receive email and attachments on the JVM.
How to send email in CSharp, PHP, Ruby, Java, Python, and NodeJS using the SMTP protocol and MailSlurp mailservers.
The Most Advanced Guide on Azure Email Sending.
1111 SMTP Connections failing
Create real throw-away email addresses for testing and development in .NET core. Follow this guide to get started.
Create cross platform build scripts for DotNET Core in a way similar to Makefiles.
Send emails using SMTP mailer in Python. Detailed tutorial for sending mail in Python.
JS SMTP email sending guide using HTML and NodeJS.
Alternatives to Mailinator for test email accounts. Create real email addresses using MailSlurp
PHPMailer tutorial (Sending email with PHP)
Use Send-MailMessage in Windows Powershell to send emails using an SMTP server or MailSlurp's free email API.
RLang tutorial for sending emails over SMTP.
A full list of commands and response status codes for SMTP mail servers when sending emails.
How to use Telnet to test the SMTP servers?
Amazon SES is an email service among virtual servers, AI apps, and libraries. Ideally, you can integrate your email system with your program hosted on Amazon.
What are common SMTP mail server ports and how do you find them?
Picking the right port for SMTP mailservers
How to test .NET authentication and sign-up using real email accounts with MailSlurp and SpecFlow.
Send and receive email in DotNET Nunit tests using Selenium and MailSlurp.
How to send emails that don't trigger a spam warning.
What is cc and bcc in email? What is the difference between cc and bcc?
What is BCC email field (blind carbon copy) and how does it differ to CC?
MailServers use TCP ports to transmit emails. Picking the right port between 25 vs 2555, 465 vs 587 in SMTP is easy with this guide.
How to setup MailSlurp with Outlook, Gmail, Thunderbird, Mail app and more.
SMTP mailserver testing and usage in CSharp using
Create a custom SMTP client and access MailSlurp inboxes from CSharp/DotNET.
DMARC – All Facts that you should know
Email size limits
How to manage email subdomain custom mailboxes using SMTP/IMAP mailserver settings.
Send email in Visual basic
A guide to PHP Mailer
How to access MailSlurp email accounts using IMAP and SMTP servers
How to use SMTP relay to address spam and security issues that affect outbound mail servers.
The difference between HTTP and SMTP inboxes and domains in MailSlurp.
What are SMTP ports
How to use CSharp SMTP client to send email with MailSlurp mail server
How to use Java SMTP client to send email with MailSlurp mail server on the JDK
Use PHPMailer to send emails with SMTP and MailSlurp
How to send emails with Python's built-in SMTP client and MailSlurp mail servers.
How to use Ruby SMTP client to send email with the MailSlurp mail servers