Solved

How do I send Emails through Outlook Express, vba, from different accounts

Posted on 2012-03-20
8
585 Views
Last Modified: 2012-04-24
Dear Experts,

I have tried and tried and tried, and am so frustrated that I'm at the point of giving up practically. I have looked at hundreds of web pages and just can't find any way to do this. All I'm trying to do is extend my email class so that I can specify which email account to send an email from. But no matter what I do, or whether I set the .username or .password properties on the mapisession, it /always/ sends from the default account. No matter what. Nor does it ever throw an error. What do I need to do to do this?

Thanks! ~Jeffrey

'This is on a form that calls it..
Private Sub btnSendTest_Click()
    Dim myEmail As New SendEmail
    With myEmail
        myEmail.MessageSubject = "Test Email from Michael"
        myEmail.MessageText = "This is a test email.."
        myEmail.RecipientAddress = "someone@someplace.com"
        myEmail.RecipientName = "Michael"
        myEmail.UserName = Nz(Me.fldSenderAddress)
        myEmail.Password = Nz(Me.fldPassword)
        myEmail.olxSend
    End With
    Set myEmail = Nothing
End Sub

Open in new window


'This is an email class I've created called SendEmail
'Lets you send emails through the user's outlook express account
'  New feature, does CC's and Blind CC's
'Written by Jeffrey Durham, Last Updated on 3/20/2012

Option Compare Database
Option Explicit

' Define and create a structure to hold all of this object's properties
Private Type PropertyStructType
    MessageSubject        As String
    MessageText           As String
    RecipientAddress      As String
    RecipientCC           As String  'takes multiples (seperate by ;'s), CC stands for Carbon Copy
    RecipientBlindCC      As String  'takes multiples (seperate by ;'s), Blind CC stands for Blind Carbon Copy
    RecipientName         As String
    UserName              As String
    Password              As String
    SenderName            As String
End Type
Private PropertyStruct As PropertyStructType

'These are the different types of recipients you can add to a message
Private Enum RecipientsTypes
    mapOrigList = 0     'The message originator (this is always first address sent from)
    MapToList = 1       'The recipient is a primary recipient.
    mapCcList = 2       'The recipient is a copy recipient.
    mapBccList = 3      'The recipient is a blind copy recipient.
End Enum

'The Attachments collection will contain the files that are attached to this email message
Public attachments As New Collection

'######### CLASS PROPERTIES

Public Property Get SenderName() As String
    SenderName = PropertyStruct.SenderName
End Property

Public Property Let SenderName(ByVal strNewValue As String)
    PropertyStruct.SenderName = strNewValue
End Property

Public Property Get UserName() As String
    UserName = PropertyStruct.UserName
End Property

Public Property Let UserName(ByVal strNewValue As String)
    PropertyStruct.UserName = strNewValue
End Property

Public Property Get Password() As String
    Password = PropertyStruct.Password
End Property

Public Property Let Password(ByVal strNewValue As String)
    PropertyStruct.Password = strNewValue
End Property

Public Property Get RecipientName() As String
    RecipientName = PropertyStruct.RecipientName
End Property

Public Property Let RecipientName(ByVal strNewValue As String)
    PropertyStruct.RecipientName = strNewValue
End Property

Public Static Property Get RecipientAddress() As String
    RecipientAddress = PropertyStruct.RecipientAddress
End Property

Public Static Property Let RecipientAddress(ByVal strNewValue As String)
    PropertyStruct.RecipientAddress = strNewValue
End Property

Public Property Get RecipientCC() As String
    RecipientCC = PropertyStruct.RecipientCC
End Property

Public Property Let RecipientCC(ByVal strNewValue As String)
    PropertyStruct.RecipientCC = strNewValue
End Property

Public Property Get RecipientBlindCC() As String
    RecipientBlindCC = PropertyStruct.RecipientBlindCC
End Property

Public Property Let RecipientBlindCC(ByVal strNewValue As String)
    PropertyStruct.RecipientBlindCC = strNewValue
End Property

Public Property Get MessageSubject() As String
    MessageSubject = PropertyStruct.MessageSubject
End Property

Public Property Let MessageSubject(ByVal strNewValue As String)
    PropertyStruct.MessageSubject = strNewValue
End Property

Public Property Get MessageText() As String
    MessageText = PropertyStruct.MessageText
End Property

Public Property Let MessageText(ByVal strNewValue As String)
    PropertyStruct.MessageText = strNewValue
End Property

'######### MAIN

