i want to genrate emails using microsoft outlok express object.
plz help me alo give the code if possible
Printable View
i want to genrate emails using microsoft outlok express object.
plz help me alo give the code if possible
Dim oOutlook as Outlook.Application
Dim oMail as Outlook.MailItem
set OLApp = new Outlook.Application
set OLMI = OLApp.CreateItem(olMailItem)
OLMI.to = "[email protected]"
OLMI.Subject = "Hi Tom"
OLMI.Body = "This message is sent by some VB code"
OLMI.Send
OLMI.Close olDiscard
OLApp.Quit
Tom Cannaerts
[email protected]
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
th code is running smoothly without errors but still the email is not getting generated what could be th eproblem
'From Microsoft.com:
'Example: Sending E-Mail Messages
'Probably the most common item you will work with is the e-mail message. The e-
'mail message item represents an e-mail message. Aside from the common
'properties and methods including Subject, Body, to, CC, and Send, you will
'need to be able to work with two other objects nested within the item. These
'objects are the Recipients collection object, which represents all recipients
'of the e-mail (to and CC alike), and the Attachments collection object, which
'represents any attachments to the e-mail. The code shown below demonstrates
'the creation of an e-mail message with recipients and an attachment.
'
'to indicate a recipient, use the Add method of the Recipients collection and
'pass the name or address of the user. to make certain that the person can be
'reached through e-mail, use the Resolve method and the Resolved property.
'
'to include an attachment to an e-mail message (or any item), use the Add
'method of the Attachments collection and pass the location of the file to be
'attached, and the way the file should be attached such as by reference or
'embedded. You can also specify a different display name for the item with the
'DisplayName property of the Attachment object.
'
Sub NewMailMessage()
Dim ol as new Outlook.Application
Dim ns as Outlook.NameSpace
Dim newMail as Outlook.MailItem
'Return a reference to the MAPI layer.
set ns = ol.GetNamespace("MAPI")
'Create a new mail message item.
set newMail = ol.CreateItem(olMailItem)
With newMail
'Add the subject of the mail message.
.Subject = "Training Information for October 1997"
'Create some body text.
.Body = "Here is the training information you requested:" & vbCrLf
'Add a recipient and test to make sure that the
'address is valid using the Resolve method.
With .Recipients.Add("[email protected]")
.Type = olTo
If Not .Resolve then
MsgBox "Unable to resolve address.", vbInformation
Exit Sub
End If
End With
'Attach a file as a link with an icon.
With .Attachments.Add _
("\\Training\training.xls", olByReference)
.DisplayName = "Training info"
End With
'Send the mail message.
.Send
End With
'Release memory.
set ol = nothing
set ns = nothing
set newMail = nothing
End Sub
Special thanks to Lothar "the Great" Haensler,
Tom Archer, Chris Eastwood (http://vbcodelibrary.co.uk),
TCartwright, Bruno Paris,
Dr_Michael (http://www.zeusflash.com)
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater