DotNetNuke Daily Tip #2 Mail.SendMail

Check out all of the DotNetNuke Daily Tips from Chris Hammond.

DotNetNuke Daily Tip for 6/23/2006

Have you ever needed to send mail from one of your DotNetNuke modules? Here's the DotNetNuke method for sending off a message.

public static System.String SendMail ( System.String MailFrom , System.String MailTo , System.String Cc , System.String Bcc , DotNetNuke.Services.Mail.MailPriority Priority , System.String Subject , DotNetNuke.Services.Mail.MailFormat BodyFormat , System.Text.Encoding BodyEncoding , System.String Body , System.String Attachment , System.String SMTPServer , System.String SMTPAuthentication , System.String SMTPUsername , System.String SMTPPassword )

But what does that really mean? Here's a simple call to the mail function from a module.

Mail.SendMail("FROM@ADDRESS.COM", "TO@ADDRESS.COM", "", "THIS IS THE SUBJECT", "THIS IS THE MESSAGE BODY", "", "HTML", "", "", "", "" );

Could it get any simpler than that?

Actually it could! Here's another signature for sendmail

public static System.String SendMail ( System.String MailFrom , System.String MailTo , System.String Bcc , System.String Subject , System.String Body , System.String Attachment , System.String BodyType , System.String SMTPServer , System.String SMTPAuthentication , System.String SMTPUsername , System.String SMTPPassword )

 

Do you have a question about DotNetNuke? Perhaps I can answer it here as a Daily DotNetNuke Tip! Email chris.hammond at dotnetnuke.com  with your question! Be sure to put Daily Tips in the subject line.

16 Comments

  • I hope there are some more overloads so that people don't always have to pass more empty parameters than they pass values.

  • So simple for sending mail in DNN but it is not working for me (version DNN 4.3.2) in certain respects. Do you see what is wrong with this code below ?

    The email actually goes out via the SMTP server and is delived to my email inbox but I wanted to make sure I had HTML that would do some nice line breaks using carriage returns and new lines. So the email goes out but the "carriage return and new line are not working. Everything appears on 1 line in my email inbox.

    string _EmailMsg1;
    string _EmailMsg2;

    _EmailMsg1 = "There has been Service and Help transaction activity. You can read and respond at ... ";
    _EmailMsg2 = "Portal Name";

    string _EmailFullMsg = _EmailMsg1 + "\r\n" + _EmailMsg2;
    Regex _EmailExp = new Regex(_EmailFullMsg, RegexOptions.IgnoreCase);

    // Send out to the current user
    Mail.SendMail(PortalSettings.Email.ToString(), UserInfo.Email.ToString(), "",
    "An I-Receipt of transaction activity", Server.HtmlEncode(_EmailExp.ToString()),
    "", "html", "", "", "", "");

    Thanks in advance for any C# thoughts you have on what is missing.

  • You should include for your linebreaks if you want to include HTML, not \r\n

  • Hi,

    Hers is a updated attempt to get HTML line breaks when using DNN Send Mail. I actually thought this would work but still no line breaks on the email received in my inbox but the email itself does work.

    string _EmailMsg1;
    string _EmailMsg2;
    string _break = @"
    ";

    _EmailMsg1 = "There has been Service and Help transaction activity. You can read and respond at ... ";
    _EmailMsg2 = PortalSettings.PortalAlias.HTTPAlias.ToString();

    string _EmailFullMsg = _EmailMsg1 + _break + _EmailMsg2;
    string _EmailExp = new Regex(_EmailFullMsg, RegexOptions.Multiline).ToString();

    // Send out to the current user
    Mail.SendMail(PortalSettings.Email.ToString(), UserInfo.Email.ToString(), "",
    "An I-Receipt of transaction activity", Server.HtmlEncode(_EmailExp),
    "", "html", "", "", "", "");

  • Did you not see my above comment? You have to use actual HTML in your emails if you want it to show up.

  • What is the edifference between dotnetNuke.Common.sendNotifications() and Dotnetnuke.services.Mail.Mail.SendMail()

    Please help me out

  • Here's one. How do you set arbitrary SMTP headers?

    For example, assume I am creating a custom contact form. I want the email to the contact destination to originate from a common website address. But I want to dynamically set the Reply-To header to the email address provided by the person who submits the contact request.

    Foreseeing this or other specific SMTP headers should not necessarily be in the purview of the SendMail API. For these cases, it should be possible to specify any arbitrary SMTP header. Is this currently possible? If so, how? If not, will it soon be?

  • I believe SendMail is deprecated in DNN 4. SendNotification is the replacement.

  • You might try Server.HtmlDecode(youremailmessage) in case the HTML hasn't already been decoded.

  • I believe that Vic Kumar has the two methods the other way round. DotNetNuke.Common.Globals.SendMail is obsolete and has be replaced by DotNetNuke.Services.Mail.Mail.SendMail.

  • Do you know how to setup separate smtp server settings (with authentication) for each portal - so that they all don't have to use the settings in host menu?


  • Cjepp, I don't believe that is possible

  • Prashant, can you try without utilizing the Replyto, does it work in that case?

  • Hi,

    I need to know how I can attach a pdf file as a byte array insted of a string array?

    Many Thanks

  • Hi,

    I am using SendMail to submit a form, and I want to include more than 1 attachment. Is that possible, and if Yes, how?

    Please reply urgently!

  • Does mailTo accept a semi-colon-delimited list of e-mail addresses, or a comma-delimited list, or just one address?

    (I suppose an overload that accepts MailAddress or MailAddress[] or MailAddressCollection would have made too much sense.)

Comments have been disabled for this content.