Copied to clipboard

Flag this post as spam?

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


  • navneet kaur 11 posts 103 karma points
    May 05, 2017 @ 04:15
    navneet kaur
    0

    Send Email From Contact Us Form

    I am using surface controller to send the email.I define the "From Email" ([email protected]) hard coded. But now I want that the admin update the "From Email" field whenever they want.so How can I get the value of email field from admin panel using Datatype. /umbraco.

    MailAddress from = new MailAddress("[email protected]", "testproject");

     [HttpPost]
            public int SendMail(MessageModel model)
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        string subject = "From: " + model.Email;
                        string body = model.Message;
    
                        var smtp = new SmtpClient
                        {
                            Host = "smtp.gmail.com",
                            Port = 587,
                            EnableSsl = true,
                            DeliveryMethod = SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials = new NetworkCredential("[email protected]", "abc123#!")
                        };
                        body = PopulateEmailBody(model.Name, model.Email, model.Mobile, model.Message);
    
                        MailAddress from = new MailAddress("[email protected]", "testproject");
                        MailAddress to = new MailAddress(model.Email,model.Name);
                        MailMessage message = new MailMessage(from, to);
                        message.Subject = subject;
                        message.IsBodyHtml = true;
                        message.Body = body;
                        SmtpClient client = smtp;
                        client.Send(message);
                        //TempData.Add("Success", true);
                        return 1;
                    }
                    catch (Exception ex)
                    {
                        return -1;
                    }
                }
                else
                    return -1;
    
            }
    
  • Jeffrey Schoemaker 408 posts 2138 karma points MVP 8x c-trib
    May 05, 2017 @ 07:16
    Jeffrey Schoemaker
    0

    Hi Navneet,

    you could add a property called "To emailaddress" somewhere in your Umbraco node structure and read this property in your function. This is standard Umbraco-code that you could use.

    We've faced the same problem where editors require they can change almost anything for their emails (to, from, cc, bcc, subject, content, etcetera). We developed a package for that, called PerplexMail (https://our.umbraco.org/projects/backoffice-extensions/perplexmail-for-umbraco/).

    Maybe it's an option for your project too,

    regards,

    Jeffrey

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    May 05, 2017 @ 07:29
    Nik
    0

    Hi Navneet,

    Just an observation, if you are wanting to allow the editors to change the from address, they must also be able to change the login credentials as you are sending via Gmail.

    If your from address isn't the address you are logging into the Google mail server with then I believe Google with reject the mail and not allow you to send it.

    Thanks,

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft