I've written this simple module that is used to easily drop in e-mail notification functionality into any application. My original version of this utilized application settings on a .NET project however I found this really cumbersome to keep creating each SMTP setting per every project I've worked on. Especially since those cannot be easily copied. I typically use the same NOREPLY e-mail address for each project anyway so I've opted to use an "SMTP.cred" file that stores the e-mail credentials. With this module now I can just drop into the application's directory and also simply add in the module to get my SendEmail() subroutine. Be cautious using this as the SMTP.cred file is not encrypted and will be exposed as plain text. It is best to use this for applications in a managed server environment where the application directory cannot be reached from an average user.
The SMTP.cred file will look like this (The variables can be specified in any order):
SMTPEmail: example@mail.com
SMTPPass: SomePassword
SMTPPort: 587
SMTPSSL: True
SMTPHost: smtp.office365.com
And of course the drop in module:
Imports System.IO Imports System.Net.Mail Module SMTP Public SMTPLoaded As Boolean = False Public SMTPEmail As String = "" Public SMTPPass As String = "" Public SMTPSSL As Boolean = True Public SMTPHost As String = "" Public SMTPPort As Integer = 0 'An SMTP.cred file must be stored in the same folder of the application. Sub New() Dim CredPath As String = Path.Combine(My.Application.Info.DirectoryPath, "SMTP.cred") If File.Exists(CredPath) Then Dim Settings() As String = IO.File.ReadAllLines(CredPath) For Each SettingLine As String In Settings If SettingLine.Contains(":") Then Dim KeyValue() = Strings.Split(SettingLine, ":") If KeyValue.Count <> 2 Then Continue For 'Ignore null values Select Case KeyValue(0).ToUpper Case "SMTPEMAIL" : SMTPEmail = KeyValue(1).Trim Case "SMTPPASS" : SMTPPass = KeyValue(1).Trim Case "SMTPSSL" : If KeyValue(1).ToUpper.Trim = "TRUE" Or KeyValue(1).ToUpper.Trim = "1" Or KeyValue(1).ToUpper.Trim = "T" Then SMTPSSL = True Else SMTPSSL = False Case "SMTPHOST" : SMTPHost = KeyValue(1).Trim Case "SMTPPORT" : SMTPPort = Convert.ToInt32(KeyValue(1).Trim) End Select End If Next If SMTPEmail = "" Or SMTPHost = "" Then Throw New Exception("E-mail Authorization Failure", New Exception("SMTPEmail or SMTPHost is missing from SMTP.cred file")) End If SMTPLoaded = True Else Throw New Exception("E-mail Authorization Failure", New Exception("SMTP.cred file is missing.")) End If End Sub ''' <summary> ''' Sends an e-mail to the specified recipient using the credentials stored in an SMTP.cred file. ''' </summary> ''' <param name="Recipient">The e-mail address to send to</param> ''' <param name="Subject">The subject of the message</param> ''' <param name="Message">The body of the message</param> ''' <param name="AttachmentList">OPTIONAL: Attachment file paths</param> Public Sub SendEmail(ByVal Recipient As String, ByVal Subject As String, ByVal Message As String, Optional ByVal AttachmentList() As String = Nothing) If SMTPLoaded = False Then Throw New Exception("SMTP.cred was not found!") 'LoadSMTPCredentials() 'Needs Imports System.Net.Mail Dim Smtp_Server As New SmtpClient Dim e_mail As New MailMessage() Smtp_Server.UseDefaultCredentials = False Smtp_Server.Credentials = New Net.NetworkCredential(SMTPEmail, SMTPPass) Smtp_Server.Port = SMTPPort Smtp_Server.EnableSsl = SMTPSSL Smtp_Server.Host = SMTPHost Debug.WriteLine("Email sending to " + Recipient + " - " + Subject) e_mail = New MailMessage() e_mail.From = New MailAddress(SMTPEmail) e_mail.To.Add(Recipient) e_mail.Subject = Subject e_mail.IsBodyHtml = False e_mail.Body = Message 'If attachments are listed, attach them to the e-mail If AttachmentList IsNot Nothing Then For Each attachpath As String In AttachmentList.ToList e_mail.Attachments.Add(New Attachment(attachpath)) Next End If Smtp_Server.Send(e_mail) End Sub End Module
Comments
Post a Comment