bluetiger9 / SmtpClient-for-Qt

An SMTP Client writen in C++ for Qt. Allows applications to send emails (MIME with text, html, attachments, inline files, etc.) via SMTP. Supports SSL and SMTP authentication.
https://github.com/bluetiger9/SmtpClient-for-Qt/wiki
GNU Lesser General Public License v2.1
452 stars 227 forks source link

I can't embed images in a HTML mail. #80

Closed ChrisBakerIKinema closed 2 years ago

ChrisBakerIKinema commented 5 years ago

I have tried the following example and I can't get it to work..

https://github.com/bluetiger9/SmtpClient-for-Qt/wiki/Examples#example3

I have a HTML email with 2 images in it. If I email to gmail the images do appear as attachments, if I email to my own outlook I don't even get that.

Here is my code...

SmtpClient smtp("smtp.office365.com", 587, SmtpClient::TlsConnection);

smtp.setUser(m_sender_email);
smtp.setPassword(m_sender_email_password);
smtp.setAuthMethod(SmtpClient::AuthLogin);

MimeMessage message;

message.setSender(new EmailAddress(m_sender_email, "Your Name"));
message.addRecipient(new EmailAddress(dlg.lineEditClientMail->text(), "Recipient's Name"));
message.setSubject(dlg.lineEditSubject->text());

MimeHtml html;
html.setHtml(dlg.textEditMessage->toHtml());

QFile att(m_attach_file);
MimeAttachment ma(&att);

QFile image1("image001.png");
QFile image2("image002.png");
MimeInlineFile mai1(&image1);
MimeInlineFile mai2(&image2);

mai1.setContentId("image001");
mai1.setContentType("image/png");

mai2.setContentId("image002");
mai2.setContentType("image/png");

//  message.addPart(&text);
message.addPart(&html);
message.addPart(&mai1);
message.addPart(&mai2);
message.addPart(&ma);

smtp.connectToHost();
smtp.login();
smtp.sendMail(message);
smtp.quit();
bluetiger9 commented 5 years ago

Hi @ChrisBakerIKinema,

Can you show me the HTML returned by:

dlg.textEditMessage->toHtml()

?

Does it look similar to the HTML from the example:

    html.setHtml("<h1> Hello! </h1>"
                 "<h2> This is the first image </h2>"
                 "<img src='cid:image1' />"
                 "<h2> This is the second image </h2>"
                 "<img src='cid:image2' />");

?

Thanks, Attila

ChrisBakerIKinema commented 5 years ago

I have to be careful here not to give away anything confidential.

It looks like HTML.

I tried pasting it but it didn't come out,

It did look complicated, it was 60k worth of HTML.

bluetiger9 commented 5 years ago

@ChrisBakerIKinema, ok. I understand this.

I would recommend to start with a simple HTML, with just a single <img> tag. Make sure the src='cid:image001' argument matches the content id set on the MimeInlineFile:

mai1.setContentId("image001");

If this works, you can build the rest of the HTML around it.

Btw, Gmail has an "Show original" option to view the raw email. This usually helps to find out what's wrong with the email.

ChrisBakerIKinema commented 5 years ago

Thanks for the help, I'll give that a go. My HTML is poor but I know Word can save in HTML format.

ChrisBakerIKinema commented 5 years ago

Also notice the line...

smtp.setAuthMethod(SmtpClient::AuthLogin);

I does not appear in your examples but on my computer it didn't work without it.

ChrisBakerIKinema commented 5 years ago

I tried a simple example and it did embed correctly thanks.