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!

Sending Exchange e-mail from Visual Basic ??? 2

Status
Not open for further replies.

BobTheMad

Programmer
Jun 11, 1999
81
US
I am writing code that will need to rapidly send large quantities of e-mails through the company's exchange server. HJow would you reccommend that I go about solving this problem? (My previous attempts have resulted in mail sessions that lock up on a regular basis)
 
I've used the Outlook Object Library in VB and it's pretty easy to invoke, but whether it's up to rapidly sending large quantities of e-mails, I couldn't guess. My app is a background job waiting for files to appear to be e-mailed.
 
Hi,<br><br>I have just completed a project to much the same thing. <br>I used CDO 1.21 which works well with exchange <br><br>'I used the two functions below on in a Dll which I then called from both another VB app and from a Web Page.<br><br>'You must have CDO 1.21 reference before complie or run<br><br>'Declarations<br><br>Dim objRecipColl As Recipients&nbsp;&nbsp;' Recipients collection<br>Dim objNewRecip As Recipient&nbsp;&nbsp;&nbsp;&nbsp;' New recipient object<br>Dim objSession As Object<br>Dim objMessage As Object<br>Dim objRecipient As Object<br><br><br>Public Function SendMail(tSubject As String, tBody As String) As Boolean<br><br>On Error GoTo ErrorHandler<br><br>SendMail = True<br><br>&nbsp;&nbsp;'Create the Session Object<br>&nbsp;&nbsp;Set objSession = CreateObject(&quot;mapi.session&quot;)<br><br><br>&nbsp;&nbsp;'Logon using the session object<br>&nbsp;&nbsp;'Specify a valid profile name if you want to<br>&nbsp;&nbsp;'Avoid the logon dialog box<br>&nbsp;&nbsp;'If you don't include the profilename argument then the profile prompt will appear<br><br>&nbsp;&nbsp;objSession.Logon profileName:=&quot;MS Exchange Settings&quot;<br><br><br>&nbsp;&nbsp;'Add a new message object to the OutBox<br>&nbsp;&nbsp;Set objMessage = objSession.Outbox.Messages.Add<br><br><br>&nbsp;&nbsp;'Set the properties of the message object<br>&nbsp;&nbsp;objMessage.Subject = tSubject<br>&nbsp;&nbsp;objMessage.Text = tBody<br><br><br>&nbsp;&nbsp;'Add a recipient object to the objMessage.Recipients collection<br>&nbsp;&nbsp;Set objRecipient = objMessage.Recipients.Add<br><br><br>&nbsp;&nbsp;'Set the properties of the recipient object<br>&nbsp;&nbsp;objRecipient.Name = GetAddress<br>&nbsp;&nbsp;objRecipient.Type = mapiTo<br>&nbsp;&nbsp;objRecipient.Resolve<br><br>&nbsp;&nbsp;'Send the message<br>&nbsp;&nbsp;objMessage.Send showDialog:=False<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;'Logoff using the session object<br>&nbsp;&nbsp;objSession.Logoff<br><br><br>***************************************************<br>***************************************************<br><br>Public Function GetAddress() As String<br><br>' Purpose: Set the global variable that contains the current recipients<br>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;collection to that returned by the Session AddressBook method<br>' See documentation topic: AddressBook method (Session object)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo err_Session_AddressBook<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;If objSession Is Nothing Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Must first create MAPI session and logon&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Function<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;Set objRecipColl = objSession.AddressBook( _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Title:=&quot;Select Attendees&quot;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;forceResolution:=True, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;recipLists:=1, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;toLabel:=&quot;&Cdo&quot;) ' appears on button<br>&nbsp;&nbsp;&nbsp;&nbsp;' Note: first parameter (&quot;recipients&quot;) not used in this call<br>&nbsp;&nbsp;&nbsp;&nbsp;' recipients:=objInitRecipColl initializes recipients for dialog<br>&nbsp;&nbsp;&nbsp;' MsgBox &quot;Name of first recipient = &quot; & objRecipColl.Item(1).Name<br>&nbsp;&nbsp;&nbsp;'If you want to send to a large number of people then loop the array and send for them all<br><br>&nbsp;&nbsp;&nbsp;GetAddress = objRecipColl.Item(1).Name<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Function<br>&nbsp;<br>err_Session_AddressBook:<br><br>Resume Next<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Function<br><br>End Function<br>Exit Function<br><br>ErrorHandler:<br>SendMail = False<br>Debug.Print Err.Number; Err.Description; vbTab; Err.Source<br>Debug.Assert False<br>Resume Next<br><br>End Function<br><br><br>Hopefully this helps <br><br>Regards <br><br>Vince
 
That CDO code works great so far... Thanks! The only problem I can see is when an attachment is sent, and the user pulls up the e-mail. The user sees the attachment name as what it is supposed to be (example: address.txt), but when the file is double-clicked in Outlook, the file comes up with a random 'temp' name (Example: att32F4.TMP)... This is bad...<br><br>Got any ideas?
 
Add this to the Declarations<br><br>dim objAttach as attachment <br><br><br>Add this additional code in the send mail function <br><br><br>&nbsp;Set objAttach = objMessage.Attachments.Add<br>&nbsp;<br>&nbsp;objAttach.Name = &quot;Name&quot; 'The name you want to display to the user<br>&nbsp;objAttach.Source = &quot;Fullpath&quot; 'The full path of the attachment data<br><br><br>Hope this works <br><br>Vince&nbsp;&nbsp;
 
What is CDO 1.21 and the Outlook Object Library? I can't find them in the components selection.
Best Regards and many Thanks!
Michael G. Bronner X-)

&quot;They who drink beer will think beer.&quot; Washington Irving
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top