Reputation: 567
I have a simple little email app that allows a user to pick certain options that generate a string and sends out an email. I wanted to see if its possible to add images to the email i.e. a header logo or signature, etc. The research I've been looking at is very HTML heavy and I know very little HTML. Can anyone help? My code is as follows...
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;
}
}
}
Upvotes: 2
Views: 4828
Reputation: 41
Here is the sample code for sending the image thru outlook in 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 = "test.bmp@123";
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);
Upvotes: 2
Reputation: 4756
I would always use System.Net.Mail
to send emails but maybe this is a requirement of yours?
Read up on system.net.mail here.
Upvotes: 0