×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

E-mail from VB

E-mail from VB

E-mail from VB

(OP)
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

RE: E-mail from VB

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 http://www.ostrosoft.com/ and haven't had any problems with it.

RE: E-mail from VB

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

RE: E-mail from VB

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources