Solved

Automated Email within VB

Posted on 2001-06-06
5
236 Views
Last Modified: 2010-05-02
I am urgently looking for code that is able to send automatic email in VB6 when certain conditions are met, example is where stock level drops below a minimum quantity. I want it to happen automatic without the users knowing it.

Thanks
0
Comment
Question by:Stars
[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
5 Comments
 
LVL 3

Expert Comment

by:Maxim10553
ID: 6159693
use the SMTP control that ships with VB6

if stock1.level < 50 then
'IF YOUR CONDTION ARE MET THEN SEND THE EMAIL, you must fill in the info
 With SMTP
        .Server = txtServer
        .MailFrom = txtMailFrom
        .SendTo = txtSendTo
        .MessageSubject = txtMessageSubject
        .MessageText = txtMessageText
        .Connect
    End With
end if
0
 
LVL 4

Expert Comment

by:rallsaldo
ID: 6159998
Alternatively, if you are using Outlook as your e-mail program, Create a Reference to Outlook 98 Library, and use the following:

This button event simply sets the recipient and calls the sub. Obviously for your use you would be testing the stock level (on a timer say) and then calling the sub.

Private Sub Command1_Click()

    Dim strTo() As String
    Dim strFile() As String
   
    'For the purpose of this I have set the recipient tojust one address and no file attachments, ccs or bccs

    ReDim strTo(0)
    ReDim strFile(0)
   
    strTo(0) = "email@email.com" 'set this to who you want the e-mail recipient to be
   
    Call SendMessage("Subject", "", "", strTo(), "Body", strFile(), True)

End Sub


Sub SendMessage(strSubject As String, strCopyTo As String, strBCCTo As String, _
                strRecipient() As String, strBody As String, strFile() As String, _
                DisplayMsg As Boolean, Optional AttachmentPath)
         
          Dim objOutlook As Outlook.Application
          Dim objOutlookMsg As Outlook.MailItem
          Dim objOutlookRecip As Outlook.Recipient
          Dim objOutlookAttach As Outlook.Attachment
          Dim intCounter As Integer
         
          'Start Outlook
          Set objOutlook = CreateObject("Outlook.Application")
         
          'This is the message section
          Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
          With objOutlookMsg
             
                For intCounter = 0 To UBound(strRecipient)
                    If strRecipient(intCounter) <> "" Then
                        ' Add the To recipient(s) to the message.
                        Set objOutlookRecip = .Recipients.Add(strRecipient(intCounter))
                        objOutlookRecip.Type = olTo
                    End If
                Next intCounter
                                 
            ' Add the CC recipient(s) to the message.
            If strCopyTo <> "" Then
                Set objOutlookRecip = .Recipients.Add(strCopyTo)
                objOutlookRecip.Type = olCC
            End If
             
            ' Add the BCC recipient(s) to the message.
            If strBCCTo <> "" Then
                Set objOutlookRecip = .Recipients.Add(strBCCTo)
                objOutlookRecip.Type = olBCC
            End If
             
            ' Set the Subject, Body, and Importance of the message.
            .Subject = strSubject
            .Body = strBody & vbCrLf & vbCrLf
             .Importance = olImportanceNormal 'Normal importance

             ' Add attachments to the message.
             For intCounter = 0 To UBound(strFile)
                    If Not IsMissing(AttachmentPath) Then
                        Set objOutlookAttach = .Attachments.Add(strFile(intCounter))
                    End If
             Next intCounter
             
             ' Resolve each Recipient's name.
             For Each objOutlookRecip In .Recipients
                 objOutlookRecip.Resolve
             Next
                       
             ' Should we display the message before sending?
             If DisplayMsg Then
               
                .Display
            Else
                .Send
           
            End If
           
            End With
         
          Set objOutlook = Nothing
End Sub

I would then have a PC which is running the code and is using outlook.

Hope this helps.
R

0
 
LVL 20

Accepted Solution

by:
hes earned 50 total points
ID: 6160013
When you reach where you want to send the email try using this. It uses the Microsoft Mapi controls

'Name the MapiSession object MapiSess
'Name the MapiMessage object MapiMess



Const vbsname As String = "SMTP:"
' Prefix for MS EXCHANGE User Provider
Const vbExchange As String = "MS Exchange Settings"
' Email Subject
Const vbMsgSubject1 As String = "Stock level has dropped below  minimum quantity"
' Email Text if any
Const vbMsgText As String = "Stock level has dropped below  minimum quantity "
' Setup Send Email Constants
Const vbMessageSend = 3
Const vbMessageResolveName = 13

MapiSess.UserName = vbExchange
MapiSess.SignOn

'Format Message Body
MapiMess.MsgIndex = -1
MapiMess.SessionID = MapiSess.SessionID
MapiMess.MsgSubject = vbMsgSubject1
MapiMess.MsgNoteText = vbMsgText
MapiMess.RecipIndex = 0
MapiMess.RecipType = 1
MapiMess.RecipDisplayName = "John Doe"
MapiMess.RecipAddress = "JohnDoe@Anywhere.com"
       
MapiMess.AddressResolveUI = True
MapiMess.Action = vbMessageResolveName

' Send Email
MapiMess.Action = vbMessageSend

' Logoff Email System
MapiSess.Action = 2

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6160481
You can use vbSendMail downloadable from http://www.freevbcode.com/ShowCode.Asp?ID=109
0
 

Author Comment

by:Stars
ID: 6162868
What do I need to get the code to run. I am getting a Run-time error "424'."Object required" Which object is the code looking for.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

617 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