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.


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.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";

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.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;

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

Total: 12 Comment(s)
Well, I never had the need to send advanced emails but I guess I will have to know. I saw something about it onhttps://cheapessayservice.net/our-services/movie-review/ but since I did not know anything about it, I was worried. This really helps, thanks!
Saturday, November 3, 2018 · reply ·
Sending emails in dotnetnuke is very easy with a single hit method Mail.SendMail(…) living in the DotNetNuke.Services.Mail namespace. In case you want more options such as images in the mail, Text parts, dealing HTML and attachments you should have the direct Mail stuff from.NET Framework. I try to know about a site which is https://www.toptenwritingservices.com/7dollaressay-com-review/ and if anyone have suggestion or want to share experience then please do it.
Monday, November 5, 2018 · reply ·
I am glad to read it. https://www.sdfsd.com
Wednesday, December 19, 2018 · reply ·
I am glad to read it. https://www.sdfsd.com
Sunday, January 20, 2019 · reply ·
Amazing blog with lots of information, thanks a lot for sharing this.
Wednesday, April 24, 2019 · reply ·
【丰胸产品】刘丽丽: https://1807614030.wixsite.com/fngzsite 那个时候我刚好失恋 ,很显然男人大多喜欢胸部大的 https://1807614030.wixsite.com/fngzgw 女生 ,那次失恋给我的打击很大 。我下定决心一定要丰胸 , https://1807614030.wixsite.com/fngzsite 也是很偶然找到粉嫩公主酒酿蛋 ,但粉嫩公主酒酿蛋没有让我失望 http://fngzmy.com
Thursday, July 4, 2019 · reply ·
how to give robux to friends 2019
Ask your friend what items he has for sale so you can buy one and donate Robux to him.
Friday, August 2, 2019 · reply ·
Ask your friend what items he has for sale so you can buy one and donate Robux to him. Record the names of the objects; Finding the name of an item is the fastest way to find it. https://robuxguide.club/how-to-give-robux-to-friends/
Friday, August 2, 2019 · reply ·
Lucille F. Parham
I just can’t stop reading this. Its so fresh, so filled with updates that I just didn’t know. https://summonerswar.io/
Saturday, January 11, 2020 · reply ·
https://styleup365.net/watchcartoononline/">akashic records of bastard magic instructor season 2
Tuesday, January 14, 2020 · reply ·


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

Torsten Weggen 3/17/2019

You can make a real difference in Abir ’s life. Join me on @Kiva https://t.co/NlCTgIAZAN

Torsten Weggen 2/2/2019

As a freelance developer I often get invites from headhunters. This one should be very interesting... https://t.co/CNrEXBTBuJ