Solution to Sending Email from Site using SMTP

Discussion in 'Email' started by refransen, Jul 21, 2010.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hello everyone,

    I finally got this set up today after a couple hours of pointless tutorials. Perhaps this code can help others :)

    This code uses the input from a "Contact Form" on my website, to use the information and send it to my postmaster email account on discountasp.net.

    This code is put on the "Button_Click" event, when I click the "Send Button" on my contact form within my website.
    Code:
    
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(EmailAddTextBox.Text);
             
                // This is the email where you will be sending the form's contents
                // It can be any valid email
                mail.To.Add("[email protected]");
    
                mail.Subject = "Form Response from YourSite.com";
    
                mail.IsBodyHtml = true;
    
                // This is taken from the contents of my contact form
                mail.Body = "First Name: " + FNameTextBox.Text + "<br />";
                mail.Body += "Last Name: " + LNameTextBox.Text + "<br />";
                mail.Body += "Email: " + EmailAddTextBox.Text + "<br />";
                mail.Body += "Comments: " + CommentTextBox.Text + "<br />";
    
                try
                {
                    SmtpClient smtp = new SmtpClient();
                    smtp.Send(mail);
    
                    // My own functions
                    ClearForm();
                    SuccessPlaceHolder.Visible = true;
                }
                catch (Exception)
                {
                    // Shows a fail message if email was never sent
                    FailPlaceHolder.Visible = true;
                }
    This code is placed in the webconfig file. I placed this at the very bottom, just before the ending of the document.

    Code:
     
    <system.net>
            <mailSettings>
                <smtp from="[email protected]">
                    <network host="localhost" port="25" 
    userName="mydiscountasp username" 
    password="mydiscountasp password" 
    defaultCredentials="true" />
                </smtp>
            </mailSettings>
        </system.net>

    This code ONLY WORKS once your site is live hosted on discountasp.net. Simply running on this code on your computer, it will not work.

    Hope that helps!
     
  2. dmitri

    dmitri DiscountASP.NET Staff

    When you are sending messages through a web application, please specify "localhost" as the SMTP server (which you did) with user name and password left blank to send messages through the local mail relay.
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page