+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / How to fetch smtp username , password from the sitefinity admin site to code?

How to fetch smtp username , password from the sitefinity admin site to code?

5 posts, 0 answered
  1. Surya CD
    Surya CD avatar
    57 posts
    Registered:
    06 Apr 2010
    25 May 2012
    Link to this post
    Hi,
    I need to implement email functionality in  a form in sitefinity. 
    For that I have created a usercontrol and dragged it to the the form. It is working now.

    For that I gave the username and password in the web.config file. But I need to fetch this data
    from the EmailSettings given in the admin site of Sitefinity.

    Also, there are 2 contact forms. I have used the same user control for both forms. When ContactForm1 is submitted, it should go to admin1@site.com and if ContactForm2 is submitted, mail should go to admin2@site.com.

    What should be done so as to provide a pop up so that  these mail ids can be given when the usercontrol is dragged on to the page and not hardcoded in the usercontrol? 

    Thasnks, Surya.
  2. Mark
    Mark avatar
    147 posts
    Registered:
    01 Jun 2010
    25 May 2012
    Link to this post
    This is what I've been using to send mail using the Sitefinity email settings:

    protected void SendNotification()
            {
                try
                {
                    var mailServer = new SmtpClient();
                    var mailMsg = new MailMessage();
                    var clientMailMsg = new MailMessage();
     
                    var smtpSettings = Config.Get<SystemConfig>().SmtpSettings;
     
                    if (!smtpSettings.Host.IsNullOrWhitespace())
                    {
                        mailServer.Host = smtpSettings.Host;
                        mailServer.Port = smtpSettings.Port;
                        mailServer.EnableSsl = smtpSettings.EnableSSL;
                        mailServer.Timeout = smtpSettings.Timeout;
     
                        if (!string.IsNullOrEmpty(smtpSettings.UserName))
                        {
                            mailServer.UseDefaultCredentials = false;
                            mailServer.Credentials = new NetworkCredential(smtpSettings.UserName, smtpSettings.Password);
                        }
                    }
     
                    mailMsg.From = new MailAddress(smtpSettings.DefaultSenderEmailAddress);
                    mailMsg.To.Add(new MailAddress("email@here.com", "Name here"));
                    mailMsg.IsBodyHtml = true;
     
                    mailMsg.Subject = "Subject here";
                    string text = "Text version of your email";
                    string html = @"<p>HTML version of your email</p>";
                    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
                    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
     
                    mailServer.Send(mailMsg);
     
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

  3. Surya CD
    Surya CD avatar
    57 posts
    Registered:
    06 Apr 2010
    26 May 2012
    Link to this post
    HI,
    Thanks for the answer. It worked. Now I can fetch smtp settings in my code.
    Can you pls give a solution for the second question I have asked in my first post.
    ie. There are 2 contact forms. Same user control is used in both. The email notification should go to different address on submitting the forms. 
    Now I  have hardcoded these values in web.config. But the actual requirement is that
    one should be able to provide the address while dragging the usercontrol to the form.
    How can this be done. Please help.

    Thanks, Surya.


    Thanks,
    Surya.
  4. Mark
    Mark avatar
    147 posts
    Registered:
    01 Jun 2010
    26 May 2012
    Link to this post
    You need to install Sitefinity Thunder.

    Then your control must have a get/set for the value you'd like to set using the designer in the backend.

    Then click add file to solution, click Sitefinity, then add designer to existing widget. Thunder will find the widget and find the parameter you want to give access to through the backend and allow you to create the designer interface.

    This is a great video to watch on Thunder to understand some basics: http://www.sitefinity.com/blogs/joshmorales/posts/12-04-19/sitefinity_thunder_webinar_notes.aspx
  5. Surya CD
    Surya CD avatar
    57 posts
    Registered:
    06 Apr 2010
    26 May 2012
    Link to this post
    Hi,
    Thanks for the reply. It would be really helpful if you please tell me the steps in detail.

    Thanks,
    Surya.
5 posts, 0 answered