Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 Sending email

Status
Not open for further replies.

Bluejay07

Programmer
Mar 9, 2007
780
CA
Hello,

I am creating a new project that will require sending an email with an attachment. Looking through past projects, we have one project that uses MAPISession and MAPIMessages while the other uses vbSendMail.clsSendMail. To my knowledge, both methods have worked equally well in the past.

I'm wondering which method is more recommended or more efficient or if another method is preferred.

Thanks.



If at first you don't succeed, then sky diving wasn't meant for you!
 

I have pretty good luck with CDO:

Add the reference to Microsoft CDO for Windows 2000 library (at least that's what is on my machine)

Code:
Dim objMessage As CDO.Message

Public Sub SendAMessage(strFrom As String, strTo As String, _
    strCC As String, strSubject As String, strTextBody As String, _
    Optional strBcc As String, Optional strAttachDoc As String)

On Error GoTo MyErrorHadler

Set objMessage = New CDO.Message

With objMessage
    .From = strFrom
    .To = strTo
    If Len(Trim$(strCC)) > 0 Then
        .CC = strCC
    End If
    If Len(strBcc) > 0 Then
        .BCC = strBcc
    End If[green]
    ''' On behalf of
    '.Sender = "Bill@msn.com"[/green]
    .Subject = strSubject
    .TextBody = strTextBody
    
    If Len(strAttachDoc) > 0 Then
        .AddAttachment strAttachDoc
    End If
    
    With .Configuration.Fields
        .Item(CDO.cdoSMTPServer) = "NTSMTP.ABC.XYZ.LAN"
        .Item(CDO.cdoSMTPServerPort) = 25
        .Item(CDO.cdoSendUsingMethod) = CDO.cdoSendUsingPort
        .Item(cdoSMTPConnectionTimeout) = 10
        .Update
    End With
    .Send
End With

Have fun.

---- Andy
 
I recently used Microsoft Mapi Controls 6.0.
Works fine with Outlook, Live Mail and Thunderbird. Not with Dreammail.

I Can't get Andys code to work. Don't know why. Cannot connect to server. Tried other SMTP servers as well.
 
Personally I've always liked vbSendMail, I never had problems with it as long as my settings were correct. Also nice to have the source code for it available.
 
I like VBSendMail because the with events code returns meaningful messages when there is a failure. Maybe the others do also but that is what I use.
 
Thanks for the feedback, including Andy for providing the associated code as well. It seems like it's more personal preference rather than anything else. I'll try each of these methods to see what works best for me.

Thanks again.

If at first you don't succeed, then sky diving wasn't meant for you!
 
CDO for Windows is probably your only free option when you need secure authentication and SSL/TLS though, since Simple MAPI is fading away. I'm not convinced that "VBSendMail" thing ever made sense as an option once Win2K came out.

Plus you get MHTML message support, the IDataSource interface, and lots of other advanced goodies.
 
Thanks Dilettante.

I'm assuming I would need to at the reference 'Microsoft CDO for Windows 2000 Library'? In addition, I found some sample code but not sure if it's the best. Would you be able to provide some sample code for usage?

If at first you don't succeed, then sky diving wasn't meant for you!
 
>the reference 'Microsoft CDO for Windows 2000 Library'?

That'll be the one
 
Thanks Strongm.

If at first you don't succeed, then sky diving wasn't meant for you!
 
It isn't as complicated as people often make out. A simple example sending a text message with an attachment looks like:
Code:
    Dim cdoMsg As New CDO.Message

    With cdoMsg
        With .Configuration.Fields
            .Item(cdoSendUsingMethod).Value = cdoSendUsingPort
            .Item(cdoSMTPServerPort).Value = 25
            .Item(cdoSMTPServer).Value = "smtp.demo.net"
            .Item(cdoSendUserName).Value = "myuserid"
            .Item(cdoSendPassword).Value = "secretpassword"
            .Item(cdoSMTPAuthenticate).Value = cdoBasic
            .Update
        End With
        .From = """Big Bob"" <myuserid@demo.net>"
        .To = """Fred"" <fflintstone@yahoo.com>"
        .Subject = "Some more really spiffy mail for you!"
        .TextBody = "This is some mail I think you'll want." & vbNewLine _
                  & "Be sure to read the attachment."
        .AddAttachment App.Path & "\attach.txt"
        
        On Error Resume Next
        .Send
    End With
    
    If Err.Number <> 0 Then
        MsgBox "CDO error " & Hex$(Err.Number) & vbNewLine & Err.Description
    Else
        MsgBox "Mail sent!"
    End If
 
Thank you for that code.

If at first you don't succeed, then sky diving wasn't meant for you!
 
The code provided by Andy and Dilettante is very similar, however Andy's code doesn't require a user name, password or SMTPAuthenticate.

Are these required?

If at first you don't succeed, then sky diving wasn't meant for you!
 
OK, I got the CDO to work. When using Gmail as sender one must use these extra lines:

.Item(cdoSMTPServerPort).Value = 465
.Item(cdoSMTPUseSSL).Value = True

If I understand correctly, the main difference between CDO en MAPI is that CDO sends mail bypassing the email client. You don't even need an email client. You only need an email address en access to it, to send a mail.
With MAPI, you generate an email (you can, but don't need to send it immediately) in the MAPI compatible email client. Like Outlook or Thunderbird. Its shows up in the Inbox or Concept box or on your screen.
That means, if you make a program for different (other) users you don't need parameters or setups for the actual email address/passwords/smtp server of the sender. You just use the settings of the email client. So in that case MAPI makes more sense.


 
Thanks for the reply Jack.

I don't think that your assumption is entirely correct. As mentioned in the first post, my company used different methods in older programs and I do not believe those programs accessed any user settings with either of those methods.

If at first you don't succeed, then sky diving wasn't meant for you!
 

With my code you need to provide the cdoSMTPServer, but if you can not send a message this way (but the code compiles fine) check with your e-mail admin if you are allow to send e-mails this way from your application. I had to do that because of possibility of sending 1000 spam e-mails and our firewall would stop it. They had to add my application to ‘not a junk e-mail’ list or something

Have fun.

---- Andy
 
Thanks Andy

If at first you don't succeed, then sky diving wasn't meant for you!
 
I think with MSMAPI32.OCX you need an email client.
If you have users in company A, Company B, C and D, and dozens of other (home)users, they all have different standard email addresses.

To prevent changing settings and figuring out how, one can use MAPI:
Add MS MAPI controle 6.0 to the project and add MapiSession and MapiMessages objects to the form.
Then use some code like:
Code:
Private Sub Command1_Click()
    With MAPISession1
       .DownLoadMail = False
       .LogonUI = False
       .SignOn
       .NewSession = True
    End With
    With MAPIMessages1
        .SessionID = MAPISession1.SessionID
        .Compose
        .RecipAddress = "abcdef123@gmail.com" 'or a variable
        .ResolveName
        .MsgSubject = "subject text"
        .MsgNoteText = "Hi there, this is a test"
        .AttachmentPathName = "g:\aa.jpg"

        ' chose save or send
        '.Save 'Saves it as a draft
        '.Send True 'Pops up on you screen in your email client
        .Send False 'Sends automatically via your email client
    End With
    MAPISession1.SignOff
End Sub
As you can see, no sender settings required, because the program generates messages within the email client thereby using the settings of that client.
So it all depends on your goal: One user with one email address or hundreds of users with different addresses.
 
Port 465 is for secure (SSL) SMTP


Port 587 is the alternative to port 25 for using TLS authentication with GMail.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Using CDO, do you have to have an email client? This program will probably be installed on a machine without email and testing environment does not have email.

ERROR:
The server rejected one or more recipient addresses. The server response was: 554 5.7.1 <___@_____.com>: Relay access denied

If at first you don't succeed, then sky diving wasn't meant for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top