Sending emails in dotnetnuke is very simple with the one hit method Mail.SendMail(…) living in the DotNetNuke.Services.Mail namespace. But if you need more options like integrating images in the mail or handling HTML-, Text parts and attachments you need the direct Mail stuff from the .NET Framework.
Settings
First thing we need is reading the Host settings, because we want to use the SMTP server which is configured in dnn:
string smtpServer = (string)DotNetNuke.Common.Globals.HostSettings["SMTPServer"];
string smtpAuthentication = (string)DotNetNuke.Common.Globals.HostSettings["SMTPAuthentication"];
string smtpUsername = (string)DotNetNuke.Common.Globals.HostSettings["SMTPUsername"];
string smtpPassword = (string)DotNetNuke.Common.Globals.HostSettings["SMTPPassword"];</pre>
Our goal is now sending an email with a text part, a HTML-Part which has the sitelogo integrated and an attachment. Don’t forget to add System.Net.Mail to your namespace usings!
Lets create the mail object and add some address information:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("\"My Name\" <me@myaddress.com>");
mail.To.Add("you@youraddress.com");
mail.Subject = "Hello this mail is from dotnetnuke";
Now let us create our first part, the plain text part of our mail. (If you want a special encoding, change the null to a System.Text.Encoding type)
AlternateView av1 = AlternateView.CreateAlternateViewFromString("This is the plain text part",null,"text/plain");
Next is adding our HTML part as a second AlternateView with our logo inside. In your HTML, you use “cid:” as the source of the image. Then you add a linked resource and set the ContentId to whatever you set in your HTML image source.:
string htmltext = "Hello folks! <br> This is my new logo: <img src="\"cid:Logo\">";
AlternateView av2 = AlternateView.CreateAlternateViewFromString(htmltext, null, "text/html");
string logoFile = MapPath(PortalSettings.HomeDirectory + PortalSettings.LogoFile);
if (File.Exists(logoFile))
{
LinkedResource linkedResource = new LinkedResource(logoFile);
linkedResource.ContentId = "Logo";
linkedResource.ContentType.Name = logoFile;
linkedResource.ContentType.MediaType = "image/jpeg";
av2.LinkedResources.Add(linkedResource);
}
Now lets us add the AlternateViews to our mail and attach a file to it. Please be aware that the ASP.NET process (or the ASP.NET impersonated account) will need permission to read the file!
mail.AlternateViews.Add(av1);
mail.AlternateViews.Add(av2);
mail.Attachments.Add(new Attachment("c:\\temp\\example.txt"));</pre>
OK, Our mail is ready, now we can send it!
SmtpClient emailClient = new SmtpClient(smtpServer);
if (smtpAuthentication == "1")
{
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(smtpUsername, smtpPassword);
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = SMTPUserInfo;
}
emailClient.Send(mail);
As you see, this is no big deal and hopefully you start now sending out marvelous designed emails with attachments and integrated images that are not suppressed by the customers email client. A good resource if you want to know more about this is the website http://www.systemnetmail.com