Results 1 to 5 of 5

Thread: Email from VB (MAPI)

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2001
    Posts
    40

    Email from VB (MAPI)

    I have a question regarding sending email from a VB program. I read up on the MAPI, and was able to add the controls to my project and send a message. Here are the problems. It appears to be sending the mail through outlook (which is OK with me), but it pops up a message saying that my program is attempting to send a message automatically and asks if it is OK. While I understand the need for this dialog box (security), my app needs to be able to email when certain events occur, so I can't have that message pop up.

    The second issue is that when I issue the send command, the message is not actually sent, but in fact placed in the outbox of Outlook. It is not actually sent until after I start Outlook. At that time it is processed as it receives my mail that I check with outlook. Since there won't be a person sitting at this machine this is not a viable option.

    Any suggestions are greatly appreciated.

    Kevin

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Couple of suggestions. For number one, here is some code you can implment to automatically and unattended, close the message box.
    VB Code:
    1. 'place in a module
    2. Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    3. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Public Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    5. Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
    6. Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hWnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
    7. Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
    8.  
    9. Public Const NV_CLOSEMSGBOX As Long = &H5000&
    10. Public Const API_FALSE As Long = 0&
    11.  
    12.  
    13. Public Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    14. KillTimer hWnd, idEvent
    15. Dim hMessageBox As Long
    16. 'Replace 'Self Closing Message Box' with the title you gave to your message box.
    17. hMessageBox = FindWindow("#32770", "Self Closing Message Box")
    18. If hMessageBox Then
    19.      Call SetForegroundWindow(hMessageBox)
    20.      SendKeys "{enter}"
    21. End If
    22. Call LockWindowUpdate(API_FALSE)
    23. End Sub
    24. 'place on your form
    25. Private Sub Command1_Click()
    26. 'Replace the '4000' below with the number of milliseconds the message box
    27. 'will appear. 1000 milliseconds = 1 second
    28. SetTimer hWnd, NV_CLOSEMSGBOX, 4000&, AddressOf TimerProc
    29. Call MessageBox(hWnd, "Watch this message box close itself after four seconds", _
    30. "Self Closing Message Box", vbInformation)
    31. End Sub
    For number two, you could replace your MAPI code with this.
    VB Code:
    1. Option Explicit
    2. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    3. Private Const SW_SHOW = 5
    4.  
    5. Private Sub Command1_Click()
    6. Dim MySubject As String
    7. Dim MyMessage As String
    8.  
    9. MySubect = "Give this a shot"
    10. MyMessage = "This will open the default mail for the user, and pop the message off."
    11.  
    12. ShellExecute hwnd, "Open", "mailto:[email protected]?subject=MySubject&body=MyMessage, vbNullString, vbNullString, SW_SHOW

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2001
    Posts
    40
    Thank you for the suggestions. I tried the second one, and it wouldn't compile (I think a misplaced quote), but I modified it so it would run(Although I'm not sure if it was the right modification. I move the quote at the end of the Shell Execute line to the end of the mailto argument). However, it just pops up the outlook window with the paramters already set.

    Also, I forgot to mention that I might need the ability to attach files. Any suggestions on that?

    Kevin

  4. #4
    Addicted Member
    Join Date
    Nov 2000
    Location
    State of Confusion
    Posts
    133
    Check out this thread, it should help you.....

    http://www.vbforums.com/showthread.p...hreadid=118765
    "Don't take life too seriously, you'll never get out alive"
    from Van Wilder

  5. #5
    Member
    Join Date
    Sep 2001
    Location
    Montreal
    Posts
    34
    I am currently using a DLL in many projects, get it there:
    http://www.freevbcode.com/searchresults.asp

    It's called vbSendMail, Very simple to use. The only catch is that you need an SMTP to route your message.

    If you use a public SMTP sometimes it takes a while for the message to reach the destination, but if you have access to a private one, then it's a flash...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width