我要求发送包含嵌入照片的电子邮件。 我正在使用带有img标签的html模板电子邮件,就像这样
<img src = "cid:image1">
我正在将模板加载到字符串中,然后将其添加到邮件中,然后添加图像文件部分
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"));
这对于某些邮件客户端(outlook,gmail)工作正常,但对于其他图像(ios,thunderbird),其中图像作为附件发布。 这是一个真正的问题,因为我们希望邮件能够在iPhone和Android上显示,屏幕上显示图片。
我做错了什么,还是有更好的方法?
答案 0 :(得分:0)
下面的代码可能不是最优雅的实现,但它对我有用。
//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;
}