473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How do you select which mail box to send from?

I have found the following code which makes excellent use of outlook to send emails without warnings etc.

Expand|Select|Wrap|Line Numbers
  1.   Application_Startup() 
  2.   Dim Ns As Outlook.NameSpace 
  3.   Dim Folder As Outlook.MAPIFolder 
  4.  
  5.   Set Ns = Application.GetNamespace("MAPI") 
  6.   Set Folder = Ns.GetDefaultFolder(olFolderInbox) 
  7.   Set Items = Folder.Items 
  8.  
  9. ' Code: Send E-mail without Security Warnings 
  10. ' OUTLOOK 2003 VBA CODE FOR 'ThisOutlookSession' MODULE 
  11. ' (c) 2005 Wayne Phillips (http://www.everythingaccess.com) 
  12. ' Written 07/05/2005 
  13. ' Last updated v1.4 - 26/03/2008 
  14. ' Please read the full tutorial here: 
  15. ' http://www.everythingaccess.com/tutorials.asp?ID=112 
  16. ' Please leave the copyright notices in place - Thank you. 
  17.  
  18.  
  19. ' FnSendMailSafe 
  20. ' -------------- 
  21. ' Simply sends an e-mail using Outlook/Simple MAPI. 
  22. ' Calling this function by Automation will prevent the warnings 
  23. ' 'A program is trying to send a mesage on your behalf...' 
  24. ' Also features optional HTML message body and attachments by file path. 
  25. ' The To/CC/BCC/Attachments function parameters can contain multiple items 
  26. ' by seperating them with a semicolon. (e.g. for the strTo parameter, 
  27. ' 'test@test.com; test2@test.com' would be acceptable for sending to 
  28. ' multiple recipients. 
  29. End Sub 
  30. Public Function FnSendMailSafe(strTo As String, _ 
  31.                                 strCC As String, _ 
  32.                                 strBCC As String, _ 
  33.                                 strSubject As String, _ 
  34.                                 strMessageBody As String, _ 
  35.                                 Optional strAttachments As String) As Boolean 
  36.  
  37. ' (c) 2005 Wayne Phillips - Written 07/05/2005 
  38. ' Last updated 26/03/2008 - Bugfix for empty recipient strings 
  39. ' http://www.everythingaccess.com 
  40. ' You are free to use this code within your application(s) 
  41. ' as long as the copyright notice and this message remains intact. 
  42.  
  43. On Error GoTo ErrorHandler: 
  44.  
  45.     Dim MAPISession As Outlook.NameSpace 
  46.     Dim MAPIFolder As Outlook.MAPIFolder 
  47.     Dim MAPIMailItem As Outlook.MailItem 
  48.     Dim oRecipient As Outlook.Recipient 
  49.  
  50.     Dim TempArray() As String 
  51.     Dim varArrayItem As Variant 
  52.     Dim strEmailAddress As String 
  53.     Dim strAttachmentPath As String 
  54.  
  55.     Dim blnSuccessful As Boolean 
  56.  
  57.     'Get the MAPI NameSpace object 
  58.     Set MAPISession = Application.Session 
  59.  
  60.     If Not MAPISession Is Nothing Then 
  61.  
  62.       'Logon to the MAPI session 
  63.       MAPISession.Logon , , True, False 
  64.  
  65.       'Create a pointer to the Outbox folder 
  66.       Set MAPIFolder = MAPISession.GetDefaultFolder(olFolderOutbox) 
  67.       If Not MAPIFolder Is Nothing Then 
  68.  
  69.         'Create a new mail item in the "Outbox" folder 
  70.         Set MAPIMailItem = MAPIFolder.Items.Add(olMailItem) 
  71.         If Not MAPIMailItem Is Nothing Then 
  72.  
  73.           With MAPIMailItem 
  74.  
  75.             'Create the recipients TO 
  76.                 TempArray = Split(strTo, ";") 
  77.                 For Each varArrayItem In TempArray 
  78.  
  79.                     strEmailAddress = Trim(varArrayItem) 
  80.                     If Len(strEmailAddress) > 0 Then 
  81.                         Set oRecipient = .Recipients.Add(strEmailAddress) 
  82.                         oRecipient.Type = olTo 
  83.                         Set oRecipient = Nothing 
  84.                     End If 
  85.  
  86.                 Next varArrayItem 
  87.  
  88.             'Create the recipients CC 
  89.                 TempArray = Split(strCC, ";") 
  90.                 For Each varArrayItem In TempArray 
  91.  
  92.                     strEmailAddress = Trim(varArrayItem) 
  93.                     If Len(strEmailAddress) > 0 Then 
  94.                         Set oRecipient = .Recipients.Add(strEmailAddress) 
  95.                         oRecipient.Type = olCC 
  96.                         Set oRecipient = Nothing 
  97.                     End If 
  98.  
  99.                 Next varArrayItem 
  100.  
  101.             'Create the recipients BCC 
  102.                 TempArray = Split(strBCC, ";") 
  103.                 For Each varArrayItem In TempArray 
  104.  
  105.                     strEmailAddress = Trim(varArrayItem) 
  106.                     If Len(strEmailAddress) > 0 Then 
  107.                         Set oRecipient = .Recipients.Add(strEmailAddress) 
  108.                         oRecipient.Type = olBCC 
  109.                         Set oRecipient = Nothing 
  110.                     End If 
  111.  
  112.                 Next varArrayItem 
  113.  
  114.             'Set the message SUBJECT 
  115.                 .Subject = strSubject 
  116.  
  117.             'Set the message BODY (HTML or plain text) 
  118.                 If StrComp(Left(strMessageBody, 6), "<HTML>", _ 
  119.                             vbTextCompare) = 0 Then 
  120.                     .HTMLBody = strMessageBody 
  121.                 Else 
  122.                     .Body = strMessageBody 
  123.                 End If 
  124.  
  125.             'Add any specified attachments 
  126.                 TempArray = Split(strAttachments, ";") 
  127.                 For Each varArrayItem In TempArray 
  128.  
  129.                     strAttachmentPath = Trim(varArrayItem) 
  130.                     If Len(strAttachmentPath) > 0 Then 
  131.                         .Attachments.Add strAttachmentPath 
  132.                     End If 
  133.  
  134.                 Next varArrayItem 
  135.  
  136.             .Display  'The message will remain in the outbox if this fails 
  137.  
  138.             Set MAPIMailItem = Nothing 
  139.  
  140.           End With 
  141.  
  142.         End If 
  143.  
  144.         Set MAPIFolder = Nothing 
  145.  
  146.       End If 
  147.  
  148.       MAPISession.Logoff 
  149.  
  150.     End If 
  151.  
  152.     'If we got to here, then we shall assume everything went ok. 
  153.     blnSuccessful = True 
  154.  
  155. ExitRoutine: 
  156.     Set MAPISession = Nothing 
  157.     FnSendMailSafe = blnSuccessful 
  158.  
  159.     Exit Function 
  160.  
  161. ErrorHandler: 
  162.     MsgBox "An error has occured in the user defined Outlook VBA function " & _ 
  163.             "FnSendMailSafe()" & vbCrLf & vbCrLf & _ 
  164.             "Error Number: " & CStr(Err.Number) & vbCrLf & _ 
  165.             "Error Description: " & Err.Description, _ 
  166.                 vbApplicationModal + vbCritical 
  167.     Resume ExitRoutine 
  168.  
  169. End Function 

