0

How can I send an image within an E-Mail, but not as an attachment?

The image will be combined with some generated number, that Access provides.

Thank You for registration bla, bla, bla... Your generated number, that you need to sign is (for example) 123456789. Please sign it in bla, bla, bla...

I have code that sends E-Mail with generated number within. It is only text.

Private Sub D001_Click()

'Program za registrovanje novog zaposlenog
    CurrentDb.Execute "INSERT INTO Registrovani (Ime, Prezime, Korisnicko_ime, Lozinka, Adresa, Broj, Dodatak, Postanski_broj, Grad, Drzava, Telefon, Elektronska_adresa) VALUES('" & Me.IZ & "', '" & Me.PZ & "', '" & Me.KIZ & "', '" & Me.LZ & "', '" & Me.APZ & "', " & Me.BAPZ & ", '" & Me.DBAPZ & "', " & Me.PBZ & ", '" & Me.GZ & "', '" & Me.DZ & "', '" & Me.KTZ & "', '" & Me.EAZ & "')"

'Program za slanje automatske elektronske poste sa kodnim brojem
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strbody = "Poštovani," & vbNewLine & _
    "" & vbNewLine & _
    "ovo je automatska poruka poslata od strane programa, na koju NE TREBA da odgovorite." & vbNewLine & _
    "" & vbNewLine & _
    "Poslata je u svrhu Vaše registracije, a u vezi provere Vašeg identiteta." & vbNewLine & _
    "Potrebno je da dobijeni kodni broj sa svim svojim karakterima ukucate nazad u bazu podataka, na definisano mesto." & vbNewLine & _
    "" & vbNewLine & _
    "Vaš kodni broj je:" & vbNewLine & _
    "" & vbNewLine & _
    "" & vbNewLine & _
    DLookup("ID", "Registrovani", "[Korisnicko_ime]='" & Me.KIZ.Value & "'") & vbNewLine & _
    "" & vbNewLine & _
    "" & vbNewLine & _
    "U koliko niste u procesu registracije, neko pokušava Vašu elektronsku poštu da zloupotrebi!" & vbNewLine & _
    "Hitno preduzmite sve potrebne korake radi zaštite Vaših podataka!" & vbNewLine & _
    "U toj situaciji, sadržaj ovog elektronskog pisma možete ignorisati." & vbNewLine & _
    "" & vbNewLine & _
    "Hvala na razumevanju i pažnji."

    On Error Resume Next
    With OutMail
        .To = Me.EAZ.Value
        .CC = ""
        .BCC = ""
        .Subject = "Registracija"
        .Body = strbody
        .Attachments.Add 'npr. ("C:\Users\10466237\Desktop\1111111111111111111111111111111111111.jpg")
        .Send 'ili mozes samo prikazati elektronsku poruku pomocu komande .Display ili oboje (poslati i prikazati)
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
    
    Me.IZ = Null
    Me.PZ = Null
    Me.KIZ = Null
    Me.LZ = Null
    Me.APZ = Null
    Me.BAPZ = Null
    Me.DBAPZ = Null
    Me.PBZ = Null
    Me.GZ = Null
    Me.DZ = Null
    Me.KTZ = Null
    Me.EAZ = Null
    DoCmd.Close acForm, 1
    DoCmd.OpenForm (500)
End Sub
Community
  • 1
  • 1
Mare_KST
  • 1
  • 3
  • Do you create the mail as text or HTML mail? – FunThomas Jun 05 '23 at 14:44
  • 1
    It's probably easiest to create the mails from a template instead of from scratch. Use `Set oItem = oOutlook.CreateItemFromTemplate(sPathToTemplate)` instead of `Set oItem = oOutlook.CreateItem(olMailItem)`. Then replace keyword(s) in the HTMLBody with your variables. – Andre Jun 05 '23 at 15:02
  • Try it step by step, this looks to be a nice example of what you are looking for... https://www.codevba.com/outlook/email-with-picture-in-body.htm – Mathias Z Jun 06 '23 at 09:00
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jun 06 '23 at 11:53
  • @MathiasZ: that's an attachment, not an inline image. – Andre Jun 06 '23 at 13:25
  • Yes, that is old code with attachment. I would like to improve it to have picture in body of E-Mail. Community asked me to provide code and I did it. – Mare_KST Jun 06 '23 at 14:15
  • fyi - Before you try the suggested duplicate remove `On Error Resume Next` so you can debug if necessary. In general remove `On Error Resume Next` from all code you find where it is followed by `With OutMail`. – niton Jun 06 '23 at 15:19

2 Answers2

0

You are setting plaintext Body property, use HTMLBody property instead.

Your <img> tag can either

  1. embed the picture as base64 encoded string (<img src='data:image/jpeg;base64, LzlqLzRB.../>') (older versions of Outlook won't display it)
  2. Add the image as an attachment (Attachments.Add), set PR_ATTACH_CONTENT_ID property on the attachment, and refer to that cid (src=cid:xyz) in the img tag. See https://stackoverflow.com/a/16968736/332059. This works in all email clients.
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

The following code sets a plain text message body and attaches a file (image):

With OutMail
        .To = Me.EAZ.Value
        .CC = ""
        .BCC = ""
        .Subject = "Registracija"
        .Body = strbody
        .Attachments.Add 'npr. ("C:\Users\10466237\Desktop\1111111111111111111111111111111111111.jpg")
        .Send 'ili mozes samo prikazati elektronsku poruku pomocu komande .Display ili oboje (poslati i prikazati)
    End With

If you want to get your attached image displayed in the message body you need:

  1. Use the HTMLBody property instead of Body. That change will allow displaying rich UI including images and other text formatting.
  2. In the message body string you need to add the img tag with your image in the following way:
.HTMLBody = "<html><body><img src=""cid:Image.jpg""height=520 width=750></body></html>"

where the Image.jpg is the name of the attached file.

Sometimes you need to set the content ID of your attached file explicitly so that you could use that value in the message body, for example:

attachment = attachments.Add("c:\temp\MyPicture.jpg")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "AttachmentId1")
mailItem.HTMLBody = "<html><body>Test image <img src=""cid:AttachmentId1""></body></html>"
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Hi Eugene, thanks for the explanation and time. I have problem with line: attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "AttachmentId1") Something is wrong with the part ".PropertyAccessor.", it is always red and reports an compile error: syntax error?! – Mare_KST Jun 10 '23 at 08:35
  • The [Attachment.PropertyAccessor](https://learn.microsoft.com/en-us/office/vba/api/outlook.attachment.propertyaccessor) is a valid property. Make sure that you added a reference to the Outlook object model in your project, i.e. use the early-binding. – Eugene Astafiev Jun 10 '23 at 08:38