发送电子邮件CDO

时间:2015-03-13 23:10:09

标签: excel vba excel-vba

我正在尝试使用CDO发送PDF和Excel电子表格页面。 我为大多数ISP提供它但我不能使它适用于gmail。

我有一个帐户,当我尝试它时,它偶尔会工作(去图)。我也有一个朋友有一个Gmail帐户,我无法让它工作......永远与他的帐户。

我已经为此工作了3天,我放弃了。 要完成它,我需要更好的人才。 以下是我尝试过的代码但没有成功。

请帮忙。

  Sub SEND_PDF_SHEET_WITH_CDO()

On Error GoTo ErrHandler3:

Dim filepath As String

filepath = Environ$("temp") & "\" & ActiveWorkbook.Name   & ".pdf" 'TODO:change filepath for the temp pdf file

    Range("A5:P31").Select

    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        filepath, _
        Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False

Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields

    With Flds

    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtp.gmail.com
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25   ' I have tried 25, 465, 587 and more
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "MyEmail"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = MyPassword
    .Update
    End With

    With iMsg
        Set .Configuration = iConf
        .From = "MyEmail" & "<NCAA@something.nl>" 'TODO:change email address here
        .To = "MyEmail"
        .Subject = "Hello"
        .HTMLBody = Range("A350").Value
        .AddAttachment (filepath)
        .Send
    End With

    Set iMsg = Nothing
    Set iConf = Nothing
    Kill filepath

    Exit Sub
ErrHandler3:

MsgBox "YOUR   PDF   E-MAIL DID NOT GO THROUGH.  IT MAY BE YOU" _
& Chr$(13) _
& Chr$(13) _
& "HAVE NOT COMPLETED YOUR NON-OULOOK E-MAIL QUESTIONS" _
& Chr$(13) _
& Chr$(13) _
& "OR ENTERED THE INFORMATION INCORRECTLY." _
& Chr$(13) _
& Chr$(13) _
& "PLEASE TRY AGAIN AFTER RE-ENTERING YOUR INFORMATION."
Range("B8").Select
STOP_SUB = "YES"

    Set iMsg = Nothing
    Set iConf = Nothing
     Kill filepath

Range("A1").Select
End Sub

Sub SEND_EXCEL_SHEET_WITH_CDO()

 On Error GoTo ErrHandler2:

'Working in 97-2007
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim iMsg As Object
    Dim iConf As Object
        Dim Flds As Variant

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook

    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007
            'We exit the sub when your answer is NO in the security dialog that you only
            'see  when you copy a sheet from a xlsm file with macro's disabled.
            If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox "Your answer is NO in the security dialog"
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End If
    End With

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        .Close savechanges:=False
    End With

    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1    ' CDO Source Defaults
    Set Flds = iConf.Fields
    With Flds

    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = Range("JA1").Value
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Range("JA2").Value
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = Range("JA3").Value
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Range("JA4").Value
    .Update
    End With

    With iMsg
        Set .Configuration = iConf
        .To = "MyEmail"
        .CC = ""
        .BCC = ""
        .From = "My Name" & "<NCAA@something.nl>"
        .Subject = "HELLO"
        .TextBody = "HELLO AGAIN" '<-- email body
        .AddAttachment TempFilePath & TempFileName & FileExtStr
        .Send
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

Exit Sub
ErrHandler2:

MsgBox "YOUR    EXCEL    E-MAIL DID NOT GO THROUGH.  IT MAY BE YOU" _
& Chr$(13) _
& Chr$(13) _
& "HAVE NOT COMPLETED YOUR NON-OULOOK E-MAIL QUESTIONS" _
& Chr$(13) _
& Chr$(13) _
& "OR ENTERED THE INFORMATION INCORRECTLY." _
& Chr$(13) _
& Chr$(13) _
& "PLEASE TRY AGAIN AFTER RE-ENTERING YOUR INFORMATION."
Range("B8").Select
STOP_SUB = "YES"
Kill TempFilePath & TempFileName

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With

Application.DisplayAlerts = False

ActiveWorkbook.Close

End Sub

1 个答案:

答案 0 :(得分:2)

此代码有效。 PLUS 它会显示任何错误,告诉您它无法正常工作的原因。

Set emailObj      = CreateObject("CDO.Message")
emailObj.From     = "dc@gmail.com"

emailObj.To       = "dc@gmail.com"

emailObj.Subject  = "Test CDO"
emailObj.TextBody = "Test CDO"

emailObj.AddAttachment "C:/Users/User/Desktop/err.fff"

Set emailConfig = emailObj.Configuration

emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")    = 2  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")      = true 
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")    = "dc"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")    = "Ss"
emailConfig.Fields.Update

On Error Resume Next
emailObj.Send

If err.number = 0 then 
    Msgbox "Done"
Else
    Msgbox err.number & " " & err.description
    err.clear
End If

此外,您在www.gmail.com的帐户需要设置为允许SMTP访问。

配置信息来自Outlook Express(WinXP中的最后一个,在Vista中重命名为Windows Mail,从Win7及更高版本中删除)。这显示了计算机上的默认配置。

Set emailObj      = CreateObject("CDO.Message")

Set emailConfig = emailObj.Configuration

On Error Resume Next    

For Each fld in emailConfig.Fields
msgbox fld.name & " = " & fld
Next

Windows 2000的所有版本/版本并不总是包含Windows 2000的CDO。见http://support.microsoft.com/en-au/kb/171440