Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys..
I have a piece of code written to send e-mail from webpage to a gmail id. Below is the full CS page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class Contact_Us : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {            
                    sendmail();
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language=JavaScript>alert('Query sent successfully.');</script>");
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                    TextBox3.Text = "";
                    TextBox4.Text = "";                
        }
        catch(Exception)
        {

        }
    }

    void sendmail()
    {
        string mail_text = TextBox3.Text + "\n\nFrom: " + TextBox1.Text + "\n\nE-Mail: " + TextBox2.Text;
        MailMessage message = new MailMessage("id@gmail.com", "id@gmail.com", TextBox4.Text, mail_text);
        SmtpClient clnt = new SmtpClient();
        clnt.Port = 587;
        clnt.Host = "smtp.gmail.com";
        clnt.Credentials = new System.Net.NetworkCredential("id@gmail.com", "password");
        clnt.EnableSsl = true;
        clnt.Send(message);
    }
}


Now the problem that I am facing is, when i run this code in my local server through visual studio, it works fine and the mail gets sent. But when i published the site and is trying to send mail from the web server, the mail is not getting sent. Also the textboxes are not getting blank, so my guess is the code is throwing some exception and is going over to the Catch section. What is the issue and what do i need to change?? Please help..
Thanks :)
Posted
Updated 1-Mar-13 7:43am
v2

We can't tell from that any more than you can.

The first thing you need to do is stop swallowing all exceptions - they are there for a reason, and if you just blankly swallow it without even looking or recording the fact of an exception, you will never be able to work out what the heck the problem is.

So get the exception, and log it to a file, or a database, or whatever you have available, so you can look at the content and work out what the problem is given some actual information on why it failed!
 
Share this answer
 
Fifteen years ago you probably could have done this without any hindrance. Spam is a major problem nowadays. The site that hosts your webserver likely has mucho firewall rules that block web pages from sending email from any of their hosting sites. If the hosting company didn't do this, a spammer could purchase a hosting package and then use the web host site to spam away using the hosting company servers.

My first guess is that outgoing email (ie. smtp) is likely blocked, intentionally. You will likely need to have a polite discussion with your web host company and maybe agree to some additional terms. This might fix the problem you have, but be prepared that the response to your request might be "not just no but HELL NO"!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900