C#: Embedding images in an email
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); } } }
To send a HTML based email is equally as simple, simply set the IsBodyHtml property true on your MailMessage instance and pass some HTML to your body property.
But what about things like embedding images in an email (title of this post ;))?
One method I saw involves adding base64 encoded strings to image tags within the html e.g.
<img src="data:image/jpeg;base64,/4AASCDVCAGDFG//ASDFSDF....etc" />
Unfortunately a lot of email clients dislike this method (not to mention spam filters).
Another more common method is to add multipart entities to an e-mail and referencing them from cid's e.g.
<img src="cid:img1" />
We achieve that using the LinkedResource and AlternateView classes in .net.
What we're going to do, is create two files, template1.txt and template1.html.
template1.txt (contains the text part of the email)
Dear {recipient}
I would like to introduce you to our exciting website, please visit
CSTruter.com
Yours Sincerely
{author}
template1.htm (contains the html part of the email)
<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>
You might have noticed {recipient} & {author}, these are placeholders that we're going to populated via code.
In the following snippet we process the templates and send the email to our recipient:
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); } } }
Note, we add a LinkedResource (the embedded resource, assign a ContentID to it - the cid needed for referencing the image in the HTML) to an AlternateView, and an AlternateView to our MailMessage instance.
It might also be interesting to have a look at the MailDefinition class, available within System.Web.UI.WebControls namespace - perhaps a future post ;)
Posted by - Christoff Truter
Date - 2010-04-22 18:26:49
Comments - 0
Date - 2010-04-22 18:26:49
Comments - 0
Javascript: Limit Textarea
Source Code
Limiting the amount of characters that a html textbox can take is quite straightforward,
you simply add the maxlength attribute and thats it.
Unfortunately for some strange reason a html textarea doesn't have this attribute, hence
the reason for this post.
We're going to attempt to add/process this attribute, like the following snippet:
<table> <tr> <td> Title </td> <td> <input name="txtTitle" type="text" maxlength="32"> </td> </tr> <tr> <td> Description </td> <td> <textarea name="txtDescription" maxlength="128" rows="4" cols="40"></textarea> </td> </tr> <tr> <td> Body </td> <td> <textarea name="txtBody" maxlength="1024" rows="20" cols="80"></textarea> </td> </tr> </table>
You simply need to include the following script on your page, which loops through all the textareas on a page, searching for the maxlength attribute.
function limiter() { var textareas = document.getElementsByTagName('textarea'); for(var i = 0; i < textareas.length; i++) { var textarea = textareas.item(i); limit(textarea); } } function limit(sender) { var maxlength = sender.getAttribute('maxlength'); if (maxlength != null) { // limit input values sender.onkeypress = function(e) { // Crossbrowser Issue if (e == null) e = window.event; // exclude certain keys from our limiter if ((e.keyCode == 8) || (e.keyCode > 36) && (e.keyCode < 41)) return true; return (sender.value.length < maxlength); } // limit pasted values sender.onpaste = function() { // onAfterPaste setTimeout(function() { if(sender.value.length > maxlength) { sender.value = sender.value.substring(0, maxlength); } }, 1); } } } // Only Attach events needed for limiting the textareas once the page finished loading if (window.addEventListener) { // FF etc window.addEventListener('load', limiter, false); } else{ // IE window.attachEvent('onload', limiter); }
Posted by - Christoff Truter
Date - 2010-04-14 22:01:43
Comments - 1
Date - 2010-04-14 22:01:43
Comments - 1
First 6 7 8 9 10 11 12 13 14 15 Last / 42 Pages (83 Entries)