我想使用C#将图片从picturebox添加到我的电子邮件正文中。这就是我所做的。有人可以帮帮我吗?
var fromAddress = new MailAddress("aaa@gmail.com", "aaa");
var toAddress = new MailAddress("bbb@gmail.com", "bbb");
const string fromPassword = "mypassword";
const string subject = "Tes Program";
const string body = "Bersama ini kami kirimkan QR Code sebagai sarana validasi pengiriman rekening koran Anda. Harap simpan dan tunjukkan QR Code ini saat kurir kami datang untuk mengantar rekening koran. Atas perhatiannya kami sampaikan terima kasih. Salam";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
}
)
{
smtp.Send(message);
}
我有picturebox1并希望将picturebox1中显示的图像添加到我的电子邮件正文
答案 0 :(得分:1)
您需要将其添加为电子邮件的嵌入式资源并为其创建HTML视图,这是一个让您入门的示例:
private static void AddImageToEmail(MailMessage mail, Image image)
{
var imageStream = GetImageStream(image);
var imageResource = new LinkedResource(imageStream, "image/png") { ContentId = "added-image-id" };
var alternateView = AlternateView.CreateAlternateViewFromString(mail.Body, mail.BodyEncoding, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(imageResource);
mail.AlternateViews.Add(alternateView);
}
private static Stream GetImageStream(Image image)
{
// Conver the image to a memory stream and return.
var imageConverter = new ImageConverter();
var imgaBytes = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
var memoryStream = new MemoryStream(imgaBytes);
return memoryStream;
}
答案 1 :(得分:0)
您需要调整代码以包含内嵌图像。
请参阅以下已经回答过的问题:
代码片段(您需要添加类似的内容以实现相同的结果),取自Link 1;
protected void Page_Load(object sender, EventArgs e)
{
string Themessage = @"<html>
<body>
<table width=""100%"">
<tr>
<td style=""font-style:arial; color:maroon; font-weight:bold"">
Hi! <br>
<img src=cid:myImageID>
</td>
</tr>
</table>
</body>
</html>";
sendHtmlEmail("from@gmail.com", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);
}
protected void sendHtmlEmail(string from_Email, string to_Email, string body, string from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
{
//create an instance of new mail message
MailMessage mail = new MailMessage();
//set the HTML format to true
mail.IsBodyHtml = true;
//create Alrternative HTML view
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
//Add Image
LinkedResource theEmailImage = new LinkedResource("E:\\IMG_3332.jpg");
theEmailImage.ContentId = "myImageID";
//Add the Image to the Alternate view
htmlView.LinkedResources.Add(theEmailImage);
//Add view to the Email Message
mail.AlternateViews.Add(htmlView);
//set the "from email" address and specify a friendly 'from' name
mail.From = new MailAddress(from_Email, from_Name);
//set the "to" email address
mail.To.Add(to_Email);
//set the Email subject
mail.Subject = Subject;
//set the SMTP info
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail@gmail.com", "fromEmail password");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
//send the email
smtp.Send(mail);
}