PDA

View Full Version : Send recurring email from specific account



Andy505
11-29-2017, 01:08 PM
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

skatonni
11-29-2017, 02:27 PM
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

Andy505
11-29-2017, 03:38 PM
Many thanks skatonni, your solution worked perfectly for me! Your help is much appreciated.

suryakant02
05-04-2018, 05:24 PM
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.

techguy4557
10-11-2021, 06:17 AM
f

techguy4557
10-11-2021, 06:19 AM
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. :yes