Copied to clipboard

Flag this post as spam?

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


  • Sharmarke Hujale 103 posts 346 karma points
    Dec 08, 2015 @ 19:43
    Sharmarke Hujale
    0

    Sending mails with contact form

    Hi!

    I want to send email through a contact formular

    This is how it looks like in my web config file:

    <mailSettings>
        <smtp>
          <network host="smtp.gmail.com" port="587" userName="[email protected]" password="******" defaultCredentials="false" />
        </smtp>
    </mailSettings>
    

    But I don't know how to make the razor or the function to send it.

    I've tried something like this on a partial view macro:

    @using System;
    @using System.Linq;
    @using System.Net.Mail;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
    string name = Request["Name"];
    string email = Request["Email"];
    string subject = Request["Subject"];
    
    if (IsPost)
    {
        try
        {
    
    
            var body = "<p>Email From: {0} ({1})</p><p>Message:</p><b>{2}</b>";
            var message = new MailMessage();
            message.To.Add(new MailAddress("[email protected]"));  // replace with valid value 
            message.From = new MailAddress(email);  // replace with valid value
            message.Subject = (subject);
            message.Body = string.Format(body, name, email, message);
            message.IsBodyHtml = true;
    
            using (var smtp = new SmtpClient())
            {
                var credential = new NetworkCredential
                {
                    UserName = "[email protected]",  // replace with valid value
                    Password = "********"  // replace with valid value
                };
                smtp.Credentials = credential;
                smtp.Host = "smtp.gmail.com ";
                smtp.Port = 587;
                smtp.EnableSsl = true;
            }
    
        }
        catch (Exception ex)
        {
    
        }
    
    
        <text>
            You entered: <br />
            Name: @name <br />
            Email: @email <br />
        </text>
    }
    }
    
    <form role="form" id="formID" method="post">
    <h3 style="margin-bottom: 25px;">Say Hello.</h3>
    <p>Ideas, Feedback &amp; Suggestions.</p>
    <!--Contact form-->
    <div class="form-group">
        <label>Your name (required)</label>
        <input type="text" class="form-control" id="Name" name="name" placeholder="Name*" required>
    </div>
    <div class="form-group">
        <label>Your Email (required)</label>
        <input type="text" class="form-control" id="Email" name="email" placeholder="Email*" required>
    </div>
    <div class="form-group">
        <label>Subject (required)</label>
        <input type="text" class="form-control" id="Subject" name="subject" placeholder="Subject*" required>
    </div>
    <div class="form-group">
        <label>Message (required)</label>
        <textarea class="form-control" type="textarea" id="Message" placeholder="Message*" maxlength="250" rows="10"></textarea>
    </div>
    <button type="submit" id="submit" name="SEND" class="btn btn-default-custom-reverse">SEND</button>
    

    But it doens't work.... Has anyone a better way to tackle this problem of mine? I would appreciate it very much

    Thanks in advance

    /Sharmarke

  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    Dec 08, 2015 @ 20:18
    Dennis Aaen
    0

    Hi Sharmarke,

    If you have access to Umbraco TV, then I think what you need to do, is in this video.

    https://umbraco.tv/videos/developer/fundamentals/surface-controllers/handling-posts/

    Also a good tool to test if there is something wrong with you SMTP settings is the tool called SMTP4dev, you can download the tool here. https://smtp4dev.codeplex.com

    Hope this helps,

    /Dennis

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 08, 2015 @ 20:34
    Sharmarke Hujale
    0

    Hi Dennis

    I don't have acces to UmbracoTv - can you suggest something else?

    Have you tried to send an email to a gmail or live/hotmail before?

    /Sharmarke

  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    Dec 08, 2015 @ 21:00
    Dennis Aaen
    100

    Hi Sharmarke,

    Maybe this blog post from Warren Buckley can help you a step further,

    http://creativewebspecialist.co.uk/2013/07/22/umbraco-mvc-what-on-earth-is-a-surface-controller/

    Hope this helps,

    /Dennis

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 08, 2015 @ 22:29
    Sharmarke Hujale
    0

    Hi Dennis,

    I used the link which you send me, but I ran into some problems

    enter image description here

    As you can see

    namespace CCUD.Controllers
    {
    public class ContactFormSurfaceController : SurfaceController
    {
        public ActionResult RenderContactForm()
        {
            return PartialView("ContactForm", new ContactFormViewModel());
        }
    }
    
    }
    

    Here's my Controller,

    Can you please help me with this,

    Maybe I've overlooked something...

    /Sharmarke

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 08, 2015 @ 22:48
    Sharmarke Hujale
    0

    Hi Dennis,

    I got it working!!! Is just that my Controller name was "ContactFormSurface" and not "ContactSurface"

    And now I can send emails to a gmail account

    Thanks a lot, again!

    /Sharmarke

  • gary 385 posts 915 karma points
    Dec 08, 2015 @ 22:51
    gary
    0

    Hi Sharmarke

    Ran into exactly this yesterday.

    The second part of the Html.Action is the route, took me a long time to find that one!

    So your class being ContactFormSurfaceController, you need ContactFormSurface, omitting the controller.

    @Html,Action("RenderContactForm", "ContactFormSurface")
    

    That should call the correct route for you.

    The part where I am stuck is the first part which is the action in the controller, clearly yours is RenderContactForm, so you should get the result.

    Hope it helps

    Gary

  • Sharmarke Hujale 103 posts 346 karma points
    Dec 08, 2015 @ 23:15
    Sharmarke Hujale
    0

    Hi Gary,

    I figure it out, after I send my post reply to Dennis, lol!

    But thanks anyway!

    /Sharmarke

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies