In previous section, I introduced how to send email using Yahoo account. In this section, I will introduce how to use your Hotmail/MSN Live/Outlook.com/Office365 account to send email in VB.
Sections:
Hotmail/MSN Live/Outlook.com SMTP server address is smtp.office365.com
. It requires TLS connection
to do user authentication, and you should use your Hotmail/MSN Live/Outlook.com email address
as the user name for ESMTP authentication. For example: your email is liveid@hotmail.com
,
and then the user name should be liveid@hotmail.com
.
Server | Port | SSL/TLS |
smtp.office365.com | 25, 587 | TLS |
Note
Remarks: All of samples in this section are based on first section: Send email in A simple VB XAML Windows Store App project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.
The following example codes demonstrate how to use EASendMail SMTP component to send email using Hotmail/MSN Live account. To get the full samples of EASendMail, please refer to Samples section.
' Add EASendMail and Tasks Namespace
Imports EASendMail
Imports System.Threading.Tasks
Public NotInheritable Class MainPage
Inherits Page
Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
End Sub
Private Async Function btnSend_Click(sender As Object, e As RoutedEventArgs) _
As Task Handles btnSend.Click
btnSend.IsEnabled = False
Await Send_Email()
btnSend.IsEnabled = True
End Function
Private Async Function Send_Email() As Task
Dim Result As String = ""
Try
Dim oMail As New SmtpMail("TryIt")
' Set hotmail email address
oMail.From = New MailAddress("liveid@hotmail.com")
' Add recipient email address, please change it to yours
oMail.To.Add(New MailAddress("support@emailarchitect.net"))
' Set email subject
oMail.Subject = "test email from VB XAML project"
' Set email body
oMail.TextBody = "this is a test email sent from Windows Store App using Hotmail."
' Hotmail SMTP server address
Dim oServer As New SmtpServer("smtp.office365.com")
' User and password for Hotmail authentication
oServer.User = "liveid@hotmail.com"
' If you got authentication error, try to create an app password instead of your user password.
' https://support.microsoft.com/en-us/account-billing/using-app-passwords-with-apps-that-don-t-support-two-step-verification-5896ed9b-4263-e681-128a-a6f2979a7944
oServer.Password = "your password or app password"
' Enable TLS connection on 25 port, please add this line
oServer.Port = 25
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
Dim oSmtp As New SmtpClient()
Await oSmtp.SendMailAsync(oServer, oMail)
Result = "Email was sent successfully!"
Catch ep As Exception
Result = String.Format("Failed to send email with the following error: {0}", ep.Message)
End Try
' Display Result by Diaglog box
Dim dlg As New Windows.UI.Popups.MessageDialog(Result)
Await dlg.ShowAsync()
End Function
End Class
If your account enabled two-factor authentication, you cannot login your account by normal user authentication, you should use SMTP OAUTH or App Password.
Microsoft Live SMTP servers (Hotmail, Oultook personal account) have been extended to support authorization via the industry-standard OAuth 2.0 protocol. Using OAUTH protocol, user can do authentication by Microsoft Web OAuth instead of inputting user and password directly in application. This way is more secure, but a little bit complex.
Using Microsoft Hotmail SMTP OAUTH
Or you can generate App Passwords and use this app password instead of your user password.
Office 365 SMTP server uses 587 port and explicit SSL (TLS) connection.
Server | Port | SSL/TLS |
smtp.office365.com | 25, 587 (recommended) | TLS |
If your account enabled two-factor authentication, you cannot login your account by normal user authentication,
you should create an App Passwords and
use this App Password
instead of the user password.
You should also check if authenticated client SMTP submission (SMTP AUTH)
is enabled:
Enable or disable authenticated client SMTP submission (SMTP AUTH) in Exchange Online.
The following example codes demonstrate how to send email using Office 365 account.
Note
To get the full sample projects, please refer to Samples section.
' Add EASendMail and Tasks Namespace
Imports EASendMail
Imports System.Threading.Tasks
Public NotInheritable Class MainPage
Inherits Page
Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
End Sub
Private Async Function btnSend_Click(sender As Object, e As RoutedEventArgs) _
As Task Handles btnSend.Click
btnSend.IsEnabled = False
Await Send_Email()
btnSend.IsEnabled = True
End Function
Private Async Function Send_Email() As Task
Dim Result As String = ""
Try
Dim oMail As New SmtpMail("TryIt")
' Set hotmail email address
oMail.From = New MailAddress("myid@mydomain")
' Add recipient email address, please change it to yours
oMail.To.Add(New MailAddress("support@emailarchitect.net"))
' Set email subject
oMail.Subject = "test email from VB XAML project"
' Set email body
oMail.TextBody = "this is a test email sent from Windows Store App using Office365."
' Office365 SMTP server address
Dim oServer As New SmtpServer("smtp.office365.com")
' User and password for Office365 authentication
oServer.User = "myid@mydomain"
' If you got authentication error, try to create an app password instead of your user password.
' https://support.microsoft.com/en-us/account-billing/using-app-passwords-with-apps-that-don-t-support-two-step-verification-5896ed9b-4263-e681-128a-a6f2979a7944
oServer.Password = "your password or app password"
' Enable TLS connection on 587 port
oServer.Port = 587
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
Dim oSmtp As New SmtpClient()
Await oSmtp.SendMailAsync(oServer, oMail)
Result = "Email was sent successfully!"
Catch ep As Exception
Result = String.Format("Failed to send email with the following error: {0}", ep.Message)
End Try
' Display Result by Diaglog box
Dim dlg As New Windows.UI.Popups.MessageDialog(Result)
Await dlg.ShowAsync()
End Function
End Class
If your account enabled two-factor authentication, you cannot login your account by normal user authentication, you should use SMTP OAUTH or App Password.
Microsoft Office365 EWS/SMTP servers have been extended to support authorization via the industry-standard OAuth 2.0 protocol. Using OAUTH protocol, user can do authentication by Microsoft Web OAuth instead of inputting user and password directly in application. This way is more secure, but a little bit complex.
Using Microsoft Office365 EWS OAUTH
Or you can generate App Passwords and use this app password instead of your user password.
Next Section
At next section I will introduce how to send email with HTML format in VB.
Appendix
Comments
If you have any comments or questions about above example codes, please click here to add your comments.