Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

E-mail from VB 1

Status
Not open for further replies.

derrick

Computer
Jun 13, 2001
40
Does anyone know how to automatically mail a document using VB, how to write a message and heading on the e-mail, and attach a document.

Thx in advance
Derrick
 
Replies continue below

Recommended for you

There are a couple of ways to e-mail through VB. You can use the mapi controls which use the default mailing program on the system to send and receive mail, or you can you search the web for an smtp componet that will allow you to send e-mail directly to the smtp server. I've used the smtp componet offered by and haven't had any problems with it.
 
Here is a reply I posted in Tek-Tips recently:

It's on tek-tips.com
Look for thread222-504286


You can use the MapiSession and MapiMessage controls if you want to work with any Mapi client

Stick a MapiSession control and a MapiMessage control on a form and use this as the basis for your code:

MAPISession1.SignOn
DoEvents
mapMess.SessionID = MAPISession1.SessionID
mapMess.Compose
mapMess.MsgNoteText = "hi"
mapMess.RecipDisplayName = "fred"
'mapMess.RecipAddress = "fred@aol.com"
mapMess.ResolveName
mapMess.MsgSubject = " T E S T M E S S A G E "
mapMess.AttachmentPathName = "c:\fred.txt"
mapMess.AttachmentName = "my fred"
mapMess.Send False
MAPISession1.SignOff


Use the remmed out line instead of the line above if you want to use the full email address rather than the default address book lookup

Good Luck
johnwm
 
Thanks John,

the solution I have used is shown below, commented out lines can be used to add other options etc.

Sub SendMAPIMessage()
Dim MapiSession As Object
Dim MapiMessage As Object
Dim MapiRecipient As Object
Dim MapiAttachment As Object
Dim Recpt, errObj As Long, errMsg
Const mapiTo = 1
Const mapiHigh = 1

On Error GoTo MAPITrap ' Create the MAPI Session.
Set MapiSession = CreateObject("Mapi.Session")
' Log on to the session
' MapiSession.Logon profilename:="MS Exchange Settings"
MapiSession.Logon profilename:="Microsoft Outlook"
' Add a message to the Outbox.
Set MapiMessage = MapiSession.Outbox.Messages.Add
' Add the recipients of the message. Note, each recipient must be
' added separately to the Recipients collection of the Message
' object.
With MapiMessage
Set MapiRecipient = MapiMessage.Recipients.Add
' MapiRecipient.Name = "derrick.taylor@stockport.gov.uk"
MapiRecipient.Name = "Derrick Taylor"
MapiRecipient.Type = mapiTo
'Set MapiRecipient = MapiMessage.Recipients.Add
'MapiRecipient.Name = "Andrew Fuller"
'MapiRecipient.type = mapiCc
'Set MapiRecipient = MapiMessage.Recipients.Add
'MapiRecipient.Name = "Michael Suyama"
'MapiRecipient.type = mapiBcc
' Resolve each recipient's e-mail name.
Debug.Print .Recipients.Count
' For Recpt = 0 To .Recipients.Count - 1
For Recpt = 1 To .Recipients.Count
.Recipients(Recpt).Resolve showdialog:=True
Next
' Attach a file to the message.
'Set MapiAttachment = MapiMessage.Attachments.Add
'With MapiAttachment
' .Name = "Customers.txt"
' .type = mapiFileData
' .Source = "C:\Examples\Customers.txt"
' .ReadFromFile FileName:="C:\Examples\Customers.txt"
' .position = 2880
'End With
' Assign the text, subject, and importance of the message.
.subject = "My Subject"
.Text = "This is the text of my message." & vbCrLf & vbCrLf
.importance = mapiHigh
' View the message in Microsoft Exchange before sending. Set
' the ShowDialog argument to False if you want to send the
' message without viewing it in Microsoft Exchange.
.Send showdialog:=False
End With
Set MapiSession = Nothing ' Clear the object variable.

MAPIExit:
Exit Sub

MAPITrap:
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.")
MsgBox "Error number " & Err.Number & "." & Chr(13) & Err.Description

Resume MAPIExit
End Select

Resume
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor