I would normally call a method to send mail from the actual Razor form. I've never actually used the umbraco sendmail() function, I normally just role my own. A simple example I've sent to other people would be:
try
{
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress("[email protected]"));
message.Subject = "Subject of your email";
message.Body = String.Format("Name: {0} {1} <br/> Email: {2} <br/> Message: {3} <br/>",
formModel.FirstName, formModel.LastName, formModel.Email, formModel.Comment);
SmtpClient client = new SmtpClient();
client.Send(message);
}
catch (SmtpException sex)
{
<h3>Oops - Your Message Could Not Be Sent</h3>
<p>The following error was reported by the mail server: @sex.Message</p>
}
catch (Exception ex)
{
<p>We are very sorry but an error occurred sending your message. Please try again later.</p>
<pre>@ex.ToString()</pre>
}
You can call that from inside the DisplayCompletionMessage() helper method in the razor script (just after the line Session.Add(ContactFormModel.SessionKey, true);
Remember to add @using System.Net.Mail to the top of the razor script. If you get any errors they should be caught and displayed (often these are down to mail server configuration problems).
I implemented your sendmail method and everything is working:
The only thing I changed was putting the call to the SendMail method in the condition where there is checked for errors:
formModel = new ContactFormModel(Request.Form);
var errors = formModel.Validate();
if (errors.Count > 0)
{
@RenderForm(formModel)
@DisplayErrors(errors)
}
else
{
@SendMail(formModel)
}
Because in my scenario the only thing that happens after clicking the submit button is to send it to the visitors and editors mailbox. I then put the call to the DisplayCompletionMessage in the SendMail method's try-catch block because it makes sense to only show a completion message when the mail was succesfully sent:
try{
...
@DisplayCompletionMessage(formModel);
}
catch (SmtpException sex)
{
<h3>Oops - Your Message Could Not Be Sent</h3>
<p>The following error was reported by the mail server: @sex.Message</p>
}
catch (Exception ex)
{
<p>We are very sorry but an error occurred sending your message. Please try again later.</p>
}
So again, thanks a lot for your excellent blogpost on creating forms for Umbraco with Razor. I'll be using this implementation on a lot of websites because it's so flexible and easy to maintain.
Glad it all worked out for you! What you say makes perfect sense. It took me a while to come up with a solution for forms in Razor, but I like it because it is so adaptable, as you have found. I now have completley ditched web-forms from most of my Umbraco solutions! Thanks for all the postive feedback - I appreciate it.
I have struggled with form mails for some time and now I am trying this package. I have added the code from Anthony's first post right below "Session.Add(ContactFormModel.SessionKey, true);". But I am not sure where to setup the smtp that i want to use for sending out mails and at the moment i have added it in the webconfig file. Am I missing out something?
I have just figured it out why my test did not work. It was because I was testing locally. As soon as I uploaded the test to a live server i worked straight away.
By the way it was your code I used and not Anthony's like I wrote in my first post.
Thank's for your advice regarding a pickup directory i will try that out also.
Thank you for this great package! It has very clean code and it is very easy to integrate in my templates.
add a Sendmail member to the ContactFormModel
I'm trying to add SendMail() functionality to the Diplo Razor Form.
I'm wondering where it is best to add this functionality, in the ContactFormModel.cs class itself or in the RazorForm.cshtml script,
I implement the SendMail() functionality using the umbraco.library.sendmail() helper method.
I tried both options, but when I try to debug the SendMail() method in Visual Studio it isn't stepping into the SendMail() method.
thanks,
Anthony
Hi Anthony,
I would normally call a method to send mail from the actual Razor form. I've never actually used the umbraco sendmail() function, I normally just role my own. A simple example I've sent to other people would be:
Hi Dan,
Thanks for the code.
I had added my sendmail method as a member to the ContactFormModel.cs class.
In this method I used the umbraco.library.sendmail method like so:
umbraco.library.SendMail("from-email-address", "to-email-address", subject, message, bool ishtml)
This works fine, but as I said in my previous mail, I can't debug it in Visual Studio (because it's compiled dynamically?)
So I'll try your method and let you know the result.
greetings,
Anthony
Hi Dan,
I implemented your sendmail method and everything is working:
The only thing I changed was putting the call to the SendMail method in the condition where there is checked for errors:
formModel = new ContactFormModel(Request.Form);
var errors = formModel.Validate();
if (errors.Count > 0)
{
@RenderForm(formModel)
@DisplayErrors(errors)
}
else
{
@SendMail(formModel)
}
Because in my scenario the only thing that happens after clicking the submit button is to send it to the visitors and editors mailbox. I then put the call to the DisplayCompletionMessage in the SendMail method's try-catch block because it makes sense to only show a completion message when the mail was succesfully sent:
try{
...
@DisplayCompletionMessage(formModel);
}
catch (SmtpException sex)
{
<h3>Oops - Your Message Could Not Be Sent</h3>
<p>The following error was reported by the mail server: @sex.Message</p>
}
catch (Exception ex)
{
<p>We are very sorry but an error occurred sending your message. Please try again later.</p>
}
So again, thanks a lot for your excellent blogpost on creating forms for Umbraco with Razor. I'll be using this implementation on a lot of websites because it's so flexible and easy to maintain.
greetings,
Anthony
Hi Anthony,
Glad it all worked out for you! What you say makes perfect sense. It took me a while to come up with a solution for forms in Razor, but I like it because it is so adaptable, as you have found. I now have completley ditched web-forms from most of my Umbraco solutions! Thanks for all the postive feedback - I appreciate it.
Dan
Hi Dan,
Thank you very much for the beautiful package!
Best regards
Kim
Anthony can you share your package?
thanks :-)
Hi
I have struggled with form mails for some time and now I am trying this package. I have added the code from Anthony's first post right below "Session.Add(ContactFormModel.SessionKey, true);". But I am not sure where to setup the smtp that i want to use for sending out mails and at the moment i have added it in the webconfig file. Am I missing out something?
Kind regards
René
Hi Rene,
You are correct - you configure SMTP settings in web.config in:
Obviously you'll need to be on a server with SMTP running that you have access to.
If you don't, you can try setting a "pickup directory" instead (where the emails are written to a file on disk) using the method here:
http://weblogs.asp.net/gunnarpeipman/archive/2010/05/27/asp-net-using-pickup-directory-for-outgoing-e-mails.aspx
Dan
Hi Dan,
I have just figured it out why my test did not work. It was because I was testing locally. As soon as I uploaded the test to a live server i worked straight away.
By the way it was your code I used and not Anthony's like I wrote in my first post.
Thank's for your advice regarding a pickup directory i will try that out also.
Thank you for this great package! It has very clean code and it is very easy to integrate in my templates.
// René
is working on a reply...