设为首页 收藏本站
查看: 772|回复: 0

[经验分享] Use Outlook or Lotus.Notes to send email (Need to add relevant reference .)

[复制链接]

尚未签到

发表于 2015-9-13 04:50:22 | 显示全部楼层 |阅读模式
  Private Sub SendToAll_Click()
    Dim strErrMsg As String 'For Error Handling
    Dim olApp As New Outlook.Application
    Dim olNameSpace As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    Dim oleGrf As Object
    Dim strFileName As String
  Dim MySQL As String, j, i As Integer
    On Error GoTo MailError
    Set olNameSpace = olApp.GetNamespace("MAPI")
    For j = 0 To CustList.ListCount - 1
'=================循环处理客户,每客户一个EXCEL表,命名为客户名+日期      
      For i = 0 To TabList.ListCount - 1 '============循环处理表
         MySQL = "select * from TempExcelTab_" & TabList.Column(0, i) &
" WHERE  客户='" & CustList.Column(0, j) & "'"
  '=======导出到EXCEL
   If HasQuery(TabList.Column(1, i)) Then DoCmd.DeleteObject acQuery, TabList.Column(1, i)
           
    CurrentDb.CreateQueryDef TabList.Column(1, i), MySQL
         
   DoCmd.TransferSpreadsheet transfertype:=acExport, tablename:=CurrentDb.QueryDefs(TabList.Column(1, i)).Name, _
   FileName:=tFilePath & CustList.Column(0, j) & Format(Date, "YYYYMMDD") & ".xls", Hasfieldnames:=True, SpreadsheetType:=5
               
         'DoCmd.Close acQuery, TabList.Column(1, i), acSaveNo
         
      Next i
'==================循环处理完一个表
      
       SysCmd acSysCmdSetStatus, "正在发送邮件给" & CustList.Column(0, j) & "..."
       Me.LabState.Caption = "正在发送邮件给" & CustList.Column(0, j) & "...": Me.Repaint
  '===========自动Mail过程开始﹍
         Set olMail = olApp.CreateItem(olMailItem)
         strFileName = tFilePath & CustList.Column(0, j) &
                               Format(Date, "YYYYMMDD") & ".xls"
  With olMail
            .To = Trim(CustList.Column(1, j))
            If Not ((Nz(CustList.Column(2, j), "NO") = "NO") And
                                       (Trim(CustList.Column(2, j)) <> &quot;&quot;)) Then
              .CC = CustList.Column(2, j)
            End If
            .Subject = &quot;Customer's automation docs from TaiShan ELECTRONIC CO.,LTD&quot;
            .Body = CustList.Column(3, j) & vbNewLine & vbNewLine & &quot;Hello,&quot; & vbNewLine _
              & &quot;The attachments are made by automation,if you find any errors,please submit your advises.&quot; _& vbNewLine & vbNewLine & &quot;Best RGS!&quot;
            .Attachments.Add strFileName
            .ReadReceiptRequested = False
            .Send
        End With
             'Kill strFileName   ' 处理完后删除文件
        Set olMail = Nothing
        Set oleGrf = Nothing
        
        StateList.RowSource = CustList.Column(0, j) & &quot;; 成功发送!;&quot; & StateList.RowSource
       SysCmd acSysCmdSetStatus, &quot;正在发送邮件给&quot; & CustList.Column(0, j) & &quot;......成功&quot;
       Me.LabState.Caption = &quot;正在发送邮件给&quot; & CustList.Column(0, j) &
                                          &quot;......成功&quot;: Me.Repaint
       Me.LabState.Caption = &quot; &quot;: Me.Repaint
'===========Email过程结束
  Next j '====================处理完一个客户

MailError:   Set olApp = Nothing
    Set olApp = Nothing
    SysCmd acSysCmdClearStatus
  End Sub
  ==================================================
==================================================
'SendNotesMail &quot;Test send lotus mail subject&quot;, &quot;&quot;,
&quot;james.wong@tais.com&quot;, &quot;OK,test it&quot;, True
  '============Public Sub SendNotesMail(Subject as string, attachment as string,
'recipient as string, bodytext as string,saveit as Boolean)
'This public sub will send a mail and attachment if neccessary to the
'recipient including the body text.
'Requires that notes client is installed on the system.
Function SendNotesMail(Subject As String, Attachment As String,
Recipient As String, BodyText As String, SaveIt As Boolean)
'Set up the objects required for Automation into lotus notes
    Dim Maildb As Object 'The mail database
    Dim UserName As String 'The current users notes name
    Dim MailDbName As String 'THe current users notes mail database name
    Dim MailDoc As Object 'The mail document itself
    Dim AttachME As Object 'The attachment richtextfile object
    Dim Session As Object 'The notes session
    Dim EmbedObj As Object 'The embedded object (Attachment)
    'Start a session to notes
    Set Session = CreateObject(&quot;Notes.NotesSession&quot;)
    'Next line only works with 5.x and above. Replace password with your password
    'Session.Initialize (&quot;password&quot;)
    'Get the sessions username and then calculate the mail file name
    'You may or may not need this as for MailDBname with some systems you
    'can pass an empty string or using above password you can use other mailboxes.
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) & Right$(UserName,
(Len(UserName) - InStr(1, UserName, &quot; &quot;))) & &quot;.nsf&quot;
    'Open the mail database in notes
    Set Maildb = Session.GETDATABASE(&quot;&quot;, MailDbName)
     If Maildb.ISOPEN = True Then
          'Already open for mail
     Else
         Maildb.OPENMAIL
     End If
    'Set up the new mail document
    Set MailDoc = Maildb.CREATEDOCUMENT
    MailDoc.Form = &quot;Memo&quot;
    MailDoc.sendto = Recipient
    MailDoc.Subject = Subject
    MailDoc.Body = BodyText
    MailDoc.SAVEMESSAGEONSEND = SaveIt
    'Set up the embedded object and attachment and attach it
    If Attachment <> &quot;&quot; Then
        Set AttachME = MailDoc.CREATERICHTEXTITEM(&quot;Attachment&quot;)
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, &quot;&quot;, Attachment, &quot;Attachment&quot;)
        MailDoc.CREATERICHTEXTITEM (&quot;Attachment&quot;)
    End If
    'Send the document
    MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
    MailDoc.Send 0, Recipient
    'Clean Up
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set AttachME = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing
End Function

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-112769-1-1.html 上篇帖子: .NET中如何用Outlook自动发送邮件(C#) 下篇帖子: OutLook解析邮件头的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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