I have set this up and it is working perfectly, the only thing I cannot seem to get right is how to set which mailbox to send emails from!

I have two mailboxes in Outlook 2007 one personal, one shared.
The personal one is the default and all emails generated with the above code are sent from that account.

How/where can I change the mailbox?

Any help would be great as I'm a little stuck! :)
PS Not sure if it makes any difference but Outlook is running as exchange.
Jul 14 '10 #1
0 1249

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tom Petersen | last post by:
Ok, I'm 99.9999999999999999999% sure there haven't been any code changes, since the date stamps of the code are older than any email 'failures' The email piece quit working using the mail.send. ...
11
by: Wenfei | last post by:
Hi, For socket, I know how to use select and recv, but I don't know how to use select and send? Is anybody know? Thanks, Wenfei
4
by: Therese A. Sorna | last post by:
Hello all... I am using Access 2002, and am trying to find a way for a user to be able to select multiple entries from a drop-down list. I am hoping that given a list as such: a b c d
2
by: vamsikrishna_b | last post by:
Hello all,the following problem i encountered while transferring data(mail ids) from MS-outlook to one application.Some mail ids after came into the application looked as strange characters.Eg are...
0
by: Igor Charlamov | last post by:
How to access one or more e-mail accounts from a system and then pass it to the mail.to class Mail { public void send() { MailMessage mailMsg = new MailMessage(); mailMsg.From =...
1
by: bluewind44 | last post by:
Is there any way to send mail attachment from stream or binary? Because I design my solution to save files in DB not in physical files, I wanna send files to mail attachments directly. @.@
1
by: mayanksrmcem | last post by:
I need ur help. I want to send emails through php code. but there are some problems n i'm not able to understand the actual problems. the code , i've used is as followes---- <?php if...
0
by: Amit Singla | last post by:
Hi plz tell me How mail send through cdosys.dll Thanks & Regards Amit Singla
1
by: Kevien Lee | last post by:
Hi, When use the SmtpClient class,it is easy to send an e-mail,but how to konw that if the mail send successful,such as timeout and so on? can anyone advice me? Thanks
1
by: damezumari | last post by:
I use MS Office 2003 and would like to run several Word mail merges from Access. My code so far is given below. 1. The code does not do what I want. I want to save the new document with the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.