Link to home
Start Free TrialLog in
Avatar of Jmjl3
Jmjl3

asked on

E-Mail through VB

Is there a way I could send an e-mail using VB?
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deathman5
deathman5

a very good way is this way:
http://www.winsockvb.com/article.php?article_id=20
hope it helpes
Sub SendNewMail()

On Error GoTo Errorhandler

Dim objEmail As Object
Dim objEmailMsg As Object

Set objEmail = CreateObject("Outlook.Application")
Set objEmailMsg = objEmail.CreateItem(0)

'*** Define E-Mail Elements ***
Dim EmailTo As String
Dim EmailCC As String
Dim EmailSubject As String
Dim EmailBody As String

EmailBody = Variable

EmailSubject = Variable2

'*** Create E-Mail ***
With objEmailMsg
  .To = EmailTo & EmailCC
  .Subject = EmailSubject
  .Body = EmailBody
  .FlagDueBy = dteDateNeeded.Value
  .ReminderTime = dteDateNeeded.Value - 1
  If chkUrgent.Value = True Then .Importance = 2
  '.Display
  .Send
  '.SendKeys ("^~")
End With

Exit Sub
Errorhandler:
 'Error Handler Entry
End Sub
Or you can use this...

---------  Code  ---------

Public Function fhpSendEmail(strRecip As String, strSubject As String, strMsg As String, Optional strCC As String) As Boolean
' ----------------------------------------------------------------------------------- '
' Purpose      : Send an e-mail to an Outlook recipient                              '
' ----------------------------------------------------------------------------------- '
On Error GoTo Error_fhpSendEmail
  Dim strInfoMsg As String                                      'Info message
  Dim objOutlook As Outlook.Application                         'Outlook variable
  Dim objOutlookMsg As Outlook.MailItem                         'Outlook variable
  Dim objOutlookRecip As Outlook.Recipient                      'Outlook variable
         
  If (strRecip <> "") And (strSubject <> "") And (strMsg <> "") Then
    Set objOutlook = CreateObject("Outlook.Application")        'Create the Outlook session
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)       'Create the message

    With objOutlookMsg                                          'The message
      Set objOutlookRecip = .Recipients.Add(strRecip)           'Recipient of the message
      objOutlookRecip.Type = olTo

      If strCC <> "" Then                                       'If CC recipients
        Set objOutlookRecip = .Recipients.Add(strCC)            'CC recipients
        objOutlookRecip.Type = olCC
      End If
   
      .Subject = strSubject                                     'Message subject
      .Body = strMsg & vbCrLf & vbCrLf                          'Message content
      .Importance = olImportanceNormal                          'Message importance

      For Each objOutlookRecip In .Recipients                   'Resolve each Recipient's name
        objOutlookRecip.Resolve
      Next
     
      .Save                                                     'Save message
      .Send                                                     'Send message
    End With
    fhpSendEmail = True                                         'Message sent
  Else                                                          'If not enough information
    strInfoMsg = "Unable to send message because you have supplied to few data!" & vbCrLf & _
                 "Info needed are :" & vbCrLf & _
                 "Recipient" & vbTab & vbTab & "The person to recieve the mail" & vbCrLf & _
                 "Subject" & vbTab & vbTab & "What the mail is about" & vbCrLf & _
                 "Message " & vbTab & vbTab & "The text you want to send"
    MsgBox strInfoMsg, vbCritical + vbOKOnly, "Error sendig e-mail"
    fhpSendEmail = False                                        'Message NOT sent
  End If

Exit_fhpSendEmail:
  Set objOutlook = Nothing                                      'Release object
  Set objOutlookMsg = Nothing                                   'Release object
  Set objOutlookRecip = Nothing                                 'Release object
  Exit Function

Error_fhpSendEmail:
  fhpSendEmail = False                                          'Message NOT sent
  Select Case Err
    Case Else
      MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbCritical, "Error in function 'fhpSendEmail'"
      Resume Exit_fhpSendEmail
  End Select

End Function

-----------  Break  ------------
Add reference to Microsoft CDO for Windows2000 library

Dim Mail As CDO.Message
Mail.To = "blahblah@blahblah.com"
Mail.From = "blahblah@blahblah.com"
Mail.Subject = "blahblah"
Mail.TextBody = "blahblah"
mail.Send