Solved

send mail thru VB 6.0 app - will work with OUTLOOK & OUTLOOK EXPRESS

Posted on 2003-12-01
18
4,237 Views
Last Modified: 2007-12-19
i am trying to send an email using the CLIENT'S default mail program (either OUTLOOK or OUTLOOK EXPRESS)

must open email so they can alter subject, text, attachment, etc. before sending mail

i have tried MAPI, but it does not like OUTLOOK EXPRESS and gets an error 19 (invalid MAPI session) whenever the machines default mail program is OUTLOOK EXPRESS.

any ideas ?
0
Comment
Question by:kevman63
  • 7
  • 7
  • 3
  • +1
18 Comments
 
LVL 2

Expert Comment

by:devoted2christ
ID: 9852993
Do you _have_ to use Outlook/OE to do it for you? There is a standalone mailing/smtp control that you could use to accomplish this. You could create your own form for editing the email, and then use the control to actually send it, without involving OE/Outlook. The reason I say this is because you'll also run into problems if their default email program is Eudora or something else. It's best if you have control over the entire process. But if this isn't plausible, then we can help further...
0
 
LVL 1

Author Comment

by:kevman63
ID: 9853634
i would prefer to send it using the default mail client on there machine, so they could see it in SENT items history...

the biggest problem is MAPI.  works one way for OUTLOOK, works another way for OE.

with the  standalone mailing/smtp control do i have to have the user run a setup at all ?  mainly we want to keep it as simple as possible - users clicks button, email pops up to with these filled in:
 <recipient>
 <subject>
 <body>
 <attachment> (optional)

using default mail client seems ideal, "But if this isn't plausible, then we can help further..."

anymore help would be appreciated...

MS KB article that does NOT help: http://support.microsoft.com/default.aspx?scid=kb;EN-US;315458
0
 
LVL 2

Expert Comment

by:devoted2christ
ID: 9854944
Hmm... I simply don't think that is going to work, then. Yes, it would be probably be nice to have it in Sent Items, so they could archive emails that are sent. If this is neccessary, then you can do one of two things:
1) Require then to use Outlook. BAD Idea.
2) Use the DLL I'm about to give you, and simply send an additional copy to the person's email address, and include a subject line or sender they can filter on (they can filter it into their sent mail folder or another folder).

Check this out:
http://www.freevbcode.com/ShowCode.Asp?ID=109

I've used that DLL for some of my applications. It supports attachments, which is key. It is very easy to use. So easy, in fact, that you can create your own mail client within an hour.

The only thing it requires as far as setup is this: 1) You need to register the DLL in your program's setup, or create a script that registers the dll for them (regsvr32 "C:\Path\vbSendMail.dll"), and 2) You'll need to know their smtp server name, login information, and email address. I'm even pretty sure that if their email server doesn't require logins, then all you need is their email address (it will look up the rest). You may experiment with searching for these settings in the registry. Perhaps OE/Outlook stores them in a place you can access. Alternatively, if you can locate a "free" smtp server, you can have it automatically send through that, so that you don't even need those settings.

This is a pretty solid DLL, it's been around for a few years. I've had no problems with it. I would reccommend this route. Like I said, you can always provide them the option to forward it to their own email address with a filterable subject line or something. And if they need to edit the information in any way, you can create a form that looks like the OE/Outlook mail sender, with just what they need to be able to edit. It wouldn't be difficult at all.

It includes great documentation, two example executables, and the source code. In fact, if you load the code up into your project, you won't even need to have them register the separate DLL; simply compile it into your software.

Again, this seems right to me, but only you know what functionality you really need, so take this with a grain of salt. :)
0
 
LVL 2

Expert Comment

by:bdbrown
ID: 9855323
I have done this in both VB6 and Access97.  I can put the code up here tomorrow if you would like.
0
 
LVL 2

Expert Comment

by:bdbrown
ID: 9858017
In Access97 (likely the same in 2000) you use the following
'
Dim sendTo as string
Dim sendSubject as string
Dim sendMsgText as string

DoCmd.SendObject acSendNoObject, , , sendTo, , , sendSubject, sendMsgText, False
'

Here is the code I use in VB (basically)
Create a standard .exe and place a command button on the form - then paste this code into the form.
Goto Projects/References/Browse and set a reference to C:\Program Files\Microsoft Office\Office\msotl85.olb

'
Private Declare Function GetUserName Lib "advapi32" _
   Alias "GetUserNameA" _
  (ByVal lpBuffer As String, _
   nSize As Long) As Long

Private Declare Function lstrlenW Lib "kernel32" _
  (ByVal lpString As Long) As Long

Private Const MAX_USERNAME As Long = 15

Private Function GetLocalUserName() As String

  'return the name of the user
   Dim tmp As String

   tmp = Space$(MAX_USERNAME)

   If GetUserName(tmp, Len(tmp)) Then
      GetLocalUserName = TrimNull(tmp)
   End If

End Function


Private Function TrimNull(startstr As String) As String

   TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
   
End Function


Private Sub MailMain()
Dim myOlApp As Outlook.Application
Dim myItem As MailItem
Dim myAttachments As Attachments
Dim MyItemSize As Long
Dim sUserName As String

    Set myOlApp = CreateObject("Outlook.Application")
    Set myItem = myOlApp.CreateItem(olMailItem)
    Set myAttachments = myItem.Attachments

    ' get username - not required
    sUserName = GetLocalUserName()
    'add subject text
    myItem.Subject = "Message from " & sUserName
    'add body text
    myItem.Body = "Anything you want"
    myItem.Display

End Sub

Private Sub Command1_Click()

    Call MailMain

End Sub
'
0
 
