Click here to Skip to main content
11,432,119 members (54,022 online)
Click here to Skip to main content

Send SMTP mail using VB.NET

, 1 Oct 2002
Rate this:
Please Sign up or sign in to vote.
A console application to send SMTP mail

Introduction

I needed a way to send a smtp mail message from a command prompt to notify me when one our servers failed over using Double-Take.

This article demonstates sending a smtp mail message from the command prompt

    
Imports System.Web.Mail
    
Sub Main()
        Dim strMSG As String
        Dim strArgs() As String = Command.Split(",")
        Dim blnSMTP As Boolean = False
        Dim blnCC As Boolean = False
        Dim blnAttachments As Boolean = False

        'get the product name, version and description from the assembly
        strMSG = vbCrLf + vbCrLf + _
          System.Diagnostics.FileVersionInfo.GetVersionInfo( _ 
      System.Reflection.Assembly.GetExecutingAssembly.Location).ProductName _
              + " v" + System.Diagnostics.FileVersionInfo.GetVersionInfo( _
      System.Reflection.Assembly.GetExecutingAssembly.Location _
            ).ProductVersion + vbCrLf + _
         System.Diagnostics.FileVersionInfo.GetVersionInfo( _
         System.Reflection.Assembly.GetExecutingAssembly.Location _
              ).Comments + vbCrLf + vbCrLf

        If UBound(strArgs) < 3 Then
            strMSG = strMSG + "Usage: EPSendMail from@email.com, " + _
                   "to@email.com, subject, message, [smtp Server],"  + _
                   "[cc1@email.com;cc2@email.com;...], [attachment1;" + _
                        "attachment2;...]" + vbCrLf
            Console.Write(strMSG)
            Exit Sub
        End If

        strMSG = strMSG + "Sending email message" + vbCrLf + _
            "  From        --> " + Trim(strArgs(0)) + vbCrLf + _
            "  To          --> " + Trim(strArgs(1)) + vbCrLf + _
            "  Subject     --> " + Trim(strArgs(2)) + vbCrLf + _
            "  Message     --> " + Trim(strArgs(3)) + vbCrLf
        If UBound(strArgs) >= 4 Then
            If Len(Trim(strArgs(4))) > 0 Then
                blnSMTP = True
                strMSG = strMSG + "  SMTP Server --> " + Trim(strArgs(4)) + _
                   vbCrLf
            End If
        End If
        If UBound(strArgs) >= 5 Then
            If Len(Trim(strArgs(5))) > 0 Then
                blnCC = True
                strMSG = strMSG + "  CC          --> " + Trim(strArgs(5)) + _
                   vbCrLf
            End If
        End If
        If UBound(strArgs) >= 6 Then
            If Len(Trim(strArgs(6))) > 0 Then
                blnAttachments = True
                strMSG = strMSG + "  Attachments --> " + Trim(strArgs(6)) + _
                   vbCrLf
            End If
        End If
        Console.Write(strMSG)

        'send the email
        Try
            Dim insMail As New MailMessage()
            With insMail
                .From = Trim(strArgs(0))
                .To = Trim(strArgs(1))
                .Subject = Trim(strArgs(2))
                .Body = Trim(strArgs(3))
                If blnCC Then .Cc = Trim(strArgs(5))
                If blnAttachments Then
                    Dim strFile As String
                    Dim strAttach() As String = Split(strArgs(6), ";")
                    For Each strFile In strAttach
                        .Attachments.Add(New MailAttachment(Trim(strFile)))
                    Next
                End If
            End With
            If blnSMTP Then SmtpMail.SmtpServer = Trim(strArgs(4))

            SmtpMail.Send(insMail)

            Console.WriteLine("Successfully sent email message" + vbCrLf)

        Catch err As Exception
            Console.WriteLine("EXCEPTION " + err.Message + vbCrLf)
        End Try

    End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Chris Dufour
Web Developer
Canada Canada
No Biography provided

Comments and Discussions

 
GeneralExceptions when running from renaming path on Vista? [modified] Pin
John Boster12-Sep-08 17:57
memberJohn Boster12-Sep-08 17:57 
GeneralImports System.Web.Mail not found [modified] Pin
Seveley14-Jun-08 7:21
memberSeveley14-Jun-08 7:21 
GeneralRe: Imports System.Web.Mail not found [modified] Pin
dcalvinisti8-Apr-12 14:04
memberdcalvinisti8-Apr-12 14:04 
Try changing your imports to Imports System.Net.Mail... it should work.
Generalproblem smtserver Pin
raquidd2218-Mar-08 16:55
memberraquidd2218-Mar-08 16:55 
GeneralRe: problem smtserver Pin
raquidd2218-Mar-08 17:30
memberraquidd2218-Mar-08 17:30 
GeneralGreat Articles Pin
Vuyiswamb21-Oct-07 21:07
memberVuyiswamb21-Oct-07 21:07 
QuestionDo we really need an SMTP Server here ? Pin
Justme4u27-Sep-07 6:04
memberJustme4u27-Sep-07 6:04 
Generalsending mails Pin
divyathannippara18-Mar-07 19:38
memberdivyathannippara18-Mar-07 19:38 
GeneralCDO.Message Error Pin
AndyHug19-Jun-06 23:03
memberAndyHug19-Jun-06 23:03 
QuestionSMTP Server Authentication : How To? Pin
MehdiAnis7-Mar-06 4:23
memberMehdiAnis7-Mar-06 4:23 
AnswerRe: SMTP Server Authentication : How To? Pin
Chris Dufour7-Mar-06 5:03
memberChris Dufour7-Mar-06 5:03 
AnswerRe: SMTP Server Authentication : How To? Pin
MehdiAnis7-Mar-06 10:44
memberMehdiAnis7-Mar-06 10:44 
AnswerRe: SMTP Server Authentication : How To? Pin
Chris Dufour9-Mar-06 4:59
memberChris Dufour9-Mar-06 4:59 
GeneralProb with CDA.Message Pin
Buxo8-Sep-05 0:38
memberBuxo8-Sep-05 0:38 
GeneralRe: Prob with CDA.Message Pin
Z789512313-Sep-05 8:50
sussZ789512313-Sep-05 8:50 
Generalhelp Pin
pradeesh83sharma5-Sep-05 4:49
memberpradeesh83sharma5-Sep-05 4:49 
GeneralRe: help Pin
gthekid26-Jul-07 12:57
membergthekid26-Jul-07 12:57 
GeneralHTML format Pin
Khraeef15-Jul-05 6:22
memberKhraeef15-Jul-05 6:22 
GeneralRe: HTML format Pin
AdamNThompson3-Mar-08 18:19
memberAdamNThompson3-Mar-08 18:19 
GeneralRe: HTML format Pin
Chris_Inman15-Jun-11 14:04
memberChris_Inman15-Jun-11 14:04 
GeneralSMTP Blocked By McAfee Virus Scan 8 Pin
Paul Gunston21-Mar-05 7:01
sussPaul Gunston21-Mar-05 7:01 
QuestionProblem with Import? Pin
Anonymous30-Sep-04 13:19
sussAnonymous30-Sep-04 13:19 
AnswerRe: Problem with Import? Pin
Anonymous14-Jul-05 3:49
sussAnonymous14-Jul-05 3:49 
GeneralUBound Pin
abuhun23-Feb-04 18:27
memberabuhun23-Feb-04 18:27 
GeneralRe: UBound Pin
Anonymous17-Dec-04 1:47
sussAnonymous17-Dec-04 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 2 Oct 2002
Article Copyright 2002 by Chris Dufour
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid