Click here to Skip to main content
6,596,602 members and growing! (22,006 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate

Send SMTP mail using VB.NET

By Chris Dufour

A console application to send SMTP mail
VB.NET 1.0, Win2K, WinXP, Dev
Posted:1 Oct 2002
Views:268,046
Bookmarked:55 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
21 votes for this article.
Popularity: 4.57 Rating: 3.46 out of 5
2 votes, 11.8%
1
1 vote, 5.9%
2

3
6 votes, 35.3%
4
8 votes, 47.1%
5

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

About the Author

Chris Dufour


Member

Occupation: Web Developer
Location: Canada Canada

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralExceptions when running from renaming path on Vista? [modified] PinmemberJohn Boster17:57 12 Sep '08  
GeneralImports System.Web.Mail not found [modified] PinmemberSeveley7:21 14 Jun '08  
Generalproblem smtserver Pinmemberraquidd2216:55 18 Mar '08  
GeneralRe: problem smtserver Pinmemberraquidd2217:30 18 Mar '08  
GeneralGreat Articles PinmemberVuyiswamb21:07 21 Oct '07  
GeneralDo we really need an SMTP Server here ? PinmemberJustme4u6:04 27 Sep '07  
Generalsending mails Pinmemberdivyathannippara19:38 18 Mar '07  
GeneralCDO.Message Error PinmemberAndyHug23:03 19 Jun '06  
QuestionSMTP Server Authentication : How To? PinmemberMehdiAnis4:23 7 Mar '06  
AnswerRe: SMTP Server Authentication : How To? PinmemberChris Dufour5:03 7 Mar '06  
AnswerRe: SMTP Server Authentication : How To? PinmemberMehdiAnis10:44 7 Mar '06  
AnswerRe: SMTP Server Authentication : How To? PinmemberChris Dufour4:59 9 Mar '06  
GeneralProb with CDA.Message PinmemberBuxo0:38 8 Sep '05  
GeneralRe: Prob with CDA.Message PinsussZ78951238:50 13 Sep '05  
Generalhelp Pinmemberpradeesh83sharma4:49 5 Sep '05  
GeneralRe: help Pinmembergthekid12:57 26 Jul '07  
GeneralHTML format PinmemberKhraeef6:22 15 Jul '05  
GeneralRe: HTML format PinmemberAdamNThompson18:19 3 Mar '08  
GeneralSMTP Blocked By McAfee Virus Scan 8 PinsussPaul Gunston7:01 21 Mar '05  
GeneralProblem with Import? PinsussAnonymous13:19 30 Sep '04  
GeneralRe: Problem with Import? PinsussAnonymous3:49 14 Jul '05  
GeneralUBound Pinmemberabuhun18:27 23 Feb '04  
GeneralRe: UBound PinsussAnonymous1:47 17 Dec '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Oct 2002
Editor: Nishant Sivakumar
Copyright 2002 by Chris Dufour
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project