Solved

E-Mail through VB

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

932 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

11 Experts available now in Live!

Get 1:1 Help Now