Public Sub olxSend()
    'Sends the Email
    Dim iRecipIndex As Integer, vSplitRecips As Variant, vRecip As Variant
    Dim i As Integer, attch As String, bUsePassword As Boolean
    Dim olxMsg As New MAPIMessages, olxSess As New MAPISession
    
    bUsePassword = (Me.Password <> "")
    olxSess.LogonUI = True  'specifies whether or not a dialog box is provided for sign-on
    If bUsePassword = True Then
        olxSess.UserName = Me.UserName
        olxSess.Password = Me.Password
    End If
    olxSess.DownLoadMail = False
    olxSess.SignOn
    
    'Create Message using our session
    olxMsg.SessionID = olxSess.SessionID
    olxMsg.Compose
    
    'Add attachments to message
    For i = 1 To attachments.Count
        attch = attachments.Item(i)
        If Len(Dir(attch)) > 0 Then
            olxMsg.AttachmentIndex = i - 1  'must subtract 1 because this is 0-based index
            olxMsg.AttachmentPathName = attch
        End If
    Next
    
    'Add Recipient to message
    AddRecipient olxMsg, iRecipIndex, Me.RecipientAddress, Me.RecipientName, RecipientsTypes.MapToList
    
    'Add any Carbon Copies to message
    If Me.RecipientCC <> "" Then
        vSplitRecips = Split(Me.RecipientCC, ";")
        For Each vRecip In vSplitRecips
            AddRecipient olxMsg, iRecipIndex, CStr(vRecip), "", RecipientsTypes.mapCcList
        Next
    End If
    
    'Add any Blind Carbon Copies to message
    If Me.RecipientBlindCC <> "" Then
        vSplitRecips = Split(Me.RecipientBlindCC, ";")
        For Each vRecip In vSplitRecips
            AddRecipient olxMsg, iRecipIndex, CStr(vRecip), "", RecipientsTypes.mapBccList
        Next
    End If
    
    'Set any additional properties
    olxMsg.AddressResolveUI = False
    olxMsg.AddressEditFieldCount = 0  'No edit controls; only browsing is allowed
    olxMsg.AddressModifiable = True
    olxMsg.MsgSubject = Me.MessageSubject
    olxMsg.MsgNoteText = PropertyStruct.MessageText
    
    'Send it!
    olxMsg.send
   
olxExit:
   olxSess.SignOff
   Set olxSess = Nothing
   Set olxMsg = Nothing
   Exit Sub

olxErr:
   Err.Raise Err.Number, Err.Source, Err.Description
   Resume olxExit
End Sub

Public Sub olxSend_Previous()
    'Sends the Email
    Dim iRecipIndex As Integer, vSplitRecips As Variant, vRecip As Variant
    Dim i As Integer, attch As String, bUsePassword As Boolean
    Dim olxMsg As Object, olxSess As Object
    If olxSess Is Nothing Then Set olxSess = CreateObject("MSMAPI.MAPISession")
    If olxMsg Is Nothing Then Set olxMsg = CreateObject("MSMAPI.MAPIMessages")
    
    'Sign on to Mapi Session
    bUsePassword = (Me.SenderPassword <> "")
    olxSess.NewSession = True
    If bUsePassword = False Then
        olxSess.LogonUI = False
    Else
        olxSess.LogonUI = True
        olxSess.UserName = Me.SenderAddress
        olxSess.Password = Me.SenderPassword
    End If
    
    'Set other session settings
    olxSess.DownLoadMail = False
    olxSess.SignOn
    
    'Create Message using our session
    olxMsg.SessionID = olxSess.SessionID
    olxMsg.Compose
    
    'Add attachments to message
    For i = 1 To attachments.Count
        attch = attachments.Item(i)
        If Len(Dir(attch)) > 0 Then
            olxMsg.AttachmentIndex = i - 1  'must subtract 1 because this is 0-based index
            olxMsg.AttachmentPathName = attch
        End If
    Next
    
    'Add Recipient to message
    AddRecipient olxMsg, iRecipIndex, Me.RecipientAddress, Me.RecipientName, RecipientsTypes.MapToList
    
    'Add any Carbon Copies to message
    If Me.RecipientCC <> "" Then
        vSplitRecips = Split(Me.RecipientCC, ";")
        For Each vRecip In vSplitRecips
            AddRecipient olxMsg, iRecipIndex, CStr(vRecip), "", RecipientsTypes.mapCcList
        Next
    End If
    
    'Add any Blind Carbon Copies to message
    If Me.RecipientBlindCC <> "" Then
        vSplitRecips = Split(Me.RecipientBlindCC, ";")
        For Each vRecip In vSplitRecips
            AddRecipient olxMsg, iRecipIndex, CStr(vRecip), "", RecipientsTypes.mapBccList
        Next
    End If
    
    'Set any additional properties
    olxMsg.AddressResolveUI = False
    olxMsg.AddressEditFieldCount = 0  'No edit controls; only browsing is allowed
    olxMsg.AddressModifiable = True
    olxMsg.MsgSubject = Me.MessageSubject
    olxMsg.MsgNoteText = PropertyStruct.MessageText
    
    'Send it!
    olxMsg.send
   
