How to send email with embedded pictures

怎甘沉沦 提交于 2020-01-17 06:55:31

问题


I have a requirement to send emails with embedded photos. I'm using a html template email with a img tag inside it like this

<img src = "cid:image1">

I'm loading the template into a string then adding it to the mail, then adding the image file part

using namespace Poco::Net;

// create and initiliase a multipart html email
auto message = std::make_shared<Poco::Net::MailMessage>();
Poco::Net::MediaType mediaType("multipart", "related");
mediaType.setParameter("type", "text/html");
message->setContentType(mediaType);

// add the previously loaded html part
message->addPart("", new Poco::Net::StringPartSource(mailTextHtml, "text/html"), MailMessage::CONTENT_INLINE, MailMessage::ENCODING_QUOTED_PRINTABLE);

// get the image, tag it, and add it to the message
Poco::Net::FilePartSource *image = new Poco::Net::FilePartSource(ofToDataPath(imageFilePathName));
image->headers().add("Content-ID", "<image1>");   // assumes there is an image tag in the HTML for <image1>
message->addPart("", image, MailMessage::CONTENT_INLINE, MailMessage::ENCODING_BASE64);

// Encode the sender and set it.
message->setSender(Poco::Net::MailMessage::encodeWord(senderEMail, "UTF-8"));

This is working OK for some mail clients (outlook, gmail) but not for others (ios, thunderbird) where the image comes through as an attachment. Its a real problem as we want the mail to show up on both iPhone and Android with the picture in the screen.

Am I doing something wrong, or is there a better way?


回答1:


The code below may not be the most elegant implementation but it works for me.

//This is a VS2017 console app with /clr support enabled.
// SendEmail.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>

#using <System.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Net;
using namespace System::Net::Mail;
using namespace System::Net::Mime;
using namespace System::Threading;
using namespace System::ComponentModel;
using namespace System::Runtime::InteropServices;
using namespace System::Text;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;
using namespace System::IO;

int main()
{
    SmtpClient^ client = gcnew SmtpClient(L"smtp.cox.net", 587);
    MailMessage^ message = gcnew MailMessage();
    try
    {
        // Specify the message content.
        // Message body with embedded imeage
        String^ body(L"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
            + L"<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\"></HEAD>"
            + L"<BODY><br><h2>The message of the email goes here</h2><br>"
            + L"<DIV style='height:100%; width:460px;'><div style='height:450px; width:450px;'><img src=\"cid:ImgContId\" width='450' height='450' alt='Death' style='margin: 20px 0px 0px 20px;'/></div>"
            + L"</body></html>");
        Image^ newImage = Image::FromFile("C:\\Users\\xxxxxxxx\\Pictures\\EmbeddedImg.png");
        ImageConverter^ ic = gcnew ImageConverter();
        array<Byte>^ byteArray = { 1,2,3,4 };
        Type^ byteArrayType = byteArray->GetType();
        array<Byte>^ imgByteArray = (array<Byte>^)ic->ConvertTo(newImage, byteArrayType);
        MemoryStream^ imgMemStrm = gcnew MemoryStream(imgByteArray);
        LinkedResource^ imgResource = gcnew LinkedResource(imgMemStrm);
        imgResource->ContentId = L"ImgContId";
        AlternateView^ avSurrogate;
        AlternateView^ av = avSurrogate->CreateAlternateViewFromString(body, gcnew ContentType(L"text/html; charset=UTF-8"));
        av->LinkedResources->Add(imgResource);
        message->SubjectEncoding = System::Text::Encoding::UTF8;
        message->BodyEncoding = System::Text::Encoding::UTF8;
        message->IsBodyHtml = true;
        message->AlternateViews->Add(av);
        message->From = gcnew MailAddress(L"xxxxxxxx@gmail.com", L"First Last", System::Text::Encoding::UTF8);
        message->Sender = gcnew MailAddress(L"xxxxxxxx@gmail.com", L"First Last", System::Text::Encoding::UTF8);

        message->To->Add(gcnew MailAddress(L"xxxxxxxx@gmail.com", L"Loud Mouth", System::Text::Encoding::UTF8));
        message->Subject = L"RE: xxxxxxxxxxxxxxxxxxxxxxxxxx";

        // Send the message
        client->EnableSsl = true;
        client->Credentials = gcnew NetworkCredential(L"username", L"password");
        client->Send(message);
    }
    catch (Exception^ ex)
    {
        Console::WriteLine(L"Exception caught: {0}", ex->ToString());
    }
    finally
    {
        // Clean up.
        client->~SmtpClient();
        delete message;
        client = nullptr;
        Console::WriteLine("Goodbye.");
    }

    return 0;
}


来源:https://stackoverflow.com/questions/42931741/how-to-send-email-with-embedded-pictures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!