Solved

E-Mail through VB

Posted on 2003-11-08
8
188 Views
Last Modified: 2010-05-01
Is there a way I could send an e-mail using VB?
0
Comment
Question by:Jmjl3
8 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 25 total points
ID: 9707205
Have you tried the popular vbSendmail component?

http://www.freevbcode.com/ShowCode.Asp?ID=109
0
 

Expert Comment

by:deathman5
ID: 9707268
a very good way is this way:
http://www.winsockvb.com/article.php?article_id=20
hope it helpes
0
 
LVL 1

Expert Comment

by:gideonn
ID: 9707363
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
0
 
LVL 1

Expert Comment

by:gideonn
ID: 9707368
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  ------------
0
 

Expert Comment

by:Hasan_Khan
ID: 9707478
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
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question