Solved

E-Mail through VB

Posted on 2003-11-08
8
191 Views
Last Modified: 2010-05-01
Is there a way I could send an e-mail using VB?
0
Comment
Question by:Jmjl3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy a row 12 75
Child Form in front 4 65
Collapse and expand table in Word 2010 2 153
VBA: copy range dynamically based on config sheet v3 6 53
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

739 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