【问题标题】:Send Outlook email with attachment and signature发送带有附件和签名的 Outlook 电子邮件
【发布时间】:2016-03-15 01:15:39
【问题描述】:

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

下面是我的 VBA 代码。

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

我还需要用公司标志写签名。

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

【问题讨论】:

  • 这不是 vb.net 是 VBscript 吗?请相应地编辑您的标签。谢谢
  • 已编辑........
  • 谢谢。有些人——比如我。将他们不太了解的标记标记为已忽略。其他人只搜索查看带有他们知道的标签的问题。所以正确的标记可能会帮助你得到答案。
  • 查看post了解更多信息

标签: excel vba outlook


【解决方案1】:

如果您使用的是 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 上测试

【讨论】:

  • 此代码运行良好,但是此电子邮件是从我的基本外观帐户生成的。而不是按照我的“来自代码”。因为我已经配置了两个 Outlook 帐户,我想将其发送给我的特定帐户。
  • @user2317074 sry 添加代码错误的地方,我已经测试现在它应该可以工作,现在看更新,应该是Set Mail_Object.SendUsingAccount = App.Session.Accounts.Item(2) 之前With Mail_Object
  • @Om3r...有效...非常感谢您的合作
猜你喜欢
  • 2018-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 2016-04-28
相关资源
最近更新 更多