Link to home
Start Free TrialLog in
Avatar of Jeanette Durham
Jeanette DurhamFlag for United States of America

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of Kelvin Sparks
Kelvin Sparks
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
Avatar of Jeanette Durham

ASKER

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
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.