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

时间:2010-03-23 16:02:29

标签: html email vba attachment lotus

是的,我正在尝试通过Lotus笔记发送电子邮件表格excel电子表格,它有一个附件,并且正文需要是HTML格式。

我有一些代码,我已经读过的所有代码应该允许我这样做,但事实并非如此。 如果没有附件发送的HTML正文,当我发送电子邮件仍然发送的HTML正文但附件消失时,我已经尝试重新排列代码的顺序,删除可能不需要的所有内容,但所有内容都是隐藏的。

(您需要引用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

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

编辑: 我已经做了一些调查,我发现了一个奇怪的问题,如果我看一下发送的文件夹,电子邮件都有附件的回形针图标,即使你进入电子邮件,即使在发送的HTML中也没有不显示附件。

3 个答案:

答案 0 :(得分:3)

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

以同样的方式在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

答案 1 :(得分:1)

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

 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")

答案 2 :(得分:0)

不,对不起,我没有在VBA中运行这个不强类型,所以我可以逃脱不知道身体身份的实际变量类型。我无法测试这个,但我相信你需要将声明重置为

Dim bodyChild As NotesMIMEEntity

这是你遇到麻烦的人,你可能会发现问题

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

希望这有帮助