' ***********************************************************************
' Author : Elektro
' Modified : 19-May-2017
' ***********************************************************************
#Region " Public Members Summary "
#Region " Methods "
' SendHotmail(String, String, String, String, String)
' SendHotmail(String, String, String, String, MailAddress)
' SendHotmail(String, String, String, String, String())
' SendHotmail(String, String, String, String, MailAddressCollection)
' SendHotmailAsync(String, String, String, String, String) As Task
' SendHotmailAsync(String, String, String, String, MailAddress) As Task
' SendHotmailAsync(String, String, String, String, String()) As Task
' SendHotmailAsync(String, String, String, String, MailAddressCollection) As Task
#End Region
#End Region
#Region " Option Statements "
Option Strict On
Option Explicit On
Option Infer Off
#End Region
#Region " Imports "
Imports System.Net
Imports System.Net.Mail
Imports System.Threading.Tasks
#End Region
#Region " Mail Util "
Namespace Elektro.Net
Partial Public NotInheritable Class MailUtil
#Region " Public Methods "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Sends a mail through Microsoft Hotmail service.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' SendHotmail("Username@Hotmail.com", "Password", "Email Subject", "Message Body", "Address@Server.com")
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="username">
''' The username of the Hotmail account.
''' </param>
'''
''' <param name="password">
''' The password of the Hotmail account.
''' </param>
'''
''' <param name="subject">
''' The mail subject.
''' </param>
'''
''' <param name="body">
''' The mail body.
''' </param>
'''
''' <param name="address">
''' The target address that will receive our sent mail.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Sub SendHotmail(ByVal username As String,
ByVal password As String,
ByVal subject As String,
ByVal body As String,
ByVal address As String)
MailUtil.SendHotmail(username, password, subject, body, {address})
End Sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Sends a mail through Microsoft Hotmail service.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' SendHotmail("Username@Hotmail.com", "Password", "Email Subject", "Message Body", New MailAddress("Address@Server.com"))
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="username">
''' The username of the Hotmail account.
''' </param>
'''
''' <param name="password">
''' The password of the Hotmail account.
''' </param>
'''
''' <param name="subject">
''' The mail subject.
''' </param>
'''
''' <param name="body">
''' The mail body.
''' </param>
'''
''' <param name="address">
''' The target address that will receive our sent mail.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Sub SendHotmail(ByVal username As String,
ByVal password As String,
ByVal subject As String,
ByVal body As String,
ByVal address As MailAddress)
MailUtil.SendHotmail(username, password, subject, body, {address.Address})
End Sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Sends a mail through Microsoft Hotmail service.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' SendHotmail("Username@Hotmail.com", "Password", "Email Subject", "Message Body", {"Address1@Server.com", "Address2@Server.com"})
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="username">
''' The username of the Hotmail account.
''' </param>
'''
''' <param name="password">
''' The password of the Hotmail account.
''' </param>
'''
''' <param name="subject">
''' The mail subject.
''' </param>
'''
''' <param name="body">
''' The mail body.
''' </param>
'''
''' <param name="addresses">
''' The target addresses that will receive our sent mail.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Sub SendHotmail(ByVal username As String,
ByVal password As String,
ByVal subject As String,
ByVal body As String,
ByVal addresses As String())
Dim addressCollection As New MailAddressCollection
For Each address As String In addresses
addressCollection.Add(New MailAddress(address))
Next address
MailUtil.SendHotmail(username, password, subject, body, addressCollection)
End Sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Sends a mail through Microsoft Hotmail service.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim addressCollection As New MailAddressCollection
''' addressCollection.Add(New MailAddress("Address@Server.com"))
''' addressCollection.Add(New MailAddress("Address2@Server.com"))
''' SendHotmail("Username@Hotmail.com", "Password", "Email Subject", "Message Body", addressCollection)
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="username">
''' The username of the Hotmail account.
''' </param>
'''
''' <param name="password">
''' The password of the Hotmail account.
''' </param>
'''
''' <param name="subject">
''' The mail subject.
''' </param>
'''
''' <param name="body">
''' The mail body.
''' </param>
'''
''' <param name="addresses">
''' The target addresses that will receive our sent mail.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Sub SendHotmail(ByVal username As String,
ByVal password As String,
ByVal subject As String,
ByVal body As String,
ByVal addresses As MailAddressCollection)
Using msg As New MailMessage
For Each address As MailAddress In addresses
msg.To.Add(address)
Next address
msg.From = New MailAddress(username)
msg.Subject = subject
msg.Body = body
Using client As New SmtpClient()
With client
.Host = "smtp.live.com"
.Port = 587
.EnableSsl = True
.DeliveryMethod = SmtpDeliveryMethod.Network
.Timeout = CInt(TimeSpan.FromSeconds(60).TotalMilliseconds)
.Credentials = New NetworkCredential(username, password)
End With
client.Send(msg)
End Using
End Using
End Sub
' #If NET45 Then
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Asynchronously sends a mail through Microsoft Hotmail service.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Await SendHotmailAsync("Username@Hotmail.com", "Password", "Email Subject", "Message Body", "Address@Server.com")
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="username">
''' The username of the Hotmail account.
''' </param>
'''
''' <param name="password">
''' The password of the Hotmail account.
''' </param>
'''
''' <param name="subject">
''' The mail subject.
''' </param>
'''
''' <param name="body">
''' The mail body.
''' </param>
'''
''' <param name="address">
''' The target address that will receive our sent mail.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Async Function SendHotmailAsync(ByVal username As String,
ByVal password As String,
ByVal subject As String,
ByVal body As String,
ByVal address As String) As Task
Await MailUtil.SendHotmailAsync(username, password, subject, body, {address})
End Function
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Asynchronously sends a mail through Microsoft Hotmail service.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Await SendHotmailAsync("Username@Hotmail.com", "Password", "Email Subject", "Message Body", New MailAddress("Address@Server.com"))
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="username">
''' The username of the Hotmail account.
''' </param>
'''
''' <param name="password">
''' The password of the Hotmail account.
''' </param>
'''
''' <param name="subject">
''' The mail subject.
''' </param>
'''
''' <param name="body">
''' The mail body.
''' </param>
'''
''' <param name="address">
''' The target address that will receive our sent mail.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Async Function SendHotmailAsync(ByVal username As String,
ByVal password As String,
ByVal subject As String,
ByVal body As String,
ByVal address As MailAddress) As Task
Await MailUtil.SendHotmailAsync(username, password, subject, body, {address.Address})
End Function
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Asynchronously sends a mail through Microsoft Hotmail service.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Await SendHotmailAsync("Username@Hotmail.com", "Password", "Email Subject", "Message Body", {"Address1@Server.com", "Address2@Server.com"})
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="username">
''' The username of the Hotmail account.
''' </param>
'''
''' <param name="password">
''' The password of the Hotmail account.
''' </param>
'''
''' <param name="subject">
''' The mail subject.
''' </param>
'''
''' <param name="body">
''' The mail body.
''' </param>
'''
''' <param name="addresses">
''' The target addresses that will receive our sent mail.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Async Function SendHotmailAsync(ByVal username As String,
ByVal password As String,
ByVal subject As String,
ByVal body As String,
ByVal addresses As String()) As Task
Dim addressCollection As New MailAddressCollection
For Each address As String In addresses
addressCollection.Add(New MailAddress(address))
Next address
Await MailUtil.SendHotmailAsync(username, password, subject, body, addressCollection)
End Function
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Asynchronously sends a mail through Microsoft Hotmail service.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim addressCollection As New MailAddressCollection
''' addressCollection.Add(New MailAddress("Address@Server.com"))
''' addressCollection.Add(New MailAddress("Address2@Server.com"))
''' Await SendHotmailAsync("Username@Hotmail.com", "Password", "Email Subject", "Message Body", addressCollection)
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="username">
''' The username of the Hotmail account.
''' </param>
'''
''' <param name="password">
''' The password of the Hotmail account.
''' </param>
'''
''' <param name="subject">
''' The mail subject.
''' </param>
'''
''' <param name="body">
''' The mail body.
''' </param>
'''
''' <param name="addresses">
''' The target addresses that will receive our sent mail.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Async Function SendHotmailAsync(ByVal username As String,
ByVal password As String,
ByVal subject As String,
ByVal body As String,
ByVal addresses As MailAddressCollection) As Task
Using msg As New MailMessage
For Each address As MailAddress In addresses
msg.To.Add(address)
Next
msg.From = New MailAddress(username)
msg.Subject = subject
msg.Body = body
Using client As New SmtpClient()
With client
.Host = "smtp.live.com"
.Port = 587
.EnableSsl = True
.DeliveryMethod = SmtpDeliveryMethod.Network
.Timeout = CInt(TimeSpan.FromSeconds(60).TotalMilliseconds)
.Credentials = New NetworkCredential(username, password)
End With
Dim t As Task = client.SendMailAsync(msg)
Await t
End Using
End Using
End Function
' #End If
#End Region
End Class
End Namespace
#End Region