1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| Public Sub CDOSendMail(SendFrom As String, _
SendTo As String, _
Subject As String, _
PlainTextBody As String, _
FullPathFileName As String)
Dim cdoMail As CDO.Message
Dim iBp As CDO.IBodyPart ' for IBodyPart on message
Dim iBp1 As CDO.IBodyPart
Dim Flds As ADODB.Fields
Dim Stm As ADODB.Stream
Set cdoMail = New CDO.Message
With cdoMail
.From = SendFrom
.To = SendTo
.Subject = Subject
''Set iBp = .BodyPart
Set iBp = cdoMail '??
' TEXT BODYPART
' Add the body part for the text/plain part of message
Set iBp1 = iBp.AddBodyPart
' set the fields here
Set Flds = iBp1.Fields
Flds("urn:schemas:mailheader:content-type") = "text/plain; charset=""iso-8859-1"""
Flds.Update
' get the stream and add the message
Set Stm = iBp1.GetDecodedContentStream
Stm.WriteText PlainTextBody
Stm.Flush
' HTML BODYPART
' Do the HTML part here
Set iBp1 = iBp.AddBodyPart
' set the content-type field here
Set Flds = iBp1.Fields
Flds("urn:schemas:mailheader:content-type") = "text/html"
Flds.Update
' get the stream and add message HTML text to it
Set Stm = iBp1.GetDecodedContentStream
Stm.WriteText "<HTML><H1>this is some content for the body part object</H1></HTML>"
Stm.Flush
' Now set the Message object's Content-Type header
' to multipart/alternative
Set Flds = iBp.Fields
Flds("urn:schemas:mailheader:content-type") = "multipart/alternative"
Flds.Update
.AddAttachment FullPathFileName
.Send
End With
End Sub |
Partager