Solved

E-Mail through VB

Posted on 2003-11-08
8
186 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now