前言
公司的邮件系统用的是反人类的 Lotus notes, 你敢信?
最近要实现一个功能,邮件提醒功能,就是通过自动发送提醒邮件
前前后后这个问题搞了2天,由于公司的诸多条件限制,无法直接调用到公司发送邮件的接口,只有通过类似 Lotus script,VBA 等其他方式来实现。
用VBA代码实现发送邮件,其实我在n年前就实现过了
代码如下,网上一搜也一大堆
Function SendEmailbyNotesWithAttachement_2(Addresses, Attach, cc)
strSubject = ThisWorkbook.Sheets("EMAIL").Range("B1")
strbody = ThisWorkbook.Sheets("EMAIL").Range("A1")
'Declare Variables
Dim s As Object
Dim db As Object
Dim body As Object
Dim bodyChild As Object
Dim header As Object
Dim stream As Object
Dim host As String
Dim message As Object
' Notes variables
Set s = CreateObject("Notes.NotesSession")
Set db = s.CURRENTDATAbase
Set stream = s.CreateStream
' Turn off auto conversion to rtf
s.ConvertMIME = False
' Create message
Set message = db.CREATEdocument
message.Form = "memo"
message.Subject = strSubject
message.sendTo = Split(Addresses, ";")
message.CopyTo = cc
message.SaveMessageonSend = True
' Create the body to hold HTML and attachment
Set body = message.CreateMIMEEntity
'Child mime entity which is going to contain the HTML which we put in the stream
Set bodyChild = body.CreateChildEntity()
Call stream.WriteText(strbody)
Call bodyChild.SetContentFromText(stream, "text/HTML;charset=UTF-8", ENC_NONE)
Call stream.Close
Call stream.Truncate
' This will run though an array of attachment paths and add them to the email
For i = 0 To UBound(Attach)
strAttach = Attach(i)
If Len(strAttach) > 0 And Len(Dir(strAttach)) > 0 Then
' Get the attachment file name
pos = InStrRev(strAttach, "")
Filename = Right(strAttach, Len(strAttach) - pos)
'A new child mime entity to hold a file attachment
Set bodyChild = body.CreateChildEntity()
Set header = bodyChild.CreateHeader("Content-Type")
Call header.SetHeaderVal("multipart/mixed")
Set header = bodyChild.CreateHeader("Content-Disposition")
Call header.SetHeaderVal("attachment; filename=" & Filename)
Set header = bodyChild.CreateHeader("Content-ID")
Call header.SetHeaderVal(Filename)
Set stream = s.CreateStream()
If Not stream.Open(strAttach, "binary") Then
MsgBox "Open failed"
End If
If stream.Bytes = 0 Then
MsgBox "File has no content"
End If
Call bodyChild.SetContentFromBytes(stream, "application/octet-stream", ENC_IDENTITY_BINARY) ' All my attachments are excel this would need changing depensding on your attachments.
End If
Next
'Send the email
Call message.Send(False)
s.ConvertMIME = True ' Restore conversion
End Function
VBA
但是现实情况是这样的
我们需要邮件从公邮发送出去
何谓公邮:整个Team使用的邮箱,如***admin@email.com 之类的邮箱
使用过反人类的 Lotus notes 都知道公邮是需要先打开个人邮箱才能进去的
于是当我把以上的VBA 代码增加如下代码,设置从公邮里面发送邮件后
Server = "C******rS***/****"; string NotesDBName = @"**********.nsf"; string mailTo = "****t**@***.com"; string mailSubject = DateTime.Now.ToString(); string mailBoby = "
| Month | Savings |
|---|---|
| January | $100 |
期间还遇到
由于这句代码放置的位置不对,导致显示不正确
doc.AppendItemValue("Principal", "C**********am");//设置邮件的发件人昵称
最终突破的那一刻心情真的很爽,虽然到到现在仍然不知道不要密码的原因,但总归解决了困惑两天的问题,不敢独享
有时候就是听别人说,这条路走不通,就不走了
有时候就是听别人说,已经封装好了,直接调吧,就调了而不知如何实现
有时候就是抄作业,以为自己会了,于是真真用的时候就不知道了
年前终于开始不那么忙了,欠了那么多,该慢慢补回来了
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。



