Skip to content

Instantly share code, notes, and snippets.

@bobbychopra
Created June 1, 2012 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbychopra/2852272 to your computer and use it in GitHub Desktop.
Save bobbychopra/2852272 to your computer and use it in GitHub Desktop.
Utility to send email, also includes embedded image
using System.Net.Mail;
using System.Net.Mime;
namespace BinaryElephant
{
public static class EmailUtility
{
public static void Send(string to, string subject, string body)
{
SendReport("daemon@bobbychopra.com", to, subject, body, false);
}
public static void Send(string from, string to, string subject, string body, bool isBodyHtml)
{
var msg = new MailMessage(from, to) {Subject = subject, Body = body, IsBodyHtml = isBodyHtml};
Send(msg);
}
public static void SendHtmlWithLogo(string from, string to, string subject, string body)
{
var msg = new MailMessage(from, to);
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
//To refer to this image in html, use <img src="cid:logo"/>
string strImageUrl = System.Web.HttpContext.Current.Server.MapPath("~/Images/logo.gif");
var logo = new LinkedResource(strImageUrl, MediaTypeNames.Image.Gif);
logo.ContentId = "logo";
logo.TransferEncoding = TransferEncoding.Base64;
AlternateView av = AlternateView.CreateAlternateViewFromString(msg.Body, null, MediaTypeNames.Text.Html);
av.LinkedResources.Add(logo);
msg.AlternateViews.Add(av);
Send(msg);
}
private static void Send(MailMessage msg)
{
using (var client = new SmtpClient("server.bobbychopra.com"))
client.Send(msg);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment