
Sending Email via Outlook with picture attachment
July 20, 2008You have many ways how to send email from client in .NET
- You can use standard .NET API and send email via your mail server.
Disadvantages are, that each client must have configured SMTP, and you could not use your mail client interface. - You can use HTML mailto command in any component. When you activate it, it opens your mail client and you can send an email.
Disadvantages are, that clients are very various. You should never know which client does user use. Than it is very hard to pregenerate content of mail item. Especially add attachment is difficult. - You can use standard Email client interop. (In our case Outlook 2003 interop)
Disadvantages are, that user must have installed outlook 2003. It is very good solution for internal software inside of companies, that use outlook client globally. It is easy to work with attachments there.
I have been inspirate from this great article of Keith Elder. I am using same class Email from this article to show you , how to send email with picture attachment.
private void SendEmail(string attachmentFileName, string message)
{
try
{
string dir = System.IO.Directory.GetCurrentDirectory();
string path = string.Format(@"{0}\{1}", dir, attachmentFileName);
EmailProxy outlookEmail = new EmailProxy();
outlookEmail.Message.BodyFormat = OlBodyFormat.olFormatHTML;
outlookEmail.Message.Importance = OlImportance.olImportanceNormal;
outlookEmail.Message.Subject = string.Format("PT: {0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
outlookEmail.Message.Attachments.Add(path, OlAttachmentType.olByValue, 1, attachmentFileName);
outlookEmail.Message.HTMLBody = string.Format(
@"<html><table border='0'><tr><td height='30'>{0}</td></tr><tr><td></td></tr><tr><td>"
+"<img align='middle' src='{1}'/></td></tr>",message.Replace("\n","</p>"), attachmentFileName);
outlookEmail.Message.HTMLBody += @"</table></html>"
outlookEmail.Show();
}catch (Exception ex){ Trace.WriteLine(ex.Message); }
}
References