Pergunta

Certo, estou tentando enviar um formulário de e -mail uma planilha do Excel, embora a Lotus Notes, ela tem um anexo e o corpo precisa estar no HTML.

Eu tenho algum código de que, de tudo o que li, deve me permitir fazer isso como não. Sem o corpo HTML, o acessório enviará, quando eu implementar um corpo HTML que o email ainda envia, mas o anexo desapego, tentei rearranger a ordem do código que corta os bits que podem não ser necessários, mas tudo está invasão.

(Você precisa fazer referência a objetos Lotus Domino para executar este código. Stremail é o endereço de e -mail que o strattach é a localização da string do anexo strsubject é o texto em que o texto strbody é o texto do corpo)

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

Final sub

Se alguém pudesse me apontar na direção certa, eu seria muito apreciado.

EDIT: Eu fiz um pouco mais de investigação e encontrei uma estranheza, se eu olhar para a pasta enviada, todos os e -mails têm o ícone do clipe de papel de ter um anexo, embora quando você entra no e -mail, mesmo no HTML enviado pelo HTML os não mostram um apego.

Foi útil?

Solução

Consegui resolver meu próprio problema.

Da mesma maneira que você cria uma entrada MIME e o fluxo no HTML, você precisa fazer o mesmo com o anexo, também precisa colocá -los em uma entrada MIME no próprio email para manter o HTML e o anexo no mesmo nível Caso contrário, você acaba com uma situação do email com o corpo e uma entrada infantil do anexo que está dentro de outro anexo. (é estranho, mas verdadeiro), portanto, esta é a minha solução:

    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

Outras dicas

Aqui está o meu código real. Eu nem estou usando o tipo forte.

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

Não, desculpe, eu não estava executando isso no VBA, o que não é forte, para que eu possa me safar de não saber o tipo de variável real para a identidade do corpo. Não fui capaz de testar isso, mas acredito que você precisa redefinir as declarações para

Dim bodyChild As NotesMIMEEntity

Este é o que você tem problemas com aqueles que você pode encontrar causar problemas também

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

Espero que isto ajude

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top