Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Candaele 1197 posts 2049 karma points
    Oct 20, 2012 @ 15:09
    Anthony Candaele
    0

    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

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 20, 2012 @ 20:27
    Dan Diplo
    1

    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:

    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).

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 21, 2012 @ 09:50
    Anthony Candaele
    0

    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

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 21, 2012 @ 15:10
    Anthony Candaele
    0

    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

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 21, 2012 @ 15:26
    Dan Diplo
    1

    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

  • Kim Bantz Rasmussen 81 posts 310 karma points
    Oct 24, 2012 @ 10:06
    Kim Bantz Rasmussen
    0

    Hi Dan,

    Thank you very much for the beautiful package!

    Best regards
    Kim 

  • MorettoCarlo 6 posts 27 karma points
    Mar 06, 2013 @ 17:26
    MorettoCarlo
    0

    Anthony can you share your package?

    thanks :-)

  • René Andersen 238 posts 684 karma points
    Jun 21, 2013 @ 18:09
    René Andersen
    0

    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é

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jun 21, 2013 @ 22:00
    Dan Diplo
    1

    Hi Rene,

    You are correct - you configure SMTP settings in web.config in:

      <system.net>
        <mailSettings>
          <smtp>
            <network host="127.0.0.1" userName="username" password="password" />
          </smtp>
        </mailSettings>
      </system.net>

    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

  • René Andersen 238 posts 684 karma points
    Jun 21, 2013 @ 22:24
    René Andersen
    0

    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é

Please Sign in or register to post replies

Write your reply to:

Draft