• GoDaddy Community
  • Building and Managing a Website
  • Building and Managing a Website

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 
    doctorallan
    Novice

    How to Configure SMTP correctly?

    Hi, I'm new to building websites and I'm trying out a simple website with a Contact Us page. Basically, the users can just send emails for queries. I've been working on this for hours and tried some of the solutions that worked for others but to no avail, I'm still stuck. Here's some info, these might help you understand my situation.

     

    Web Hosting: godaddy

    SMTP/Mail Server: Yahoo! 

     

    I tried changing the web.config file to this:

     

    <configuration>
      <system.web>
        <trust level ="Full"/>
        <customErrors mode="Off"/>
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2"/>
        <httpRuntime targetFramework="4.5.2"/>
        <httpModules>
          <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
        </httpModules>
      </system.web>
      <system.net>
      <mailSettings>
          <smtp deliveryMethod="Network" from="<uname@uname.com>">
          <network host="smtp.mail.yahoo.com" port="587" password="<pass>" userName="<uname@uname.com>" defaultCredentials="false"/>
        </smtp>
      </mailSettings>
    </system.net
      <system.codedom>
      </system.codedom>
      <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
          <remove name="ApplicationInsightsWebTracking"/>
          <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
            preCondition="managedHandler"/>
        </modules>
      </system.webServer>
    </configuration>

    I got the port no: 587 and host: smtp.mail.yahoo.com from the Yahoo settings. It works on the local machine but not on the production server. 

     

    Here's the vb.net code for this action:

     Protected Sub btn_Send_Click(sender As Object, e As EventArgs) Handles btn_Send.Click
            Try
                Dim Smtp_Server As New SmtpClient
                Dim e_mail As New MailMessage()
                Smtp_Server.UseDefaultCredentials = False
                Smtp_Server.Credentials = New Net.NetworkCredential("uname@uname.com", "pass")
                Smtp_Server.Port = 587
                Smtp_Server.EnableSsl = True
                Smtp_Server.Host = "smtp.mail.yahoo.com"
    
                e_mail = New MailMessage()
                e_mail.From = New MailAddress("uname@uname.com")
                e_mail.To.Add("uname@uname.com")
                e_mail.Subject = "[" & TextBox1.Text & "]  [" & TextBox3.Text & "] Subject:" & TextBox5.Text
                e_mail.IsBodyHtml = False
                e_mail.Body = "Number: " & TextBox2.Text & vbNewLine & "Company: " & TextBox4.Text & vbNewLine & "Message: " & TextArea1.InnerText
    
                Smtp_Server.Send(e_mail)
                Msg("Mail Sent")
    
                e_mail = New MailMessage()
                e_mail.From = New MailAddress("uname@uname.com")
                e_mail.To.Add(TextBox3.Text)
                e_mail.Subject = "Thank you for sending us a message"
                e_mail.IsBodyHtml = False
                e_mail.Body = "Dear " & TextBox1.Text & "," & vbNewLine & "We received your inquiry, Please expect a reply within a few hours. " & vbNewLine & vbNewLine & "Thank you for using the this website. This is a system-generated e-mail. Please do not reply."
                Smtp_Server.Send(e_mail)
                Msg("Mail Sent")
    
                TextBox1.Text = ""
                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox4.Text = ""
                TextBox5.Text = ""
                TextArea1.InnerText = ""
                btn_Send.Text = "Sent."
            Catch error_t As Exception
                Msg("Error sending: Check entered fields.")
                btn_Send.Text = "Catched error."
            End Try
        End Sub

    Do I need to configure anything else aside from the web.config file and the vb.net code? Am I doing it wrong with the web.config and the code?

     

    Thanks for those who'll be helping me! Smiley Very Happy

     

     

    4 REPLIES
    SamHobbs
    Mentor

    Re: How to Configure SMTP correctly?

    @doctorallan, the following is what I have in my web.config:

     

    <system.net>
      <mailSettings>
        <smtp deliveryMethod="Network">
          <network host="relay-hosting.secureserver.net" port="25" />
        </smtp>
      </mailSettings>
    </system.net>

    I forget where the GoDaddy article is at but that is what they say and it works for me. The foolowing is my C#:

     

    try
    {
        MailMessage mm = new MailMessage();
        mm.To.Add(new MailAddress(ConfigurationManager.AppSettings["MailMessageTo"]));
        mm.From = new MailAddress(TextBoxemail.Text);
        mm.Subject = "Contact Me";
        mm.Body = Message.Text;
        mm.IsBodyHtml = false;
        new SmtpClient().Send(mm);
        TextBoxSuccess.Visible = true;
    }
    catch (Exception ex)
    {
        Result.Text = "Error: " + ex.ToString();
        Result.Visible = true;
    }

    That is relatively simple but it works. You can use that as a guide.

    doctorallan
    Novice

    Re: How to Configure SMTP correctly?

    Thanks for this @SamHobbs. I tried this. But it still didn't work for me. Are you using other emails address where the messages should be sent? Like Yahoo or Google/Gmail? I think I'm going to try your c# code tho. Did you set-up anything else? Or just the codes and the web.config file? Sorry for all the questions.

    SamHobbs
    Mentor

    Re: How to Configure SMTP correctly?

    @doctorallan, note that I get the "to" address from settings using:

    ConfigurationManager.AppSettings["MailMessageTo"]

    Did you put your "to" address in the settings? If you don't want to then you can replace that part with the actual email address. The advantage of the settings is that we don't need to modify the code to change the setting.

     

    The email address I am using is an account for the web site. The site was hosted in GoDaddy but at the moment I was using a different server for email but it is even more likely to work if you are using an email address for the web site and the site is hosted by GoDaddy and the email is using the GoDaddy email server; in other words, nothing unusual.

     

    SamHobbs
    Mentor

    Re: How to Configure SMTP correctly?

    @doctorallan, the following is the aspx code for the form, in case this helps you understand the code. Note that TextBoxSuccess and Result are TextBoxes.

     

    <form id="form1" runat="server">
    <div>
        Your email address:&nbsp;&nbsp;<asp:TextBox ID="TextBoxemail" runat="server" Width="300px"/>
        <br /><br />
        Your message:<br />
        <asp:TextBox id="Message" TextMode="multiline" Columns="60" Rows="20" runat="server" />
        <br />
        <br />
        <asp:Button ID="ButtonSend" runat="server" Text="Send"  />
        &nbsp;<asp:TextBox ID="TextBoxSuccess" runat="server" Width="98px" Visible="False">Message sent.</asp:TextBox>
        <br />
        <br />
        <asp:TextBox id="Result" TextMode="multiline" Columns="60" Rows="20" runat="server" Visible="False" />
    </div>

    Except the ButtonSend button has an "OnClick" for "ButtonSend_Click". For some reason this forum software does not allow code to be posted that looks like that.