Developer forum

Forum » Development » Email without using FormMailer aspx

Email without using FormMailer aspx

Marco Santos
Reply

Hello.

 

Can you confirm that the only alternative to sending email from a DW Module is using the .net classes with something like :

 

            SmtpClient smtp = new SmtpClient();
            smtp.Host = Dynamicweb.Base.DefaultSmtpServer;
            smtp.Send(myMailMessage);

 

I am trying to send an email but the number of tags is very large and and don´t want to send it all in the request parameters. Is this the only alternative or is there another way of sending emails from a module?

 

Thanks.

 

Marco


Replies

 
Morten Bengtson
Reply
This post has been marked as an answer

You can use Dynamicweb.EmailHandler...

var mail = new System.Net.Mail.MailMessage();
// TODO: Set message content, recipients etc.
Dynamicweb.EmailHandler.Send(mail);

It's basically the same as your example, but it can handle multiple SMTP servers (failover), sends mails to the pickup directory (if activated in management center) and logs errors.

Votes for this answer: 1
 
Nicolai Høeg Pedersen
Reply

And a more full example

 

class EmailHandlerSample
	{
		public void SendMail()
		{
			bool sendSucceded;

			using (var m = new System.Net.Mail.MailMessage())
			{
				m.Subject = "This is a test mail";
				m.From = new System.Net.Mail.MailAddress("noreply@dynamicweb-cms.com", "John Doe");
				m.To.Add("someone@gmail.com");
				m.IsBodyHtml = true;
				m.Body = "<h1>Hi John</h1>Here is your message.";
				m.BodyEncoding = System.Text.Encoding.UTF8;
				m.SubjectEncoding = System.Text.Encoding.UTF8;
				m.HeadersEncoding = System.Text.Encoding.UTF8;

				sendSucceded = EmailHandler.Send(m);
			}

			if (sendSucceded)
			{
				//Log to /Files/System/Log/MyMails
				Dynamicweb.LogToFile.Log("Mail succesfully sent", "/MyMails", LogToFile.LogType.ManyEntriesPerFile);
			}
			else
			{
				//Log to /Files/System/Log/MyMails
				Dynamicweb.LogToFile.Log("ERROR: Mail not sent", "/MyMails/Error", LogToFile.LogType.ManyEntriesPerFile);
			}

		}
	}

 

 
Marco Santos
Reply

Thanks guys!

 
George Nelzo Pereira
Reply

How can I get the email configured at

Management Center > System > Solution Settings > General information > E-mail (System)

 
Nicolai Høeg Pedersen
Reply

You just type in the one you want and press save...

 

BR Nicolai

 
Nicolai Høeg Pedersen
Reply

Sorry - now I get the question...

 

Base.GetGs("/Globalsettings/Settings/CommonInformation/Email")

 
Dmitrij Jazel
Reply

Hey guys,

Was wondering is it possible to get any feedback why the email could not be sent for example?

Dynamicweb.EmailHandler.Send(mail);

Right now, can't get any exceptions out.

Need to find out why I can't send emails from this server.

 

//Dmitrij

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Dmitrij,

You should use this overload Send(MailMessage mail, bool logging, bool throwException) instead. The first parameter is as you excepted the mail being sent. The second parameter indicated whether you wish to enable extended logging or not (default is true). The third parameter indicates whether you want an encountered exception to be thrown or not (default is false). Honestly, I don't know why it defaults to false, but that is how it is :)

- Jeppe

 
Dmitrij Jazel
Reply

Nice!

Worked perfectly, thanks allot Jeppe :)

[3/14/2014 2:29:35 PM]:    ERROR: Mail not sent0 1 2 No valid SMTP host found. Please specify a valid SMTP host in Control Panel.3 

This means that that if SMTP is provided in GlobalSettings THAN that might be a problem at SMTP server itself.

THe thing is that in this hosting environment I requested a SMTP server details, all I was given is an IP adress. I guess that I can use that IP adress as SMTP server, or do I really need to have SMTP.servername.dk ?

 

//Dmitrij

 

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

To my knowledge, an IP should work just fine. As long as it has a correct SMTP setup.

 
Jonas Mersholm
Reply

Theres no problems in using a hard-coded IP address to the SMTP server. Theres both good and bad things about doing so though.

Logically, you spare a little time, on not having to do the IP lookup from the qualified name, and, you're good to go, if the nameserver goes down someday. 

From a business-perspective i wold rather use a qualified name though. If you decide to change IP address someday, you will not have to edit the IP address multiple places, but can just edit the DNS record pointer.

 

Jonas

 

You must be logged in to post in the forum