使用 Excel 从 Lotus Notes 发送电子邮件并具有附件和 HTML 正文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2501496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:32:44  来源:igfitidea点击:

Sending Email from Lotus Notes using Excel and having Attachment & HTML body

htmlemailvbaattachmentlotus

提问by Anthony

Right I'm trying to send an Email form an excel spreadsheet though lotus notes, it has an attachment and the body needs to be in HTML.

是的,我正在尝试通过 Lotus Notes 向 Excel 电子表格发送电子邮件,它有一个附件,正文需要采用 HTML 格式。

I've got some code that from all I've read should allow me to do this however it doesn't. Without the HTML body the attachment will send, when I impliment a HTML body the Email still sends but the attachment dissapears, I've tried rearanging the order of the code cutting out bits that might not be needed but all is invain.

我有一些代码,从我读过的所有内容中应该可以让我这样做,但事实并非如此。如果没有附件将发送的 HTML 正文,当我暗示电子邮件仍发送 HTML 正文但附件消失时,我尝试重新排列代码的顺序,删除可能不需要的位,但一切都是徒劳的。

(You need to reference Lotus Domino Objects to run this code. strEmail is the email addresses strAttach is the string location of the attachment strSubject is the subject text strBody is the body text )

(您需要引用 Lotus Domino Objects 来运行此代码。strEmail 是电子邮件地址 strAttach 是附件的字符串位置 strSubject 是主题文本 strBody 是正文文本)

Sub Send_Lotus_Email(strEmail, strAttach, strSubject, strBody)

Dim noSession As Object, noDatabase As Object, noDocument As Object
Dim obAttachment As Object, EmbedObject As Object

Const EMBED_ATTACHMENT As Long = 1454

Set noSession = CreateObject("Notes.NotesSession")
Set noDatabase = noSession.GETDATABASE("", "")

'If Lotus Notes is not open then open the mail-part of it.
If noDatabase.IsOpen = False Then noDatabase.OPENMAIL

'Create the e-mail and the attachment.
Set noDocument = noDatabase.CreateDocument
Set obAttachment = noDocument.CreateRichTextItem("strAttach")
Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", strAttach)

'Add values to the created e-mail main properties.
With noDocument
    .Form = "Memo"
    .SendTo = strEmail
    '.Body = strBody ' Where to send the body if HTML body isn't used.
    .Subject = strSubject
    .SaveMessageOnSend = True
End With

noSession.ConvertMIME = False
Set Body = noDocument.CreateMIMEEntity("Body") ' MIMEEntity to support HTML
Set stream = noSession.CreateStream
Call stream.WriteText(strBody) ' Write the body text to the stream
Call Body.SetContentFromText(stream, "text/html;charset=iso-8859-1", ENC_IDENTITY_8BIT)
noSession.ConvertMIME = True

 'Send the e-mail.
With noDocument
    .PostedDate = Now()
    .Send 0, strEmail
End With

 'Release objects from the memory.
Set EmbedObject = Nothing
Set obAttachment = Nothing
Set noDocument = Nothing
Set noDatabase = Nothing
Set noSession = Nothing

End Sub

结束子

If somone could point me in the right direction I'd be greatly appreciated.

如果有人能指出我正确的方向,我将不胜感激。

Edit: I've done a little more investigating and I've found an oddity, if i look at the sent folder the emails all have the paperclip icon of having an attachment even though when you go into the email even in the sent the HTML ones don't show an attachment.

编辑:我做了更多的调查,发现了一个奇怪的地方,如果我查看已发送的文件夹,即使当您进入电子邮件时,即使在发送的 HTML 中,电子邮件也都有带有附件的回形针图标不显示附件。

回答by Anthony

I have managed to solve my own problem.

我已经设法解决了我自己的问题。

In teh same way you create a MIME entry and stream in the HTML you need to do the same with the attachment, you also need to put them both inside a MIME entry within the email itself to hold both the HTML and Attachment at the same level otherwise you end up with a situation of the email with the body and a child entry of the attachment which is within another attachment. (it's odd but true) Thus this is my solution:

以同样的方式在 HTML 中创建 MIME 条目和流,您需要对附件执行相同的操作,您还需要将它们都放在电子邮件本身的 MIME 条目中,以将 HTML 和附件保持在同一级别否则,您最终会遇到带有正文的电子邮件和另一个附件中的附件子条目的情况。(这很奇怪但确实如此)因此这是我的解决方案:

    Sub Send_Lotus_Email(Addresses, Attach, strSubject, strBody)

'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 = Addresses
 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=iso-8859-1", 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/msexcel", 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 Sub

回答by Tim

Here is my actual code. I'm not even using strong type.

这是我的实际代码。我什至没有使用强类型。

 Dim mobjNotesSession As Object      ' Back-end session reference'
 Dim bConvertMime As Boolean
 Dim stream As Object
 Dim mimeHtmlPart As Object
 Const ENC_QUOTED_PRINTABLE = 1726

 mobjNotesSession = CreateObject("Lotus.NotesSession")
 mobjNotesSession.Initialize()

 mobjNotesDatabase = mobjNotesSession.GetDatabase("HQ2", "tim4")
 mobjNotesDocument = mobjNotesDatabase.CreateDocument

 bConvertMime = mobjNotesSession.ConvertMime
 mobjNotesSession.ConvertMime = False
 stream = mobjNotesSession.CreateStream()
 Call stream.WriteText(txtBody.Text)

 mobjNotesBody = mobjNotesDocument.CreateMIMEEntity("Body")
 mimeHtmlPart = mobjNotesBody.CreateChildEntity()  'This returns "Type Mismatch" error'
 Call mimeHtmlPart.SetContentFromText(stream, "text/html; charset=""iso-8859-1""", ENC_QUOTED_PRINTABLE)

 Call stream.Close()
 mobjNotesSession.ConvertMime = bConvertMime
 Call mobjNotesDocument.CloseMIMEEntities(True, "Body")

回答by Anthony

No sorry I didn't i was running this in VBA which isn't strong typed so i can get away with not knowing the actual variable type for the body identity. i 've not been able to test this but I belive you need to reset the declarations to

不抱歉,我没有在非强类型的 VBA 中运行它,所以我可以摆脱不知道主体标识的实际变量类型。我无法对此进行测试,但我相信您需要将声明重置为

Dim bodyChild As NotesMIMEEntity

Dim bodyChild As NotesMIMEEntity

this is the one you have trouble with the ones bellow you may find cause problems as well

这是您遇到麻烦的问题,您可能会发现以下问题也可能导致问题

Dim s As New NotesSession

Dim db As NotesDatabase 

Dim body As NotesMIMEEntity 

Dim header As NotesMIMEHeader 

Dim stream As NotesStream 

Dim host As String 

Dim message As NotesDocument

Hope this helps

希望这可以帮助