Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB 6
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 26th, 2005, 09:01 AM
Registered User
Join Date: Oct 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using vb6 to send email with attachment

Hi everybody,
I want to send emails with attachment by using vb6 and outlook 2003.

The problem is:
  During this process outlook show me warning messages.
How can I send the messages without these warnings’?

I appreciate any help

Old October 26th, 2005, 09:41 AM
Friend of Wrox
Join Date: Oct 2004
Posts: 224
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to ashu_from_india Send a message via Yahoo to ashu_from_india


can u mail tht piece of code tht u r using


Old October 26th, 2005, 04:04 PM
Registered User
Join Date: Oct 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts


Here is a code:

    ' This procedure sets an object variable to the MAPI Session object
    ' using the CreateObject() function. Then, it logs on to the session
    ' using a predefined profile. As soon as you are logged on,
    ' the procedure creates
    ' a new message and adds it to the Messages collection of the Outbox
    ' of the user. Then, it creates two recipients (one on the TO: line and
    ' one on the CC: line) and then adds both to the Recipients collection
    ' of the message. Next, it resolves the names of all recipients.
    ' Then, it attaches a sample file before filling in the Subject,
    ' Text, and Importance attributes of the message.
     Sub SendMAPIMessage(ByVal MsgBody As String, ByVal Subject As String, ByVal EmailTo As String, ByVal EmailCc As String, ByVal EmailBcc As String, ByVal EmailFrom As String, ByVal FileLocation As String, ByVal FileName As String)

        Dim objApp As Outlook.Application
        Set objApp = Application
        Dim l_Msg As MailItem
        Dim colAttach As Outlook.Attachments
        Dim l_Attach As Outlook.Attachment

        Dim MapiSession As MAPI.Session
        Dim MapiMessage As MAPI.Message
        Dim MapiRecipient As MAPI.Recipient
        Dim MapiAttachment As MAPI.Attachment
        Dim MapiAttachments As MAPI.Attachments
        Dim Recpt
        Dim errObj As Long
        Dim errMsg

        On Error GoTo MAPITrap
        ' Create the MAPI Session.
        Set MapiSession = CreateObject("Mapi.Session")

    Dim strProfileInfo As String
    strProfileInfo = "newyork1" & vbLf & "TW Data"

    MapiSession.Logon "TW Data", "TreadwayS", False, True, 0, False, strProfileInfo

        ' Add a message to the Outbox.
        Set MapiMessage = MapiSession.Outbox.Messages.Add

        With MapiMessage
            Set MapiRecipient = MapiMessage.Recipients.Add
            'MapiRecipient.Name = "Nancy Davolio"
            MapiRecipient.Name = EmailTo
            MapiRecipient.Type = mapiTo
            Set MapiRecipient = MapiMessage.Recipients.Add
            MapiRecipient.Name = EmailCc
            MapiRecipient.Type = mapiCc
            Set MapiRecipient = MapiMessage.Recipients.Add
            MapiRecipient.Name = EmailBcc 'Leon. 10/05/05
            MapiRecipient.Type = mapiBcc

            ' Resolve each recipient's e-mail name.
            ' Starting with Outlook version 8.03 (ref. Q172623)
            ' OLE Messaging 1.0 was replaced with Active Messaging 1.1.
            ' Outlook 98 (version 8.5) replaced Active Messaging
            ' with Microsoft CDO (Collaborative Data Objects) 1.21.
            ' OLE Messaging 1.0 uses a zero-based Recipients collection;
            ' Active Messaging 1.1 and Microsoft CDO 1.21 are 1-based.

            For Recpt = 1 To .Recipients.Count
               .Recipients(Recpt).Resolve showdialog:=False

            Set MapiAttachments = MapiMessage.Attachments
            Set MapiAttachment = MapiAttachments.Add(FileLocation)

            With MapiAttachment

                 .Name = FileName & Format(Now, "_dd-mmm-yyyy-Hh-Nn-Ss") & ".xml" 'Leon. 10/03/05
                .Type = mapiFileData
                .Source = FileLocation
                .ReadFromFile FileName:=FileLocation
                .Position = 2880
            End With

            .Subject = Subject '"My Subject"
            .Text = MsgBody ' "This is the text of my message." & vbCrLf & vbCrLf

            .Send showdialog:=False 'True '
        End With
        Set MapiSession = Nothing ' Clear the object variable.
'MsgBox ("Done")
        Exit Sub

        errObj = Err - vbObjectError ' Strip out the OLE automation error.
        Select Case errObj
            Case 275 ' User cancelled sending of message.
                Resume MAPIExit
            Case Else
                errMsg = MsgBox("Error " & errObj & " was returned.")
                Resume MAPIExit
            End Select
     End Sub

Thanks allot,
Old August 25th, 2011, 01:56 AM
Registered User
Join Date: Aug 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Easy VB6 Multiple Attachments

I regularly send emails with multiple attachments using VB6 with the following routine

Just put the paths to the files you want to send in the string array EmailAttachments.

Filexists is a function that confirms the existence of each file to prevent run-time error.

' Procedure : SendMail
' Author : coolcurrent4u
' Date : 4/19/2011
' Purpose : sends email using the cdo namespace
' TODO : check for attachment existence before passing it
' : Pass only number to port textfield or else it will throw an error
' Questions : Please ask in vbforums.com
Public Function SendMail(sTo As String, sSubject As String, sFrom As String, _
sBody As String, sSmtpServer As String, iSmtpPort As Integer, _
sSmtpUser As String, sSmtpPword As String, _
EmailAttachments() As String, bSmtpSSL As Boolean) As String

On Error GoTo SendMail_Error:
Dim lobj_cdomsg As CDO.Message
Dim i As Integer
Set lobj_cdomsg = New CDO.Message
lobj_cdomsg.Configuration.Fields(cdoSMTPServer) = sSmtpServer
lobj_cdomsg.Configuration.Fields(cdoSMTPServerPort ) = iSmtpPort
lobj_cdomsg.Configuration.Fields(cdoSMTPUseSSL) = bSmtpSSL
lobj_cdomsg.Configuration.Fields(cdoSMTPAuthentica te) = cdoBasic
lobj_cdomsg.Configuration.Fields(cdoSendUserName) = sSmtpUser
lobj_cdomsg.Configuration.Fields(cdoSendPassword) = sSmtpPword
lobj_cdomsg.Configuration.Fields(cdoSMTPConnection Timeout) = 30
lobj_cdomsg.Configuration.Fields(cdoSendUsingMetho d) = cdoSendUsingPort
lobj_cdomsg.To = sTo
lobj_cdomsg.From = sFrom
lobj_cdomsg.Subject = sSubject
lobj_cdomsg.TextBody = sBody
' add attachments if any
For i = LBound(EmailAttachments) To UBound(EmailAttachments)
If filexists(EmailAttachments(i)) Then
lobj_cdomsg.AddAttachment (EmailAttachments(i))
End If
Next i
Set lobj_cdomsg = Nothing
SendMail = "ok"
Exit Function

SendMail = Err.Description
End Function

You must reference "Microsoft CDO for Windows 2000 Library" in your project.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to send an email with an attachment in it samputha Excel VBA 2 August 13th, 2009 09:12 PM
how to send email with attachment using JSP kirtesh4u J2EE 0 May 19th, 2007 01:17 AM
How to Send Mail with attachment using JAVA rekha_jsr Classic ASP Basics 2 September 16th, 2004 01:31 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.