X
dnn.blog.
Back

Sending advanced emails

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

Back

about.me.

Torsten WeggenMy name is Torsten Weggen and I am CEO of indisoftware GmbH in Hanover, Germany. I'm into DNN since 2008. Before this, I did a lot of desktop stuff mainly coded with Visual Foxpro (see http://www.auktionsbuddy.de). 

I'm programmer, husband, father + born in 1965.

Please feel free to contact me if you have questions.

Latest Posts

DNN module development with Angular 2+ (Part 7)
6/10/2018 1:43 PM | Torsten Weggen
DNN module development with AngularJS (Part 6)
12/16/2016 7:00 AM | Torsten Weggen
DNN module development with AngularJS (Part 5)
12/16/2016 6:00 AM | Torsten Weggen
DNN module development with AngularJS (Part 4)
12/16/2016 5:00 AM | Torsten Weggen
DNN module development with AngularJS (Part 3)
12/16/2016 4:00 AM | Torsten Weggen
DNN module development with AngularJS (Part 2)
12/16/2016 3:00 AM | Torsten Weggen
DNN module development with AngularJS (Part 1)
12/15/2016 7:19 AM | Torsten Weggen
Blogging in DNN with Markdown Monster by Rick Strahl
11/27/2016 1:14 PM | Torsten Weggen
Creating a global token engine
11/18/2016 10:25 AM | Torsten Weggen
DnnImagehandler - Hot or not ?
2/21/2015 11:52 PM | Torsten Weggen

My Twitter

Keine News gefunden!