3

I'm trying to send an email in a DNN module I'm making. However, though it doesn't crash the email isn't being sent. I think it has to do with the From Email I'm attempting to use. I'm not 100% sure what email I should be using for the from which is the first parameter.

Protected Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
    DotNetNuke.Services.Mail.Mail.SendEmail("support@localhost", "myemail@site.com", "EmailTest", "Hello world!")
End Sub
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Mitchell
  • 253
  • 1
  • 5
  • 16

3 Answers3

4

The More likely problem is you don't have your SMTP settings properly configured. To configure your SMTP settings, Login as Host. Then, go to Host -> Settings and fill out the fields under "SMTP Server Settings" and save them. There's a test link there as well to verify they are working correctly.

EfficionDave
  • 2,736
  • 3
  • 31
  • 38
  • Looks like that's the problem. However now I'm not sure what I set the email address to. It's just defaulted to that "support@localhost" address which seems to be invalid. – Mitchell Apr 18 '12 at 12:40
  • Well, I used Neptune and configured the smtp successfully. However, sending an email through my module still doesn't cause an email to show up at my email address. Neptune shows that the message was at least sent, but does it not actually send an email? I'm referencing this guide: http://www.dotnetnuke.com/Resources/Video-Library/Viewer/VideoId/214/Configuring-SMTP-In-DotNetNuke.aspx – Mitchell Apr 18 '12 at 13:12
  • Okay got it sort of. Checking emails on Windows Live Mail. Found the ones I had sent, but sending new ones don't show up. – Mitchell Apr 18 '12 at 13:42
  • if your module is going to be install on multiple sites, the from address should probably be the email address of the site admin. You can get that by using: PortalSettings.Email. As for the "To" address, that kind of depends on what you're trying to do but you could use the same address there as well. – EfficionDave Apr 18 '12 at 19:23
  • I have DNN on server A and set my SMTP settings in the host and all works great, test worked etc... Moved the database and files to new "Server B" same settings as "Server A" and DNN test and mail fails to connect to smtp server... Any ideas? I turned firewall off new server, thought it might be that... – Tim Maxey Jul 14 '12 at 03:00
0

This is probably pretty late to the party, but I often use the Mail.SendMail() method, and then manually pass all the STMP information like below, and then when debugging I check the message that comes back. (As of DotNetNuke 5.5)

        Dictionary<string, string> hostSettings = HostController.Instance.GetSettingsDictionary();
        string server = hostSettings["SMTPServer"];
        string authentication = hostSettings["SMTPAuthentication"];
        string password = hostSettings["SMTPPassword"];
        string username = hostSettings["SMTPUsername"];

        // using the Mail.SendMail() method allows for easier debugging.
        var message = Mail.SendMail(from, user.Email, String.Empty, subject, body, String.Empty, "HTML", server, authentication, username, password);
0

Late to the game as well, but I just ran into a similar issue earlier today...

The DNN sendMail or sendEmail method handle the exceptions on their own, and add it to their DNN logs. Unfortunately, they never return said exceptions to the main code where you are calling the functions - hence why your code executes just fine!

You can look further into their exceptions table, or Admin Logs in the UI, for more info on the particular issue you are having.

I changed my code to use System.Net to send emails and collect all of the info you need from DotNetNuke.Entities.Host.Host object in DNN. This way, we can handle the error and have our code work around it :) I ended up with something like this (it's in c# but you can do the same in VB.Net with a slightly different syntax):

//make the email
MailMessage mail = new MailMessage("From@me.com","to@a.com,to@b.com,to@c.com");
mail.Subject = "test subject";
mail.Body = "actual email";

string dnnServerInfo = DotNetNuke.Entities.Host.Host.SMTPServer;
// The above looks like "server.com:port#", or "smtp.server.com:25"
//so we find the colon to get the server name, and port using the index below
int index = dnnServerInfo.IndexOf(':');

//make the SMPT Client
SmtpClient smtp = new SmtpClient();
smtp.Host = dnnServerInfo.Substring(0, index);
smtp.Port = Int32.Parse(dnnServerInfo.Substring(index + 1, dnnServerInfo.Length - index - 1));
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(DotNetNuke.Entities.Host.Host.SMTPUsername, DotNetNuke.Entities.Host.Host.SMTPPassword);
smtp.EnableSsl = DotNetNuke.Entities.Host.Host.EnableSMTPSSL;

//send the email
smtp.Send(mail);

I used part of the original code from "SendMail" found here to come up with this: https://stackoverflow.com/a/19515503/6659531

Good luck to anybody who comes across this :)

Henrique Donati
  • 317
  • 2
  • 7