Programatically send email via outlook2016 including signature with image using vb.net

Hugh Self Taught 101 Reputation points
2022-08-02T13:48:08.463+00:00

Hi Gurus, I'd really like to get this working now as I'd shelved it for some time so any assistance will be really appreciated.
I have a working module in my winforms desktop app that sends emails via Outlook & includes the (if selected) signature but now I really need to use a signature which has an image. The current method only shows the normal message of the image not being able to be displayed. I use a modified Dick Kusleika's method of streaming the signature to text

``Function GetBoiler(ByVal sFile As String) As String  

Dim fso As Object, ts As Object = Nothing, tBoiler As String = Nothing, filesPath As String = Nothing

            fso = CreateObject("Scripting.FileSystemObject")  
            ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)  
  
            tBoiler = ts.ReadAll  
            ' fix relative references to images, etc. in Signature  
            ' by making them absolute paths, OL will find the image  
            filesPath = Replace(sFile, ".htm", "") & "_files/"  
            tBoiler = Replace(tBoiler, filesPath, sigpath() & filesPath)  
            GetBoiler = tBoiler  

I'm using late binding to avoid outlook version issues. Sigpath is the local path to the signatures folder & strSignature is the file name

            If Dir(sigpath) IsNot Nothing Then  
                Ssign = sigpath() & strSignature & ".htm"  
                Signature = GetBoiler(Ssign)  
            Else  
                Signature = Nothing  
            End If  

            oOutlook = CreateObject("Outlook.Application")  
            'End If  
  
            oNameSpace = oOutlook.GetNamespace("MAPI")  
            oFolder = oNameSpace.GetDefaultFolder(6)    'oFolderInbox=6  
  
            If Embedded = True Then  
                oMail = oOutlook.CreateItemFromTemplate(frmEmail.txtEmbedded.Text)  
            Else  
                oMail = oOutlook.CreateItem(0)  '(olMailItem) = 0  
            End If  

Then with oMail I send with the following line after setting the recipients etc

.HTMLBody = strBody.Replace(vbCrLf, "<br>") & "<br><br>" & Signature & strDisclaimer.Replace(vbCrLf, "<br>")  
                    '.HTMLBody = strBody.Replace(vbCrLf, "<br>") & "<br><br>" & Replace(Signature, "src = SADTA_files/image002.jpg", "src = " & sigpath() & "SADTA_files/image002.jpg") & strDisclaimer.Replace(vbCrLf, "<br>")  

I have read so many responses to adding images to an email & adding signatures but I haven't found a solution that works for me. The most common I've read is to display the email first then add the signature but I mostly need to send immediately since I could be sending the same email to several groups of recipients. Another thing was to use getInspector but I also read that after OL2013 this was no longer an option so I didn't pursue it. If anyone can help with something that will get this image displaying, I'll be eternally grateful. An alternative would be to have the text portion of the signature placed alongside the image as 2 separate actions?

Developer technologies Visual Basic for Applications
Developer technologies VB
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-08-04T17:06:28.873+00:00

    Although the following code is C# found here focus on how I setup the image. Of course you need to adapt to this as your method is different from mine but this <img src=cid:{imageIdentifier}> is key.


  2. Hugh Self Taught 101 Reputation points
    2022-08-11T04:49:59.83+00:00

    This works:
    I opened the .htm file with notepad & searched for "src=". I found 2 references. the first was imagedata src="SADTA_files/image001.jpg". The second was src="SADTA_files/image002.jpg".

    I then attached the signature image files hidden to the email thus:

    Dim SimgPath As String = sigpath() & strSignature & "_Files"  
                            oAttach = .Attachments.Add(SimgPath & "/image001.jpg")  
                            oAttach.PropertyAccessor.SetProperty(PR_ATTACH_MIME_TAG, "image/jpg")  
                            oAttach.PropertyAccessor.SetProperty(PR_ATTACH_CONTENT_ID, "SigImg1")  
                            oAttach.PropertyAccessor.SetProperty(PR_ATTACHMENT_HIDDEN, True)      'Hide this attachment  
    

    sigpath being the folder where the signatures are located. oAttach earlier defined as Object since this is Late Binding & note the "/" not "\"
    I changed my ".HTMLbody =" to .HTMLBody = strBody.Replace(vbCrLf, "<br>") & "<br><br>" & "<p></p>" & Signature & "<br><br>" & strDisclaimer.Replace(vbCrLf, "<br>")
    The reference to the 2nd "src=" didn't need changing as it made no difference. The signature came out correct with its image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.