excel - 发送带有附件和签名的 Outlook 电子邮件

标签 excel vba outlook

我需要发送带有附件和签名的 Outlook 电子邮件。

下面是我的 VBA 代码。

我收到错误“传输失败连接服务器”。看来我没有提供正确的 SMTP 服务器地址。

此外,我需要用公司 Logo 写签名。

Sub Outlook()

    Dim Mail_Object As Object
    Dim Config As Object
    Dim SMTP_Config As Variant
    Dim Email_Subject, Email_Send_From, Email_Send_To, Email_Cc, Email_Body As      String
    Dim Current_date As Date


    Current_date = DateValue(Now)
    Email_Subject = "Daily Pending IMs Report (" & Current_date & ")"
    Email_Send_From = "report@xxxx.ae"
    Email_Send_To = "yyyyyy@gmail.com"
    'Email_Cc = "vvvvvv@telenor.com.pk"

    Email_Body = "Dear All," & vbCrLf & "" & vbCrLf & "Kindly find Daily Pending IMs Report in the attached files."

    Set Mail_Object = CreateObject("CDO.Message")

    On Error GoTo debugs
    Set Config = CreateObject("CDO.Configuration")
    Config.Load -1
    Set SMTP_Config = Config.Fields
    With SMTP_Config
     .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
     .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
     .Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
     .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
     .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
     .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
     .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "report@xxxx.ae"
     .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "nnnnnn"
     .Update
    End With

    With Mail_Object
        Set .Configuration = Config
    End With

    'enter code here
    Mail_Object.Subject = Email_Subject
    Mail_Object.From = Email_Send_From
    Mail_Object.To = Email_Send_To
    Mail_Object.TextBody = Email_Body
    Mail_Object.cc = Email_Cc
    'Mail_Object.AddAttachment "C:\Pending IMs\Pending IMs.pdf"


    Mail_Object.Send

debugs:
    If Err.Description <> "" Then MsgBox Err.Description

End Sub

最佳答案

如果您使用的是 Outlook,则不需要 CDO.Configuration

只需删除所有配置,

'// Code will work on Outlook & Excel 2010
Option Explicit
Sub Outlook()
    Dim olItem As Object ' Outlook MailItem
    Dim App As Object ' Outlook Application
    Dim Email_Subject, Email_To, Email_Cc, Email_Body As String
    Dim Current_date As Date

    Set App = CreateObject("Outlook.Application")
    Set olItem = App.CreateItem(olMailItem) ' olMailItem

'   // add signature
    With olItem
        .Display
    End With

    Current_date = DateValue(Now)
    Email_Subject = "Daily Pending IMs Report (" & Current_date & ")"
    Email_To = "yyyyyy@gmail.com"

    Email_Body = "Dear All," & vbCrLf & "" & vbCrLf & "See Report in the attached files."

    Set olItem.SendUsingAccount = App.Session.Accounts.Item(2)

    With olItem
        .Subject = Email_Subject
        .To = Email_To
        .HTMLBody = Email_Body & vbCrLf & vbCrLf & .HTMLBody
        .Attachments.Add ("C:\Temp\file001.pdf") ' update Attachment Path
       '.Send ' Send directly
        .Display ' Display it
    End With

'    // Clean up
    Set olItem = Nothing
End Sub

请记住,代码将在 Outlook 和 Excel 上运行

在 Outlook 2010 上测试

关于excel - 发送带有附件和签名的 Outlook 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36001007/

相关文章:

vba - 查找并选择一个字符串

vba - 如何在 VBA 中加入集合

.net - Outlook 2003 加载项 - 创建 WPF 窗口后应用程序关闭时出现 COM 异常

VBA:从每个类别中提取顶部 'x' 条目

silverlight - 从 Outlook 拖放到 Silverlight 应用程序

outlook - 使用Outlook Redemption检索原始 header

vba - 在 VBA 中使用函数内部的数组

vba - Excel VBA从公式中删除等号

vba - 如何在 VBA 宏中应用 Excel 公式来查找倒数第二个单词

string - 根据匹配字符串获取子字符串