VB Send Email


This article is written by Pon Saravanan  on 08-Dec-09 Last modified on :10-Jun-11





Tutorial on Sending Email

If you are still working in classic VB, sending email is not as easy as we do in asp.net. Especially for notifications emails are one of the best ways to communicate. Normally the SMTP server will be in separate email hosting server reachable from application server. In this scenario we need to configure CDO to use our SMTP server. Then only the mail relay will be successful.

The function we are discussing will support most of the email functionalities like CC, BCC and attachment.
 
The function will return the status about sent. Since this function is designed for applications, the related settings can be fetched from settings file rather than changing the code and re compiling to take changes.

Basically the following configurations are necessary before sending mail with CDO object
•  CDO.Message
• CDO.Configuration

CDO.Message

This object is used to set the various properties like following

• To
• From
• Subject
• TextBody
• Attachment
• CC
• BCC
Finally we can call the Send method to send the mail. 

CDO.Configuration

This object is used to set the configurations like
• Send using
• SMTP server
• SMTP server port

To resolve "The transport failed to connect to the server"

Please change the respective SMTP server in the "change your SMTP server here" section. Or else you will get the "The transport failed to connect to the server." error message. 

Most of my applications are using this function which needs SMTP Authentication. In order for this function to work, you must have a valid SMTP server with a valid user name and password.

It happened to me that my user account was locked or grey listed once since I have tried several times with bad password. Then the setting won’t work for a while until it gets released. So it is better if you have any doubts, use any utilities which may help to test the email before using this function. So make sure you have valid credentials and information regarding the SMTP server before using this function.

Source Code

Private Sub Command1_Click()
    Dim colAtt As New Collection
    colAtt.Add "c:\boot.ini"
    colAtt.Add "c:\boot.bak"
    MsgBox SendEmail("name1@server.comccname1@server.com", "test boot", "Get Boot.ini", "name2@server.com", "ccname2@server.com4", colAtt)
End Sub
'****************************************************************
'*  Purpose :   To Send eMail
'*
'*  Inputs  :   strRecipient(String)    Recipient comma seperated
'*              strSubject(String)      Subject
'*              strBody                  Body
'*              colAttachments          Collection of attachments
'*                                      file paths.
'*
'*  Returns :   Boolean about the sent status
'****************************************************************
Public Function SendEmail(ByVal strSender As String, _
                        ByVal strRecipient As String, _
                        ByVal strSubject As String, _
                        ByVal strBody As String, _
                        Optional ByVal strCc As String, _
                        Optional ByVal strBcc As String, _
                        Optional ByVal colAttachments As Collection _
                         ) As Boolean
    Dim cdoMsg As New CDO.Message
    Dim cdoConf As New CDO.Configuration
    Dim Flds
    Dim attachment
    Dim strHTML
   
    On Error GoTo ErrTrap
    Const cdoSendUsingPort = 2
   
    'Set cdoMsg =  CreateObject("CDO.Message")
    'Set cdoConf = CreateObject("CDO.Configuration")
   
    Set Flds = cdoConf.Fields
       
    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 '
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "change your smtp server here"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Update
    End With
   
   
    ' Apply the settings to the message.
    With cdoMsg
        Set .Configuration = cdoConf
        .To = strRecipient
        .From = strSender
        .Subject = strSubject
        .TextBody = strBody
        If Not colAttachments Is Nothing Then
            For Each attachment In colAttachments
                .AddAttachment attachment
            Next
        End If
        If strCc <> "" Then .CC = strCc
        If strBcc <> "" Then .BCC = strBcc
        .Send
    End With
   
    Set cdoMsg = Nothing
    Set cdoConf = Nothing
    Set Flds = Nothing
       
    SendEmail = True
    Exit Function
ErrTrap:
Err.Raise Err.Number, "", "Error from Functions.SendEmail" & Err.Description
    SendEmail = False
End Function



« Previous - Export CSV from Dataset
- Next »







Comments
  • GUEST
    9/12/2011 4:03:53 AM

  • GUEST
    Thanks For tutorials 9/16/2011 9:20:17 PM

  • GUEST
    Thanks for your great tutorial.

    http://tec-world.info
    12/2/2011 11:21:03 AM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)