我正在使用以下代碼使用 VBA 通過 Outlook 發送電子郵件。我正在使用我的作業計算機和我的作業電子郵件作為主帳戶,但想從另一個登錄的帳戶發送。由于我是 VBA 的新手,我還沒有設法集成我在網上找到的任何代碼。其余代碼按需要作業,只是發件人帳戶是問題所在。
任何人都可以幫助修改代碼嗎?(下面的代碼沒有我試圖修復它)
Sub Test1()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strbody As String
For Each cell In Range("D2:D2")
strbody = strbody & cell.Value & vbNewLine
Next
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "C").Value) = "yes" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "A personal message from the founder"
.Body = "Hi " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
uj5u.com熱心網友回復:
你的意思是從另一個電子郵件地址發送?如果是這樣,只需添加:
.SentOnBehalfOfName = "[email protected]" 'Change to the email address you want to send from
例如
With OutMail
.To = cell.Value
.SentOnBehalfOfName = "[email protected]"
.Subject = "A personal message from the founder"
.Body = "Hi " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
.Send
End With
uj5u.com熱心網友回復:
Outlook 中有兩種可能的方法:
- 如果在 Outlook 中配置了另一個帳戶,則需要使用MailItem.SendUsingAccount屬性,該屬性回傳或設定一個
Account物件,該物件表示要在其下MailItem發送郵件的帳戶。
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("[email protected]")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
- 如果您已獲得 Exchange 管理員設定的代表其他人發送的權限,則您需要使用MailItem.SentOnBehalfOfName屬性,該屬性回傳一個字串,指示郵件的預期發件人的顯示名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417491.html
標籤:
