Consulting

Results 1 to 6 of 6

Thread: Send recurring email from specific account

  1. #1
    VBAX Newbie
    Joined
    Mar 2008
    Posts
    5
    Location

    Send recurring email from specific account

    Hi guys

    I use the macro below to send a recurring email from an Outlook To-Do List task. The email is always sent from the "default" account. How can I modify the code so that the email is sent from a different account? I believe it can be done with "SendUsingAccount" but I'm not sure how to use it. I'd be grateful if a VBA expert could help with this.

    Private Sub Application_Reminder(ByVal Item As Object)
    Dim xMailItem As MailItem
    Dim strbody As String
    On Error Resume Next
    If Item.Class <> OlObjectClass.olTask Then Exit Sub
    If Item.Categories <> "Send Recurring Email" Then Exit Sub
    Set xMailItem = Outlook.Application.CreateItem(olMailItem)
    strbody = "<font face=Verdana><font size=2>" _
    & "Weekly email event"  'Body text
    With xMailItem
    .Subject = "Weekly Email"
    .To = "thisperson-at-someisp-dot-com"
    .HTMLBody = strbody
    'Not sure how to get this to work:
    Set .SendUsingAccount = _
    Session.Accounts.Item("thatperson-at-someotherisp-dot-com") 'Account to send from.
    .Send
    End With
    Set xMailItem = Nothing
    End Sub
    Last edited by Aussiebear; 11-28-2021 at 05:12 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    First find all the names of your accounts.

    Option Explicit
    
    Private Sub Which_Account()
        
        Dim I As Long
            
        For I = 1 To Session.Accounts.count
            Debug.Print "Account #" & I & ": " & Session.Accounts.Item(I)
        Next I
        
    End Sub
    
    
    Private Sub Application_Reminder(ByVal Item As Object)
        Dim xMailItem As mailItem
        Dim strbody As String
    
        ' Avoid bypassing errors unless the error handling
        '   is set up to purposely ignore expected errors
        'On Error Resume Next
    
        If Item.Class <> OlObjectClass.OlTask Then Exit Sub
        If Item.Categories <> "Send Recurring Email" Then Exit Sub
        Set xMailItem = Outlook.Application.CreateItem(olMailItem)
        strbody = "<font face=Verdana><font size=2>" _
          & "Weekly email event" 'Body text
        With xMailItem
            .Subject = "Weekly Email"
            .To = "thisperson-at-someisp-dot-com"
            .HTMLBody = strbody
            .SendUsingAccount = Session.Accounts("Found with Which_Account")  'Account to send from.
            .Display
            '.Send
        End With
        
        Set xMailItem = Nothing
     End Sub
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  3. #3
    VBAX Newbie
    Joined
    Mar 2008
    Posts
    5
    Location
    Many thanks skatonni, your solution worked perfectly for me! Your help is much appreciated.

  4. #4
    Hello

    What if we have 02 diffirent to do list tasks as I tried to write codes with reference to this but every time both mails are being sent evenwith single popup of the task.

  5. #5
    f

  6. #6
    Hey, make sure you verify the email addresses before sending because when you are using recurring method, the process will go on automation and If you get any bounce, your mail domain reputation might get hurt.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •