Click here to Skip to main content
13,195,710 members (72,684 online)
Click here to Skip to main content
Add your own
alternative version


72 bookmarked
Posted 1 Oct 2002

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


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, " + _
                   ", subject, message, [smtp Server],"  + _
                   "[;;...], [attachment1;" + _
                        "attachment2;...]" + vbCrLf
            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)) + _
            End If
        End If
        If UBound(strArgs) >= 5 Then
            If Len(Trim(strArgs(5))) > 0 Then
                blnCC = True
                strMSG = strMSG + "  CC          --> " + Trim(strArgs(5)) + _
            End If
        End If
        If UBound(strArgs) >= 6 Then
            If Len(Trim(strArgs(6))) > 0 Then
                blnAttachments = True
                strMSG = strMSG + "  Attachments --> " + Trim(strArgs(6)) + _
            End If
        End If

        'send the email
            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)))
                End If
            End With
            If blnSMTP Then SmtpMail.SmtpServer = Trim(strArgs(4))


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

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

    End Sub


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
Web Developer
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

Questionsmtp mail Pin
nirmalVaishnav29-Jun-17 20:54
professionalnirmalVaishnav29-Jun-17 20:54 
Questionsend me a completly code for smtp mail in Pin
azruddin saiyed1-Jul-15 22:10
memberazruddin saiyed1-Jul-15 22:10 
GeneralExceptions when running from renaming path on Vista? [modified] Pin
John Boster12-Sep-08 16:57
memberJohn Boster12-Sep-08 16:57 
I found a weird issue that seems to only happen on Vista.

If I compile the project and put the exe in c:\epsendmail for example I can run it fine.
However as soon as I rename the directory (i.e. c:\epssendmail2) it will be unable to send email and the error message is "The transport failed to connect to the server."

Now if I move epsendmail.exe out of c:\epsendmail2 and then move it back it'll fix it. I can also copy it out of c:\epsendmail2 and copy it back (overwriting the current epsendmail.exe inside c:\epsendmail2) and it'll work.

If you want to try it yourself here's a quick way to do it (through DOS):

First, put epsendmail.exe in c:\epsendmail

Now open DOS and do:

c:\epsendmail\epsendmail.exe,, test, test,

And it'll send successfully. Now do:

REN c:\epsendmail epsendmail2
c:\epsendmail2\epsendmail.exe,, test, test,

And it'll fail to send. You can try running it as many times as you want but it always fails.

Now if you do:

move c:\epsendmail2\epsendmail.exe c:\
move c:\epsendmail.exe c:\epsendmail2\
c:\epsendmail2\epsendmail.exe,, test, test,

It'll work again. But now if you rename c:\epsendmail2 to c:\epsendmail or whatever it'll break again until you move the executable out and back into the renamed directory.

This all happens with copies too, copy c:\epsendmail to c:\epsendmail2 and the version in c:\epsendmail2 will fail.

Anyone else have this issue on Vista?

I'm wondering if it's firewall or .net/vista security related? After renaming the directory I tried adding the new directory and executable to the windows firewall exception list and it still failed. I also tried disabling the firewall and it failed. The only thing I haven't tried is rebooting after disabling or adding the exception. Could it be some security feature where renaming the dir stops it from having access to networking features? I noticed it gets the error instantly no matter what SMTP server I use. However if I try using an IP address instead of a domain name it just hangs. So it's like it's not even resolving IPs after renaming the directory??

I believe this is just on Vista as I tested it on XP sP3 without issue, however on my XP system I have the firewall turned off. I'll do some testing later on XP with the firewall on and see if it happens.


modified on Friday, September 12, 2008 11:17 PM

GeneralImports System.Web.Mail not found [modified] Pin
Seveley14-Jun-08 6:21
memberSeveley14-Jun-08 6:21 
GeneralRe: Imports System.Web.Mail not found [modified] Pin
dcalvinisti8-Apr-12 13:04
memberdcalvinisti8-Apr-12 13:04 
Generalproblem smtserver Pin
raquidd2218-Mar-08 15:55
memberraquidd2218-Mar-08 15:55 
GeneralRe: problem smtserver Pin
raquidd2218-Mar-08 16:30
memberraquidd2218-Mar-08 16:30 
GeneralGreat Articles Pin
Vuyiswamb21-Oct-07 20:07
memberVuyiswamb21-Oct-07 20:07 
QuestionDo we really need an SMTP Server here ? Pin
Justme4u27-Sep-07 5:04
memberJustme4u27-Sep-07 5:04 
Generalsending mails Pin
divyathannippara18-Mar-07 18:38
memberdivyathannippara18-Mar-07 18:38 
GeneralCDO.Message Error Pin
AndyHug19-Jun-06 22:03
memberAndyHug19-Jun-06 22:03 
QuestionSMTP Server Authentication : How To? Pin
MehdiAnis7-Mar-06 3:23
memberMehdiAnis7-Mar-06 3:23 
AnswerRe: SMTP Server Authentication : How To? Pin
Chris Dufour7-Mar-06 4:03
memberChris Dufour7-Mar-06 4:03 
AnswerRe: SMTP Server Authentication : How To? Pin
MehdiAnis7-Mar-06 9:44
memberMehdiAnis7-Mar-06 9:44 
AnswerRe: SMTP Server Authentication : How To? Pin
Chris Dufour9-Mar-06 3:59
memberChris Dufour9-Mar-06 3:59 
GeneralProb with CDA.Message Pin
Buxo7-Sep-05 23:38
memberBuxo7-Sep-05 23:38 
GeneralRe: Prob with CDA.Message Pin
Z789512313-Sep-05 7:50
sussZ789512313-Sep-05 7:50 
Generalhelp Pin
pradeesh83sharma5-Sep-05 3:49
memberpradeesh83sharma5-Sep-05 3:49 
GeneralRe: help Pin
gthekid26-Jul-07 11:57
membergthekid26-Jul-07 11:57 
GeneralHTML format Pin
Khraeef15-Jul-05 5:22
memberKhraeef15-Jul-05 5:22 
GeneralRe: HTML format Pin
AdamNThompson3-Mar-08 17:19
memberAdamNThompson3-Mar-08 17:19 
GeneralRe: HTML format Pin
Chris_Inman15-Jun-11 13:04
memberChris_Inman15-Jun-11 13:04 
GeneralSMTP Blocked By McAfee Virus Scan 8 Pin
Paul Gunston21-Mar-05 6:01
sussPaul Gunston21-Mar-05 6:01 
QuestionProblem with Import? Pin
Anonymous30-Sep-04 12:19
sussAnonymous30-Sep-04 12:19 
AnswerRe: Problem with Import? Pin
Anonymous14-Jul-05 2:49
sussAnonymous14-Jul-05 2:49 

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

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

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