Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 880
  • Last Modified:

Send Email

How do you use vb to send an email message. I know one way to do it but i dont want anything to pop up (Like the outlook notice).  I want it so the user would not even notice it being sent.  
1 Solution
The easiest way to send emails or SMS through VB is by using vbSendMail DLL that can be freely downloaded from this site:

However, there is no way you can stop user from noticing it. Although this DLL wont flash a message, but the user will know one way or the other that a mail is being sent.
You can send e-mail by using the winsock control.
the user would not notice it.
Add the winsock control on a form named sckSMTP
Here is the code to do so

Dim ToAdd As String
Dim CCAdd As String
Dim BCCAdd As String
Dim FromAdd As String
Dim Subject As String
Dim msg As String
Dim SenderDomain As String
Dim intWait As Integer
Dim SignedOut As Boolean
Dim MsgSubject, MsgMessage as string

Function SendMail()
    SMTPAddress = "smtp.domain.com"
    MsgSubject = "subject"
    MsgMessage = "Message"
    frmMain.sckSMTP.Connect SMTPAddress, 25
    Do Until frmMain.sckSMTP.State = sckClosed
        If frmMain.sckSMTP.State = sckError Then
             Exit Function
        end if
End Function

Private Sub sckSMTP_DataArrival(ByVal bytesTotal As Long)
Dim DatRec As String
Dim strBuffer As String
Dim Filenum As Integer
Dim i As Integer
Dim TempPath As String
Dim PrevCommand As String
Dim IsSMTPConnected As Boolean
Dim SMTPAdd As String

SMTPDomain = "domain.com"
'sender E-mailname
EmailUsername = "name@domain.com"
EmailPass = "password"

sckSMTP.GetData DatRec

'For intercepting SMTP data

Select Case Val(Left$(DatRec, 3))

Case 220
 Debug.Print sckSMTP.State
 If Not IsSMTPConnected Then
  IsSMTPConnected = True
  'Notify address
  smtpSendData "HELO <" & SMTPDomain & ">" & vbCrLf 'smtp.
 End If

Case 250

 Select Case PrevCommand
 Case "HELO"
  'Send the sender address..
  smtpSendData "MAIL FROM: <" & EmailUsername & ">" & _ vbCrLf ' & EmailUsername & Mid$(SMTPAddress, 6) & ">" & _ vbCrLf

 Case "MAIL"
    Dim strTo(0 To 50) As String
    Dim TempChar As String
    Dim CountAdress As Integer
    CountAdress = 0
    If InStr(1, EmailTo, ";", vbTextCompare) Then
       For X = 1 To Len(EmailTo)
           TempChar = Mid(EmailTo, X, 1)
           If TempChar = ";" Then
               CountAdress = CountAdress + 1
               strTo(CountAdress) = strTo(CountAdress) & TempChar
           End If
       Next X
       For Y = 0 To CountAdress
           smtpSendData "RCPT TO: <" & Trim$(strTo(Y)) & ">" & vbCrLf
       Next Y
     smtpSendData "RCPT TO: <" & Trim$(EmailTo) & ">" & vbCrLf
    End If
 Case "RCPT"

  smtpSendData "DATA" & vbCrLf
 Case "VRFY"
  IsVerified = True
 Case "DATE"
  'Email sent.
  'CLose the connection
  IsSMTPConnected = False
  'Sign Out from Server
 End Select
Case 251
 Select Case PrevCommand
 Case "RCPT"
  smtpSendData "DATA" & vbCrLf
 End Select
Case 354

 Select Case PrevCommand
  Case "DATA"

   'Server ready for message.Compose the message.
   msg = "DATE: " & Date & vbCrLf & "FROM: " & EmailUsername & vbCrLf & "TO: " & EmailTo & vbCrLf & "SUBJECT: " & MsgSubject & vbCrLf & vbCrLf & MsgMessage & vbCrLf & "." & vbCrLf
   smtpSendData msg
 End Select
Case Is >= 400
 MsgBox Mid$(DatRec, 4), vbInformation, "Error in Email Transaction"
 'Close socket
 If POPAuthorised Then POPAuthorised = False
 IsSMTPConnected = False
End Select

End Sub

Private Sub smtpSendData(strMessage As String)

PrevCommand = Left$(Trim$(strMessage), 4)

sckSMTP.SendData strMessage

End Sub

allhailcaesarAuthor Commented:
There is something wrong with this code
It is red

smtpSendData "MAIL FROM: <" & EmailUsername & ">" & _ vbCrLf ' & EmailUsername & Mid$(SMTPAddress, 6) & ">" & _ vbCrLf
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Install IIS on NT/2k/XP (on NT it is in the NT options pack, or in 2k/xp under add/remove programs, windows components)

Include Microsoft CDO in your project references.

then use this for a base;

    Dim mail As New NewMail
    mail.From = "someone@somewhere.com"
    mail.To = "someone@somewhere.com"
    mail.Subject = "some subject"
    mail.Body = "some body text"
The line must be :

smtpSendData "MAIL FROM: <" & EmailUsername & ">" &  vbCrLf  & EmailUsername & Mid$(SMTPAddress, 6) & ">" &  vbCrLf
i do beleive there is a smtp ocx hing in vb6 under the toolbar components list
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
Moderator, my recommended disposition is:

    Accept Sethi's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now