Listing 5 – C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
namespace Soyuj.HtmlMail
{
class HtmlMail
{
private static string _mailServer = String.Empty;
/// <summary>
/// The starting function of console application.
/// </summary>
/// <param name="args">Parameters in CommandLine arguments</param>
static void Main(string[] args)
{
Console.WriteLine("Welcome to Soyuj\'s Mail send application..");
Console.WriteLine("Enter your Mail Server :: ");
_mailServer = Console.ReadLine();
EmbeddedImagesLinked();
Console.WriteLine( "\r\n Completed...");
Console.ReadLine();
}
/// <summary>
/// Sends mail using SMTP client, it can use the IP of Server also
/// </summary>
/// <param name="mail">The SMTP server (MailServer) as String</param>
private static void SendMail(System.Net.Mail.MailMessage mail)
{
// send the message using SMTP client
SmtpClient smtp = new SmtpClient(_mailServer);
// _mailServer is the MailServer IP or Name
// mail Server IP or NAME
smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
smtp.Send(mail);
} // End SendMail
/// <summary>
/// Embeds image in an HTML body of MailMessage using LinkedResorce Class
/// </summary>
public static void EmbeddedImagesLinked()
{
// create the mail message
MailMessage mail = new MailMessage();
// set the addresses
mail.From = new MailAddress("from@fromdomain.com", " Display Name");
mail.To.Add("to@todomain.com");
mail.To.Add("yourHotmailID@hotmail.com");
mail.CC.Add("yourGmailID@gmail.com");
mail.CC.Add("yourYahooID@yahoo.com");
// set the content
mail.Subject = "This is an embedded image mail";
string palinBody =
"Plain text content, viewable by clients that don\'t support html";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(
palinBody, null, "text/plain");
// then we create the Html part to embed images,
// we need to use the prefix 'cid' in the img src value
string htmlBody = "<b>The embedded image file.</b><DIV> </DIV>";
htmlBody +=
"<img alt=\"\" hspace=0 src=\"cid:uniqueId\" align=baseline border=0 >";
htmlBody += "<DIV> </DIV><b>This is the end of Mail...</b>";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
htmlBody, null, "text/html");
// create image resource from image path using LinkedResource class..
LinkedResource imageResource = new LinkedResource(
"c:\\attachment\\image1.jpg" , "image/jpeg");
imageResource.ContentId = "uniqueId";
imageResource.TransferEncoding = TransferEncoding.Base64;
// adding the imaged linked to htmlView...
htmlView.LinkedResources.Add(imageResource);
// add the views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
// send mail
SendMail(mail);
} // End EmbeddedImagesLinked
} // End Class
} // End Namespace
Listing 6 – VB
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Mime
Module SampleModule
Private _mailServer As String = String.Empty
''' <summary>
''' The starting function of console application.
''' </summary>
''' <remarks>Need to set the mail server details from console</remarks>
Sub Main()
Console.WriteLine("Welcome to Soyuj's Mail send application..")
Console.WriteLine("Enter your Mail Server :: ")
_mailServer = Console.ReadLine()
EmbeddedImagesLinked()
Console.WriteLine(ControlChars.CrLf & "Completed...")
Console.ReadLine()
End Sub
''' <summary>
''' Sends mail using SMTP client
''' </summary>
''' <param name="mail">The SMTP server (MailServer) as String</param>
''' <remarks>It can use the IP of Server also</remarks>
Private Sub SendMail(ByVal mail As Mail.MailMessage)
'send the message using SMTP client
Dim smtp As New SmtpClient(_mailServer) 'mail Server IP or NAME
smtp.Credentials = CredentialCache.DefaultNetworkCredentials
smtp.Send(Mail)
End Sub ' End SendMail
''' <summary>
''' Embedes an image in a Html body of MailMessage using LinkedResorce Class
''' </summary>
''' <remarks>Image tag must be in html body with 'cid' as src value</remarks>
Public Sub EmbeddedImagesLinked()
'create the mail message
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress("from@fromdomain.com", " Display Name")
mail.To.Add("to@todomain.com")
'set the content
mail.Subject = "This is an embedded image mail"
'first we create the Plain Text part
Dim palinBody As String =
"This is my plain text content, viewable by those clients that don't support html"
Dim plainView As AlternateView =
AlternateView.CreateAlternateViewFromString(palinBody, Nothing, "text/plain")
'then we create the Html part
'to embed images, we need to use the prefix 'cid' in the img src value
Dim htmlBody As String =
"<b>This is the embedded image file.</b><DIV> </DIV>"
htmlBody +=
"<img alt="""" hspace=0 src=""cid:uniqueId"" align=baseline border=0 >"
htmlBody += "<DIV> </DIV><b>This is the end of Mail...</b>"
Dim htmlView As AlternateView =
AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, "text/html")
'create the image resource from image path using LinkedResource class..
Dim imageResource As New LinkedResource(
"c:\attachment\image1.jpg", "image/jpeg")
imageResource.ContentId = "uniqueId"
imageResource.TransferEncoding = TransferEncoding.Base64
'adding the imaged linked to htmlView...
htmlView.LinkedResources.Add(imageResource)
'add the views
mail.AlternateViews.Add(plainView)
mail.AlternateViews.Add(htmlView)
'mail.AlternateViews.Add(imageView)
'send mail
SendMail(mail)
End Sub ' End EmbedImages
End Module ' End SampleModule
NOTE: Before using the above code,
be sure to set your email addresses in the required place with Mail Server
(SMTP).