olxExit:
   olxSess.SignOff
   Set olxSess = Nothing
   Set olxMsg = Nothing
   Exit Sub

olxErr:
   Err.Raise Err.Number, Err.Source, Err.Description
   Resume olxExit
End Sub

Private Sub AddRecipient(ByRef olxMsg As Object, ByRef iRecipIndex As Integer, ByVal sRecipAddy$, ByVal sRecipDispName$, ByVal iRecipType As RecipientsTypes)
    'Adds a recipient to our email
    olxMsg.RecipIndex = iRecipIndex
    iRecipIndex = iRecipIndex + 1  'increment the index so that the next recipient goes in
    olxMsg.RecipAddress = sRecipAddy
    If sRecipDispName <> "" Then olxMsg.RecipDisplayName = sRecipDispName 'only set if provided
    olxMsg.recipType = iRecipType
End Sub

Open in new window

0
Comment
Question by:JeffreyDurham
8 Comments
 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 250 total points
ID: 37746380
Outlook Express will always use the default account to send email. What you will need to do, is to change the default account (probably a registry setting) prior to the send (and then reset it back when finnished. As I don't have OE configured I can't help further.


Kelvin
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37747991
Yeah, This can be done in "Outlook", but I an not so sure about Outlook Express.
IMHO, once you need to do things like this, it is probably time to upgrade.

I mean if you even have "Multiple Accounts", you should really be using Outlook anyway.

Again, just my thoughts...
0
 
LVL 20

Assisted Solution

by:ElrondCT
ElrondCT earned 250 total points
ID: 37749801
kelvinsparks gave a good suggestion that I can follow up on. The identity used by OE if it can't ask you which to use is set in registry key HKEY_CURRENT_USER\Identities, key name "Default User ID". The value used is the GUID for the particular identity; those are listed as subkeys of the Identities key. If you want to let your user choose the identity to use, get the "Username" value from each GUID to display to the user to choose from.

The funny thing about tag-teaming with kelvinsparks is that my given name is actually Kelvin as well. It's not a common name...

I disagree with boag2000 about Outlook vs. OE. I actually found it easier to have completely separate identities in OE (I maintain about half a dozen email accounts which I keep distinct--personal email, office, a general account for my company, etc.). Outlook forces everything together, even if you have multiple email accounts. Sadly, Windows Live mail (the replacement for OE in Vista) followed the Outlook methodology, which pushed me to Thunderbird.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37749919
ElrondCT,

No argument here buddy,...
It all depends of how you wish to use the technology...

...and of course there is the cost of upgrading.
So yes, If OE does what you need, then, stick with that...

;-)

Jeff
0
 
LVL 2

Expert Comment

by:PhilChapmanJr
ID: 37754415
Try using the free  VBSendMail

http://www.freevbcode.com/ShowCode.asp?ID=109

 it’s much easier and you can use any email account as long as the account is on your ISP or you are using something like SMTP2GO.
0
 

Author Comment

by:JeffreyDurham
ID: 37773113
I like the idea of changing identities, that actually seems like it might work rather well. I was trying to figure out how that worked in OE actually the other day when I left off on this. As to upgrading to Outlook, I currently haven't yet written a class to use outlook so we only have it set up on some of the computers. I will admit that I personally do not like outlook since it has those dumb icons instead of menus (I hate Microsoft so much sometimes) and every time I try to email something with an attachment I /always/ get an error saying "file not found". Then I have to delete my attachment, readd it, and then send again. But we use Outlook Express mainly so that we can set up databases and programs with email capability. I downloaded the VBSendMail class to check it out, but if it's only the local ISP that's not going to work necessarily either for all cases. So I guess the approach I'm going to try is either mulitple identities or changing the default email account in the registry. I'll have to toy with it and see which works better.. If neither thing is programmable through the SMTP classes, I don't really have a problem altering the registry. There might be issues with me changing it while outlook express is open, I'll have to see..

~Jeffrey
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37885354
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now