April 22, 2010 by Christoff Truter C#
To send a simple text based e-mail (using .net), we simply instantiate a MailMessage class
and pass the object to a SmtpClient class like this:
using System.Net.Mail; using System.Net; class Program { static void Main(string[] args) { using (MailMessage mail = new MailMessage()) { mail.To.Add("jane@doe.com"); mail.From = new MailAddress("john@doe.com", "John Doe"); mail.Subject = "Test subject"; mail.Body = "Test body"; SmtpClient SmtpMail = new SmtpClient("smtp.doe.com"); /* If authentication is needed on your smtp server SmtpMail.Credentials = new NetworkCredential("someusername", "somepassword"); */ SmtpMail.Send(mail); } } }
<img src="data:image/jpeg;base64,/4AASCDVCAGDFG//ASDFSDF....etc" />
<img src="cid:img1" />
Dear {recipient} I would like to introduce you to our exciting website, please visit CSTruter.com Yours Sincerely {author}
<html> <head> <title></title> </head> <body> Dear {recipient} <br /> <br /> I would like to introduce you to our exciting website, please visit<br /> <a href="http://www.cstruter.com">CSTruter.com</a> <br /> <br /> Yours Sincerely <br /> {author} <br /> <img src="cid:img1" alt="Author" /> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; using System.Net; using System.Net.Mime; using System.IO; class Program { static AlternateView CreateAlternateViewFromTemplate(String path, String mediaType, IDictionary<String, String> replacements) { using (StreamReader sr = new StreamReader(path)) { String content = sr.ReadToEnd(); if (replacements != null) { foreach (KeyValuePair<String, String> replacement in replacements) { content = content.Replace(String.Concat("{", replacement.Key, "}"), replacement.Value); } } AlternateView view = AlternateView.CreateAlternateViewFromString(content, Encoding.UTF8, mediaType); view.ContentType.CharSet = Encoding.UTF8.BodyName; if (mediaType == "text/html") { view.TransferEncoding = TransferEncoding.QuotedPrintable; } return view; } } static void Main(string[] args) { using (MailMessage mail = new MailMessage()) { mail.To.Add("jane@doe.com"); mail.From = new MailAddress("john@doe.com", "John Doe"); mail.Subject = "Test subject"; mail.Body = "test"; Dictionary<String, String> replacements = new Dictionary<string, string>(); replacements.Add("recipient", "Jane Doe"); replacements.Add("author", "John Doe"); AlternateView plain = CreateAlternateViewFromTemplate("template1.txt", "text/plain", replacements); AlternateView html = CreateAlternateViewFromTemplate("template1.htm", "text/html", replacements); LinkedResource img1 = new LinkedResource(@"cow.jpg", "image/jpeg"); img1.ContentId = "img1"; html.LinkedResources.Add(img1); mail.AlternateViews.Add(plain); mail.AlternateViews.Add(html); SmtpClient SmtpMail = new SmtpClient("smtp.doe.com"); //SmtpMail.Credentials = new NetworkCredential("someusername", "somepassword"); SmtpMail.Send(mail); } } }
Please elaborate January 11, 2016 by Christoff
Hi Peter, can you please elaborate on your issue? (Seeing that alternatives were given in this post)