Monday, January 3, 2011

How to send E-Mail from VB 6.0 via Internet

RAM RAM Friends
How to send E-Mail from VB 6.0 via Internet

When I got this work to implement in our project, first I got very excited but when I tried to search this topic on Internet via Google etc, I had to struggle a lot to find the correct method.

Here I would try to explain HOW TO SEND EMAIL FROM VB via INTERNET

Below is a very simple program to explain this


Private Sub cmdSend_Click()
    On Error GoTo ArunKakkarErr
    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    
    iConf.Load -1 ' CDO Source Defaults
    Set Flds = iConf.Fields
    Dim Schemas As String
    'Schemas = "http://schemas.Microsoft.Com/cdo/configuration/" 'causing error as it is very case sensitive
    Schemas = "http://schemas.microsoft.com/cdo/configuration/"
    With Flds
        .Item(Schemas & "smtpusessl") = 1 'True
        .Item(Schemas & "smtpauthenticate") = 1
        .Item(Schemas & "sendusername") = Trim(Me.txtMailId.Text) '"yourmail@gmail.Com"
        .Item(Schemas & "sendpassword") = Trim(Me.txtPwd.Text) '"yourpassword"
        .Item(Schemas & "smtpserver") = Trim(Me.txtSMPT.Text) '"smtp.Gmail.Com" it causes error as it is case sensitive, use "smtp.gmail.com"
        .Item(Schemas & "sendusing") = 2
        .Item(Schemas & "smtpserverport") = 25 '465 
        .Update
    End With
       
    strBody = Trim(Me.txtBody.Text) & vbCrLf & Now  '"Sample message " & Time
    
    With iMsg
        Set .Configuration = iConf
        .To = Trim(Me.txtTo.Text) 'sendto
        .CC = Trim(Me.txtCC.Text) '""
        .BCC = Trim(Me.txtBCC.Text) '""
        ' Note: The reply address is not working if you use this Gmail example
        ' It will use your Gmail address automatic. But you can add this line
        ' to change the reply address .ReplyTo = "Reply@something.Com"
        .From = Trim(Me.txtFrom.Text) '""
        .Subject = Trim(Me.txtSubject.Text) '"subject"
        .TextBody = strBody
        .AddAttachment Trim(Me.txtAttachment.Text)
        .AddAttachment Trim(Me.txtAttachment2.Text)
        .Send
    End With
    MsgBox ("Mail has been sent successfully, Please check the mail if you have any doubt.")

    Exit Sub
ArunKakkarErr:
    MsgBox "There is an error while sending mail. And details are =" & Err.Description
End Sub

Important Notes:
  1. Please Allow this program to access Internet in your Firewall setting or Antivirus program if they ask for it else you can't send the email.
  2. Your program may take time while sending email so make proper coding in your program so that user may know that your program is busy. ( e.g  by changing your mouse pointer etc)
  3. Please take the commented notes given in above code very seriously to get proper result.
Good luck for programming.

4 comments:

  1. Very Very Useful Tips...Thanks very much

    God Bless You and Share Yr Knowledge forever

    ReplyDelete
    Replies
    1. Thanks dear, I just love to share my knowledge, & whenever I got sufficient time, I would love to post many more such useful posts

      Delete
  2. this really really nice sir.....it was very usefull u r genious sir!! thanks a ton sir!!

    ReplyDelete
  3. Thanks for good response. great...

    dear sir ,

    I have one vb data entry form this form have Three fields for exam ID number, name, mobile no. i have saved in access data base. I want know this three fields send to my client email id. kindly help me please

    ReplyDelete