ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 10789|回复: 5

求教:如何VBA调用Outlook用不同发件人发送邮件

[复制链接]

TA的精华主题

TA的得分主题

发表于 2016-10-3 18:04 | 显示全部楼层 |阅读模式
如题,求教各位老师,我现在outlook有两个不同的账户,希望能用vba调用不同的账户去发送邮件,求指教,我现在用的vba代码如下:Public Sub SendMail(ByVal strTo As String, ByVal strCC As String, ByVal strSubject As String, ByVal strBody As String, Optional ByVal strAttach1 As String, Optional ByVal strAttach2 As String, Optional ByVal strAttach3 As String)

Dim Outlook As Outlook.Application
Dim NewMail As mailitem

'Dim strTo, strCC, strSubject, strBody, strAttach As String

Dim otlk As Outlook.Application
Dim mailitem As Outlook.mailitem
Dim signature As String

Set Outlook = CreateObject("outlook.application")
Set NewMail = Outlook.CreateItem(olMailItem)

On Error GoTo errHandle
Set otlk = New Outlook.Application
Set mailitem = otlk.CreateItem(olMailItem)

signature = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(signature, vbDirectory) <> vbNullString Then
    signature = signature & Dir$(signature & "*.htm")
Else:
    signature = ""
End If
signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll

With mailitem
    .To = strTo
    .CC = strCC
    .Subject = strSubject
        If InStr("Important", strSubject) > 0 Then
        .Importance = olImportanceHigh
        End If
    .HTMLBody = strBody & signature
    If Len(strAttach1) <> 0 Then .Attachments.Add strAttach1
    If Len(strAttach2) <> 0 Then .Attachments.Add strAttach2
    If Len(strAttach3) <> 0 Then .Attachments.Add strAttach3
    .Send
End With
Do Until mailitem.Sent = True
    DoEvents
Loop

end sub

TA的精华主题

TA的得分主题

发表于 2016-11-22 10:40 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 tyxvba7529 于 2016-11-22 11:45 编辑

我以前也问过这个问题, 已解决。 请参考。
http://club.excelhome.net/thread-1130351-3-1.html">[url]http://club.excelhome.net/thread-1130351-3-1.html[/url]

  1. ' 发送单个邮件的子程序
  2. Sub SendMail(ByVal to_who As String, ByVal subject As String, ByVal body As String, ByVal attachement As String, ByVal CC As String)
  3.     Dim ObjOL As Object
  4.    Dim itmNewMail As Outlook.MailItem
  5.     Dim mailaddress As String
  6.     'Dim OutMail As Outlook.MailItem
  7.     'Dim OutApp As Outlook.Application
  8.     '引用Microsoft Outlook 对象
  9.     Set ObjOL = CreateObject("Outlook.Application")
  10.     Set itmNewMail = ObjOL.CreateItem(olMailItem)
  11.     With itmNewMail
  12.         .subject = subject  '主旨
  13.         .body = body   '正文本文
  14.         .To = to_who  '收件者
  15.         .Attachments.Add attachement '附件,如果你不需要发送附件,可以把这一句删掉即可,Excel中的第四列留空,不能删哦
  16.         .CC = CC  '抄送
  17.         .SendUsingAccount = ObjOL.Session.Accounts.Item(2)
  18.         .Display  '启动Outlook发送窗口
  19.         'SetTimer 0, 0, 0, AddressOf WinProcA
  20.     End With
  21.     Set ObjOL = Nothing
  22.     Set itmNewMail = Nothing
  23. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-11-30 16:23 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
tyxvba7529 发表于 2016-11-22 10:40
我以前也问过这个问题, 已解决。 请参考。
http://club.excelhome.net/thread-1130351-3-1.html">http:// ...

多谢@tyxvba7529!!正在测试你给的建议。
以下为我目前解决的方法,也供你参考:
我放弃outlook object的引用方法,而是用了CDO方法。代码如下:
Function SendMailGmail(ByVal strFrom As String, ByVal strTo As String, ByVal strCC As String, ByVal strSubject As String, ByVal strBcc As String, ByVal strBody As String, Optional ByVal strAttach1 As String, Optional ByVal strAttach2 As String, Optional ByVal strAttach3 As String)
   
Dim NewMail As CDO.Message
Dim signature As String

Set NewMail = New CDO.Message
  
'Enable SSL Authentication
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
  
'Make SMTP authentication Enabled=true (1)
  
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  
'Set the SMTP server and port Details
'To get these details you can get on Settings Page of your Gmail Account
  
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
  
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  
'Set your credentials of your Gmail Account
signature = Environ("appdata") & "\Microsoft\Signatures\"

If strFrom = "xxxxxxxxxx11@gmail.com" Then
    NewMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxxxxxxx11@gmail.com"
      
    NewMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxx"
   
    If Dir(signature, vbDirectory) <> vbNullString Then
        signature = signature & Dir$(signature & "xxx.htm")
    Else:
        signature = ""
    End If
    signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll
ElseIf strFrom = "xxxxxxxxxx222@gmail.com" Then
    NewMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxxxxxxx222@gmail.com"
      
    NewMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxx"
    If Dir(signature, vbDirectory) <> vbNullString Then
        signature = signature & Dir$(signature & "NewMail.htm")
    Else:
        signature = ""
    End If
    signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll
End If
'Update the configuration fields
NewMail.Configuration.Fields.Update
   
'Set All Email Properties

  
With NewMail
  .Subject = strSubject
  .From = strFrom
  .To = strTo
  .cc = strCC
  .BCC = strBcc
  .HTMLBody = strBody & signature
  .BodyPart.Charset = "GB18030" 'Big5"
  .HTMLBodyPart.Charset = "GB18030" '"Big5"
  
    If Len(strAttach1) <> 0 Then .AddAttachment strAttach1
    If Len(strAttach2) <> 0 Then .AddAttachment strAttach2
    If Len(strAttach3) <> 0 Then .AddAttachment strAttach3
  
End With
  
  
NewMail.Send
'MsgBox ("Mail has been Sent")
  
'Set the NewMail Variable to Nothing
Set NewMail = Nothing
   
End Function

TA的精华主题

TA的得分主题

发表于 2019-5-31 09:25 | 显示全部楼层
两位,问一下,如果想要获取第二帐号(非默认帐号)的收件箱,要怎么做?

TA的精华主题

TA的得分主题

发表于 2019-8-6 12:54 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
牛逼,学习,mark一下。

TA的精华主题

TA的得分主题

发表于 2019-11-13 13:34 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-18 13:14 , Processed in 0.038670 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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