maxbeltra
maxbeltra

Reputation: 31

POCO libs: unable to send HTML formatted email with attachment

I'm not able to send HTML formatted email with attacment: I'm able to send HTML formatted email OR email with attachment. If I send HTML email WITH attachment I get only the attachment, not the body.

This is the snippet I'm using:

...
message.setSender(emailFrom);
message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, emailToAddr,  emailToRealName));
message.setSubject(subject);

message.setContentType("text/html; charset=utf-8"); // also tried multipart/mixed
std::string content("");
content += "<html>";
content += "<head>";
content += "</head>";
content += "<body>";
content += "Test HTML";
content += "</body>";
content += "</html>";
message.setContent(content, MailMessage::ENCODING_8BIT); // also tried message.addContent(new StringPartSource(content));
message.addAttachment("myFile", new FilePartSource("/home/max/attach.txt"));

...

If I use the setContent I get ONLY the attachment, no email body. I suppose this is because the setContent make a single part mail, no multipart. If I use the addContent, bisedis the attachment, I get also the mail body, but with all HTML tags inside as plain text. But why not in HTML format? I guess I should use the sub-type, but I don't know how... Google didn't helped me this time.

Any help would be appreciated!

Thanks.

EDIT: Ok, at the end after several tries I got it. I have to use:

message.addPart("myPart", new StringPartSource(content, "text/html; charset=utf-8"), MailMessage::CONTENT_INLINE, MailMessage::ENCODING_8BIT);

instead of setContent. Ok, now I have some confusion on addPart and addContent....

If someone has some suggestions on how to handle HTML emails with attachments, using POCO libs, is welcome.

I hope that this could help someone.

Upvotes: 3

Views: 914

Answers (1)

rafaLiusz
rafaLiusz

Reputation: 33

You need to remove the line with setting the content type. I had the same issue and this fixed it.

Upvotes: 0

Related Questions