在Web服务器上发送邮件的问题

问题描述:

你好,

我有一个联系我们表格,在我当地的主机上工作正常。但在将其上传到托管服务器后它无法正常工作。下面是我的代码。

hello,
i hava a contact us form that is working fine on my local host. but after uploading it to the hosting server it is not working . below is my code.

<div style="width:300px; margin-left:30px; float:left; height:auto;">
<h1>Feedback Form</h1><hr />
 <fieldset>
<legend>Contact us</legend>


<div>
<%--<asp:Label ID="Name" runat="server" Text="Your Name*:"/><br/>--%>
<asp:TextBox ID="txtName" runat="server" placeholder="Your Name" CssClass="twitterStyleTextbox"  /><br />
<asp:RequiredFieldValidator ID="RV1" runat="server"

                            ControlToValidate="txtName"  ForeColor=Red

                            ErrorMessage="Please Enter Your Name"

                            SetFocusOnError="True">
</asp:RequiredFieldValidator><br />
</div>

<div>
<%--<asp:Label ID="Email" runat="server" Text="Email*:"/><br/>--%>
<asp:TextBox ID="txtMail" runat="server" placeholder="Your Email Id" CssClass="twitterStyleTextbox"/><br />
<asp:RequiredFieldValidator ID="RV2" runat="server"

                            ControlToValidate="txtMail" ForeColor=Red

                            ErrorMessage="Your Email Address"

                            SetFocusOnError="True">
</asp:RequiredFieldValidator><asp:RegularExpressionValidator ID="RegularExpressionValidator1"

    runat="server" ErrorMessage="Please Enter valid Email Address"

    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

        ControlToValidate="txtMail" ForeColor="#FF3300"></asp:RegularExpressionValidator><br />
</div>

<div>
<%--<asp:Label ID="Sub" runat="server" Text="Subject*:"/><br/>--%>
<asp:TextBox ID="txtSubject" runat="server" placeholder="Subject" CssClass="twitterStyleTextbox"/><br />
<asp:RequiredFieldValidator ID="RV3" runat="server"

                            ControlToValidate="txtSubject" ForeColor=Red

                            ErrorMessage="Reason to contact us"

                            SetFocusOnError="True">
</asp:RequiredFieldValidator><br />
</div>

<div>
<%--<asp:Label ID="Message" runat="server" Text="Feedback:"/><br/>--%>
<asp:TextBox ID="txtMessage" runat="server" CssClass="twitterStyleTextbox"

        placeholder="Your Message" TextMode="MultiLine" Width="249px"/><br />
<asp:RequiredFieldValidator ID="RV4" runat="server"

                            ControlToValidate="txtMessage" ForeColor=Red

                            ErrorMessage="Please write your feedback"

                            SetFocusOnError="True">
</asp:RequiredFieldValidator><br />
</div>

<div>
<asp:Button  ID="btnSubmit" runat="server"

            Text="Submit" onclick="btnSubmit_Click"/>
</div>
<asp:ValidationSummary ID="ValidationSummary1"

                       runat="server"/>
</fieldset>
<asp:Label ID="Label1" runat="server" Text=""/>
</div>





我的c#代码



my c# code

private void sendmail()
   {
       try
       {

           str_mailer.Append(@"<b>Name : </b>  " + txtName.Text);
           str_mailer.Append(@"<br/> <b>Email Address : </b>" + txtMail.Text);
           str_mailer.Append(@"<br/><b>Subject : </b>" + txtSubject.Text);
           str_mailer.Append(@"<br/><b>Message : </b>" + txtMessage.Text);
           System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(ConfigurationManager.AppSettings["id"], ConfigurationManager.AppSettings["to"], "Contact Us", str_mailer.ToString());
           System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["id"], ConfigurationManager.AppSettings["pswd"]);
           System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["smtp"], Convert.ToInt32(ConfigurationManager.AppSettings["port"]));
           mailClient.EnableSsl = true;
           mail.IsBodyHtml = true;
           mailClient.UseDefaultCredentials = false;
           mailClient.Credentials = mailAuthentication;
           mailClient.Send(mail);
           Label1.Text = "<b>Thank you for contacting with us. We will be in contact with you soon.<b>";
       }
       catch
       {



           Label1.Text = "Please wait..";
           sendmail();


       }

   }

   protected void btnSubmit_Click(object sender, EventArgs e)
   {
       sendmail();
       txtMail.Text = "";
       txtMessage.Text = "";
       txtSubject.Text = "";
       txtName.Text = "";
   }





web.config code



web.config code

<appSettings>
  <add key="to" value="to address"/>
  <add key="from" value="mygmail@gmail.com "/>
  <add key="id" value="mygmail@gmail.com   "/>
  <add key="pswd" value="password"/>
  <add key="port" value="587"/>
  <add key="smtp" value="smtp.gmail.com"/>
</appSettings>











plz help someone to me.






plz help someone to me.

Quote:




catch
{
   Label1.Text = "Please wait..";
   sendmail();

   // Please Log the Exception
}

You are sending mail inside Catch Block. Is that what you need? Rather just log the Exception in some File or Database, so that you can track why exactly it is failing to work as expected.



There must be some problems while sending the mail and it might be due to many reasons.

Firewall/AntiVirus blocking the port 587 is a very common issue.



Refer my answer - sending email to gmail from asp.net[^] and check if you are doing it correctly.



Once you log the Exception, you will be able to know the issue specifically. Then you can just search that Exception in Google and try the solutions suggested.

You are sending mail inside Catch Block. Is that what you need? Rather just log the Exception in some File or Database, so that you can track why exactly it is failing to work as expected.

There must be some problems while sending the mail and it might be due to many reasons.
Firewall/AntiVirus blocking the port 587 is a very common issue.

Refer my answer - sending email to gmail from asp.net[^] and check if you are doing it correctly.

Once you log the Exception, you will be able to know the issue specifically. Then you can just search that Exception in Google and try the solutions suggested.