CS2: How to send mail from your CSModules
I sat and read Chris Hammond's blog today, he had a tip on sending mail from DotNetNuke, and I thought: "How do I send mail from a CSModule?". I remember downloading ScottW's CSModule Pack, and that it had a function to send mail when new users were registered etc. Looking through his code I found this (I've translated it from C# to VB):
Dim mail As MailMessage = New MailMessage
mail.To = _adminEmail
mail.Subject = subject
mail.Priority = priority
ail.From = _adminEmail
mail.BodyFormat = MailFormat.Text
mail.Body = body
EmailQueueProvider.Instance().QueueEmail(mail)
Just create a new System.Web.Mail.MailMessage object (if you do it in VS 2005 it will tell you that it's an obsolete class, but it works, and you need to use that since CS expects that class to be sent to the QueueEmail method), fill in the MailMessage's properties, and send it to CS's Email queue.
The _adminEmail, where to get the value for that? Well, you could use the CSContext.Current.SiteSettings.AdminEmailAddress property.
Of course one could always send mail directly via .NET Framework from the CSModule. But implementing CS built-in functions makes it follow the settings you do in the CS Interface and in the config files. Like changing the admin email, re-configuring the email job etc.