使用Outlook Interop在电子邮件中嵌入图像
问题描述:
我有一个简单的小电子邮件应用程序,它允许用户选择某些选项来生成字符串并发送电子邮件。我想看看是否有可能将图像添加到电子邮件中,即标题徽标或签名等。我一直在研究的研究非常繁重,而且我对HTML知之甚少。谁能帮忙?我的代码如下...使用Outlook Interop在电子邮件中嵌入图像
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Configuration;
namespace My_EmailSender
{
public class EmailSender:Notification
{
string emailRecipient = ConfigurationManager.AppSettings["emailRecipient"];
public void SendMail(string message)
{
try
{
var oApp = new Outlook.Application();
var oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
var oRecip = (Outlook.Recipient)oMsg.Recipients.Add(emailRecipient);
oRecip.Resolve();
oMsg.Subject = "Email Notification";
oMsg.Body = message;
// Display the message before sending could save() also but no need
oMsg.Send();
oMsg.Display(true);
oRecip = null;
oMsg = null;
oApp = null;
}
catch (Exception e)
{
Console.WriteLine("Problem with email execution. Exception caught: ", e);
}
return;
}
}
}
答
下面是在c#
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
System.Net.Configuration.SmtpSection smtp = settings.Smtp;
System.Net.Configuration.SmtpNetworkElement network = smtp.Network;
Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem mailItem = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.To = network.TargetName;
Attachment attachment = mailItem.Attachments.Add(
"C://test.bmp"
, OlAttachmentType.olEmbeddeditem
, null
, "test image"
);
string imageCid = "[email protected]";
attachment.PropertyAccessor.SetProperty(
"http://schemas.microsoft.com/mapi/proptag/0x3712001E"
, imageCid
);
mailItem.BodyFormat = OlBodyFormat.olFormatRichText;
mailItem.HTMLBody = String.Format(
"<body><img src=\"cid:{0}\"></body>"
, imageCid
);
mailItem.Importance = OlImportance.olImportanceNormal;
mailItem.Display(false);
很容易使用的net.mail发送图像直通前景样本代码,但是,如果您想发送大量电子邮件,我会推荐一个程序SendBlaster。 –