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

Excel 发送带有附件和签名的Outlook电子邮件,excel,vba,outlook,Excel,Vba,Outlook,我需要发送一封带有附件和签名的Outlook电子邮件 下面是我的VBA代码 我收到错误“传输失败,无法连接服务器”。似乎我没有给出正确的SMTPserver地址 此外,我需要写公司标志的签名 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

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

下面是我的VBA代码

我收到错误“传输失败,无法连接服务器”。似乎我没有给出正确的SMTPserver地址

此外,我需要写公司标志的签名

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()
Dim Mail_对象作为对象
Dim配置为对象
Dim SMTP_配置为变量
将电子邮件主题、电子邮件发送人、电子邮件发送人、电子邮件抄送人、电子邮件正文设置为字符串
将当前_日期设置为日期
当前日期=日期值(现在)
Email_Subject=“每日未决IMs报告(&U当前日期&”)
电子邮件\u发送\u发件人=”report@xxxx.ae"
电子邮件发送至“yyyyyy@gmail.com"
'Email_Cc='vvvvvv@telenor.com.pk"
电子邮件_Body=“亲爱的所有人”&vbCrLf&&vbCrLf&“请在附件中查找每日待处理的IMs报告。”
设置Mail_Object=CreateObject(“CDO.Message”)
关于错误转到调试
Set Config=CreateObject(“CDO.Configuration”)
Config.Load-1
设置SMTP_Config=Config.Fields
使用SMTP_配置
.项目(”http://schemas.microsoft.com/cdo/configuration/sendusing“”=2’NTLM方法
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpserver“”=“smtp.office365.com”
.项目(”http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpusessl“”=真
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.项目(”http://schemas.microsoft.com/cdo/configuration/sendusername") = "report@xxxx.ae"
.项目(”http://schemas.microsoft.com/cdo/configuration/sendpassword“”=“nnnnnn”
.更新
以
带邮件对象
Set.Configuration=Config
以
'在此处输入代码
Mail\u Object.Subject=电子邮件\u Subject
Mail\u Object.From=电子邮件发送
Mail\u Object.To=电子邮件发送到
Mail\u Object.TextBody=电子邮件\u Body
Mail\u Object.cc=电子邮件\u cc
'Mail_Object.AddAttachment“C:\Pending IMs\Pending IMs.pdf”
邮件发送
调试:
如果错误描述为“”,则MsgBox错误描述为“”
端接头

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

这不是vb.net,是VBscript吗?请相应地编辑您的标签。谢谢,谢谢。有些人,比如我。他们不太了解的标记被忽略了。其他人只搜索带有他们知道的标签的问题。因此,正确的标记可能会帮助您获得答案。有关更多信息,请参阅此代码。此代码运行良好,但此电子邮件是从我的基本out look帐户生成的。而不是根据我的“源代码”。由于我已配置了两个outlook帐户,我想将其发送到我的特定帐户。@user2317074 sry将代码添加到错误的位置,我现在已测试它应该可以工作,请参阅“立即更新”,应该是
Set Mail\u Object.SendUsingAccount=App.Session.accounts.Item(2)
在使用Mail\u Object之前@Om3r…它可以工作…非常感谢您的合作