Created
June 1, 2012 13:47
-
-
Save bobbychopra/2852272 to your computer and use it in GitHub Desktop.
Utility to send email, also includes embedded image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("[email protected]", 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