Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 5556|回复: 1

[已解决]通过VBA调用CDO发邮件 抄送的代码是什么?

[复制链接]
发表于 2013-5-27 19:30 | 显示全部楼层 |阅读模式
本帖最后由 jianwu1106 于 2013-5-27 19:52 编辑

通过VBA调用CDO发邮件 抄送的代码是什么? 也是TO 吗?
最佳答案
2013-5-27 22:34
  1. Option Explicit

  2. 'If you have a GMail account then you can try this example to use the GMail smtp server
  3. 'The example will send a small text message
  4. 'You must change four code lines before you can test the code

  5. '.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
  6. '.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"

  7. 'Use your own mail address to test the code in this line
  8. '.To = "Mail address receiver"

  9. 'Change YourName to the From name you want to use
  10. '.From = """YourName"" <Reply@something.nl>"

  11. 'If you get this error : The transport failed to connect to the server
  12. 'then try to change the SMTP port from 25 to 465

  13. Sub CDO_Mail_Small_Text_2()
  14.     Dim iMsg As Object
  15.     Dim iConf As Object
  16.     Dim strbody As String
  17.     Dim Flds As Variant

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

  20.     iConf.Load -1    ' CDO Source Defaults
  21.     Set Flds = iConf.Fields
  22.     With Flds
  23.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
  24.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  25.         .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
  26.         .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"
  27.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

  28.         .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  29.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  30.         .Update
  31.     End With

  32.     strbody = "Hi there" & vbNewLine & vbNewLine & _
  33.               "This is line 1" & vbNewLine & _
  34.               "This is line 2" & vbNewLine & _
  35.               "This is line 3" & vbNewLine & _
  36.               "This is line 4"

  37.     With iMsg
  38.         Set .Configuration = iConf
  39.         .To = "Mail address receiver"
  40.         .CC = ""
  41.         .BCC = ""
  42.         ' Note: The reply address is not working if you use this Gmail example
  43.         ' It will use your Gmail address automatic. But you can add this line
  44.         ' to change the reply address  .ReplyTo = "Reply@something.nl"
  45.         .From = """YourName"" <Reply@something.nl>"
  46.         .Subject = "Important message"
  47.         .TextBody = strbody
  48.         .Send
  49.     End With

  50. End Sub
复制代码
发表于 2013-5-27 22:34 | 显示全部楼层    本楼为最佳答案   
  1. Option Explicit

  2. 'If you have a GMail account then you can try this example to use the GMail smtp server
  3. 'The example will send a small text message
  4. 'You must change four code lines before you can test the code

  5. '.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
  6. '.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"

  7. 'Use your own mail address to test the code in this line
  8. '.To = "Mail address receiver"

  9. 'Change YourName to the From name you want to use
  10. '.From = """YourName"" <Reply@something.nl>"

  11. 'If you get this error : The transport failed to connect to the server
  12. 'then try to change the SMTP port from 25 to 465

  13. Sub CDO_Mail_Small_Text_2()
  14.     Dim iMsg As Object
  15.     Dim iConf As Object
  16.     Dim strbody As String
  17.     Dim Flds As Variant

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

  20.     iConf.Load -1    ' CDO Source Defaults
  21.     Set Flds = iConf.Fields
  22.     With Flds
  23.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
  24.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  25.         .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
  26.         .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"
  27.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

  28.         .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  29.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  30.         .Update
  31.     End With

  32.     strbody = "Hi there" & vbNewLine & vbNewLine & _
  33.               "This is line 1" & vbNewLine & _
  34.               "This is line 2" & vbNewLine & _
  35.               "This is line 3" & vbNewLine & _
  36.               "This is line 4"

  37.     With iMsg
  38.         Set .Configuration = iConf
  39.         .To = "Mail address receiver"
  40.         .CC = ""
  41.         .BCC = ""
  42.         ' Note: The reply address is not working if you use this Gmail example
  43.         ' It will use your Gmail address automatic. But you can add this line
  44.         ' to change the reply address  .ReplyTo = "Reply@something.nl"
  45.         .From = """YourName"" <Reply@something.nl>"
  46.         .Subject = "Important message"
  47.         .TextBody = strbody
  48.         .Send
  49.     End With

  50. End Sub
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-15 14:29 , Processed in 0.222746 second(s), 4 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表