'-------------------------------------------------------------------------------------------------- ' DESCRIPTION: Sample code for sending email in .NET using Visual Basic (VB.NET). ' SUMMARY: The sample will send a plain-text email, with an image file attached. ' REFERENCE: MSDN System.Net.Mail Namespace ' http://msdn.microsoft.com/en-us/library/System.Net.Mail(v=vs.90).aspx ' ' DISCLAIMER: This sample code is intended for reference only. Just because this worked ' for me, you must assume that this code will probably turn your computer into ' a puddle of molten silicon, or render your system inoperable requiring a full ' re-install. I can not be held liable for damage done to your system by using ' any of this sample. ' DEVELOPED BY: Jay R. Ohman, Ohman Automation Corp. - www.OhmanCorp.com ' Please contact me through the website with any comments or corrections. ' ' USAGE NOTES: For this to work in your environment, be sure to set approprate values for: ' Recipients, AttachmentFileName, Server, Port, UserName, Password ' The FromAddress can be anything, something legitimate is recommended. '-------------------------------------------------------------------------------------------------- Imports System.Net.Mail Console.WriteLine("Results of TestEmail: " & TestEmailSend()) Function TestEmailSend() As String Dim EMsg As New MailMessage() Dim FromAddress As String = "Script Smith " Dim ReplyTo As String = Nothing Dim Recipients As String = "me@contoso.com" Dim CC As String = Nothing Dim bcc As String = Nothing Dim Subject As String = "Test email" Dim Body As String = "This is a test." & vbCrLf & "This email can be deleted." Dim AttachmentFileName As String = "C:\SampleImage.jpg" Dim Server As String = "smtp.adventure-works.com" Dim Port As Integer = 587 Dim SSL As Boolean = False Dim UserName As String = "admin@adventure-works.com" Dim Password As String = "qwerty" Try Dim SMTPServer As New SmtpClient If (IsNothing(FromAddress)) Then Return "Error: FromAddress must be specified." : Exit Function EMsg.From = New MailAddress(FromAddress) Console.WriteLine("TestEmail From, Address: " & EMsg.From.Address & _ ", DisplayName: " & EMsg.From.DisplayName) ' obsolete with .NET 4.0, use ReplyToList If (Not IsNothing(ReplyTo)) Then EMsg.ReplyTo = New MailAddress(ReplyTo) If (IsNothing(Recipients)) And (IsNothing(CC)) And (IsNothing(BCC)) Then Return "Error: no recipients specified." : Exit Function End If If (Not IsNothing(Recipients)) Then For Each ax As String In Split(Recipients, ",") EMsg.To.Add(ax) Next End If If (Not IsNothing(CC)) Then For Each ax As String In Split(CC, ",") EMsg.CC.Add(ax) Next End If If (Not IsNothing(BCC)) Then For Each ax As String In Split(BCC, ",") EMsg.Bcc.Add(ax) Next End If EMsg.Subject = Subject EMsg.Body = Body If (Not IsNothing(AttachmentFileName)) Then ' did not bother with multiple attachments If (Not System.IO.File.Exists(AttachmentFileName)) Then Return "Error: attachment file specified does not exist: " & AttachmentFileName Exit Function End If EMsg.Attachments.Add(New Attachment(AttachmentFileName)) End If If (IsNothing(Server)) Then Return "Error: SMTPServer must be specified." : Exit Function SMTPServer.Host = Server If (IsNothing(Port)) Then SMTPServer.Port = 25 Else SMTPServer.Port = Port If (Not IsNothing(UserName)) And (Not IsNothing(Password)) Then _ SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password) If (IsNothing(SSL)) Then SMTPServer.EnableSsl = False Else SMTPServer.EnableSsl = SSL SMTPServer.Send(EMsg) EMsg.Dispose() Return "Email To: " & Recipients.ToString & ", From: " & FromAddress & " sent successfully." Catch ex As ArgumentOutOfRangeException EMsg.Dispose() Return "Error ArgumentOutOfRangeException: #" & Err.Number & ", " & ex.Message Catch Ex As InvalidOperationException EMsg.Dispose() Return "Error InvalidOperationException: #" & Err.Number & ", " & Ex.Message Catch Ex As SmtpFailedRecipientsException EMsg.Dispose() Return "Error SmtpFailedRecipientsException: #" & Err.Number & ", " & Ex.Message Catch ex As SmtpException EMsg.Dispose() Return "Error SmtpException: #" & Err.Number & ", " & ex.Message Catch ex As Exception EMsg.Dispose() Return "Error: #" & Err.Number & ", " & ex.Message End Try End Function