|
楼主 |
发表于 2021-11-30 19:20
|
显示全部楼层
试试看,代码没测试,如果有错调试下
先生成邮件附件在制定位置
然后发送该附件
- Sub CDOSENDEMAIL()
- Dim CDOMail As Variant
- On Error Resume Next '出错后继续执行
- Application.DisplayAlerts = False '禁用系统提示
- '生成附件工作簿
- p = ThisWorkbook.Path
- f = "邮件的附件.xlsx"
- Sheets(Array("表名1", "表名2")).Copy
- ActiveWorkbook.SaveAs Filename:=p & "" & f, FileFormat:=51
- ActiveWindow.Close
- Set CDOMail = CreateObject("CDO.Message") '创建对象
- CDOMail.From = "10000@qq.com" '设置发信人的邮箱
- CDOMail.To = "10000@qq.com" '设置收信人的邮箱
- CDOMail.Subject = "主题:用CDO发送邮件试验" '设定邮件的主题
- 'CDOMail.TextBody = "文本内容" '使用文本格式发送邮件
- CDOMail.HtmlBody = "当您看到此封邮件,表明CDO设置正确" '使用Html格式发送邮件
- CDOMail.AddAttachment p & "" & f '发送本工作簿为附件
- stUl = "http://schemas.microsoft.com/cdo/configuration/" '微软服务器网址
- With CDOMail.Configuration.Fields
- .Item(stUl & "smtpusessl") = True
- .Item(stUl & "smtpserver") = "smtp.qq.com" 'SMTP服务器地址
- .Item(stUl & "smtpserverport") = 465 'SMTP服务器端口
- .Item(stUl & "sendusing") = 2 '发送端口
- .Item(stUl & "smtpauthenticate") = 1 '远程服务器需要验证
- .Item(stUl & "sendusername") = "10000" '发送方邮箱名称
- .Item(stUl & "sendpassword") = 上面连接生成的授权码,非你qq邮箱密码" '发送方邮箱密码
- .Item(stUl & "smtpconnectiontimeout") = 60 '连接超时(秒)
- .Update
- End With
- CDOMail.Send '执行发送
- Set CDOMail = Nothing '发送成功后即时释放对象
- If Err.Number = 0 Then
- MsgBox "成功发送邮件", , "温馨提示" '如果没有出错,则提示发送成功
- Else
- MsgBox Err.Description, vbInformation, "邮件发送失败" '如果出错,则提示错误类型和错误代码
- End If
- Application.DisplayAlerts = True '恢复系统提示
- End Sub
复制代码
|
|