LVL 2

Expert Comment

by:bdbrown
ID: 9858061
One more thing;
The Reference Type Library above is For "OUTLOOK" which comes with MS Office. You may need to search for a different reference type library If you use "Outlook Express".

Enjoy - bdb
0
 
LVL 1

Author Comment

by:kevman63
ID: 9860557
to DEVOTED:

many thanks for the vb project group - very nice work... is there any way to automatically get the SMTP server IP # ?
0
 
LVL 2

Expert Comment

by:devoted2christ
ID: 9860824
You shouldn't need the IP address for this module, though there are ways to find it (search around on Google for using VB to find IP addresses of servers). In fact, it includes the code needed for those lookups, just in case you're curious (check out the MXQuery.zip archive in that folder). The code has a property SMTPHost that should accept a text version of the server, like: mail.myhost.com. I suggest wading through the example programs, as well as the .doc documentation file, as it has a wealth of information about it. For your purposes, you can probably get by with the introductory code of that manual, as you'll only likely be needing basic functionality. I'm pretty sure this will be a much easier solution than the MAPI method or hooking to the Outlook/OE libraries. Plus, you won't run into version dependencies (since different people have different versions of software), which will save you a lot of headache. Just remember that whenever possible, keep your code as self-contained as it can be. It makes everyone happier in the end :)

Let me know if you need more assistance with it...
David
0
 
LVL 1

Author Comment

by:kevman63
ID: 9870136
DEVOTED:

ok, one MAJOR stumbling block, but i think we can get past it:

when i run in VB environment, no problem, works great.

when i compile and run EXE, get error # 429 - ActiveX Component cannot create object - and EXE fails

any thoughts ?  do DLL's need to be in System - System32 folder ?

HELP!  i'm sooooooo close!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:kevman63
ID: 9870180
nevermind, read DOCS (duh!) and registered that crafty little DLL!
0
 
LVL 2

Expert Comment

by:devoted2christ
ID: 9870287
Ugh, I hate that error so much...

It may not have to be in the system/system32 folders, but you'll probably need to register the DLL...
> regsvr32 "C:\path\dll_location.dll"

Another thing that will work _better_ is if you put the actual class module in your project instead of referencing the dll. This way, you don't have to worry with "dll hell" as we like to call it. DLLs are great in theory, but always cause problems. So my suggestion would be to remove your reference in your project, put the actual code modules in there, and have it be part of your executable. But registering it should solve your immediate problem...
0
 
LVL 1

Author Comment

by:kevman63
ID: 9870349
not sure what you mean by this:  "put the actual class module in your project instead of referencing the dll."

i am all too familiar with DLL/OCX hell, but i goto PROJECT - REFERENCES and see nothing about that dll, only the standard stuff you get with a VB windows app...
0
 
LVL 2

Expert Comment

by:devoted2christ
ID: 9870464
Sorry...

In the root folder of the vbSendMail project, you'll see three files: SendMail.cls, modMXQuery.bas, and frmSck.frm. You need to put these three files in your project (Project-> Add File). Once they are in your project, you'll have no problems with referencing that DLL. That should solve all your problems.

Or, you can still use the DLL method. You'd just have to make sure that it gets registered on every user's machine first...
0
 
LVL 1

Author Comment

by:kevman63
ID: 9870532
ok, here is how it is now:

the zip i downloaded had a project GROUP, and includes the above files in a different projecyt, but still part of the group (this was how it came, i never use groups)

when i removed that project and tried to add above files to a single project, tells me "SendMail.cls cannot be made public - will bve private" - needless to say app fails when you try to run it...

i appreciate your help, and rest assured, you're going to get these points... !
0
 
LVL 2

Expert Comment

by:devoted2christ
ID: 9870767
Hmm, it should be ok. I'm assuming you're making a standalone executable? It doesn't have to be public if it is within the program.

How are you instantiating your clsSendMail object? Are you still using "Private WithEvents poSendMail as vbSendMail.clsSendMail"? If so, you need to reference the class by clsSendMail, not vbSendMail.clsSendMail. Also, make sure you're copying the files above to your own directory, not referencing the files that are already there. Let me know if that solves the issues... It works on my end, so we'll keep working until you're all set...
0
 
LVL 1

Author Comment

by:kevman63
ID: 9870802
"you need to reference the class by clsSendMail, not vbSendMail.clsSendMail"

YOU DA MAN!

thanks alot, that works great!

BTW - even with no attachment, it takes at least 5-6 seconds to go from 22/23 % to 100% - my boss is telling me that is too long!!! - any idea ?  if not, no biggy... he can just learn patience!

i'm gonna keep this for app for myself, if you want i'll send you the finished result... its mostly your work anyway ;->
0
 
LVL 2

Accepted Solution

by:
devoted2christ earned 250 total points
ID: 9870929
Hmm, I can't think of any reason it'd be slowing down, unless your connection is slow or your SMTP server is slow. It may also speed it up if you use it's IP address instead of domain name (so it doesn't have to do the lookup). Other than that, I can't think of anything that would be bogging it down. Mine is pretty quick because I run my own free smtp server (http://www.postcastserver.com/), so if he really needs speed, that may be an option. You may try and see if the DLL option works faster; I don't see why it would, but you never know.

And good to hear it at least works! vbSendMail is a pretty nifty tool; I use it to send error logs back to me from the software I write, so I know exactly what is going on with it. And yeah, I'd like to see what you've got; it'd be cool (david AT frictionlessfloor DOT com). Let me know if you need anything else!
0
 

Expert Comment

by:OwenMania
ID: 10050019
can vbSendMail auto search for a free smtp server?
or do i have to manually set one for it?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now