0

I am using the following piece of code to send mail which is working perfectly when run on local host.I uploaded the site to the server and now when i try to achieve the same thing the mail won't get sent.Please help.

public void send_click(object sender, EventArgs e)
{
    mail();
}

public void mail()
{
    SmtpClient smtpClient = new SmtpClient();

    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

    MailAddress fromAddress = new MailAddress(tb_email.Text, tb_name.Text);

    smtpClient.Host = "smtp.gmail.com";
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.UseDefaultCredentials = false;

    smtpClient.EnableSsl = true;


    smtpClient.Credentials = new NetworkCredential("infoinnovative77@gmail.com", "***Password***");

    smtpClient.Port = 587;

    message.From = fromAddress;

    message.To.Add(new MailAddress("info@mblube.com"));

   // message.Subject = ddlSubject.SelectedItem.Text;

    //message.CC.Add("taxistax@outlook.com");

    message.IsBodyHtml = true;
    StringBuilder msg = new StringBuilder();

    msg.Append(tb_msg.Text);
    msg.Append("<br/>");
    msg.Append(tb_email.Text);
    message.Body = msg.ToString();




    //this code adds event handler to notify that mail is sent or not
    smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);

    smtpClient.Send(message);
    if (smtpClient.EnableSsl == true)
    {

        string CloseWindow = "alert('Message Sent Successfully!');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
    }
    else
    {
        string CloseWindow = "alert('Problem in Sending mail...try later!');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
    }


}
void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    System.Net.Mail.MailMessage mailMessage = e.UserState as System.Net.Mail.MailMessage;
    if (e.Cancelled || e.Error != null)
    {

        Response.Write(e.Error.Message);
        Response.Write(e.Error.StackTrace);
    }
    else
    {
        Response.Write("Email sent successfully");
    }
}
Chelsea
  • 751
  • 1
  • 9
  • 23
  • On which server you have hosted If the server location is away from you Google avoid logging from different place you need to change the setting in your google account – Sagar Chavan Oct 09 '14 at 06:19
  • Check the Event Viewer of the server. Are any warnings for smtpsvc or errors for asp.net recorded? Have you tried using a pickupdirectory? With this you could ensure, that your application is working fine and the error is likely caused by the server/firewall/ISP – Marco Oct 09 '14 at 06:32

4 Answers4

0

If you have uploaded to an enterprise server: 1. Make sure your firewall is not blocking outgoing requests for the w3wp.exe which is the IIS Worker Process; 2. Verify which error message you are getting on the page as well as on the event viewer;

The way you have sent is almost the same as i have been sending but without the sendCompleted event so it shouldn't be a problem with the code. Also it is almost the same as this post and this one.

Community
  • 1
  • 1
Turay Melo
  • 114
  • 5
0

Instead of SmtpClient smtpClient = new SmtpClient(); Do SmtpClient smtpClient = new SmtpClient("xx.xx.xxx.xx");

Where xx.xx.xxx.xx is the ip address of the domain.If you are using say godaddy server, it will provide the ip address for you to host your site.If you dont know the ip address simply ping the site name.Example if your site name is sum1.com then do ping sum1.com..it will show the ip address provided by your server.

suman
  • 728
  • 3
  • 10
  • 29
0

We can't tell from here if the error is located within your application or your hosting environment. There are a few steps you can take to narrow down the source of your problem.

First of all: check the Windows EventLog. If there are Asp.net errors or smtpsvc warnings, this might be related and give you a clue on what is failing!

To check if your application is per se working and not throwing exceptions while trying to send a mail change the mail settings in your web.config

<system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\smtp"/>
      </smtp>
    </mailSettings>
  </system.net>

If you now want to send a mail from within you application, it will get dropped into c:\smtp. If it is there your application is working fine and the problem is likely to be caused from your server, firewall or ISP.

Secondly fireup a cmd shell and check if your smtp settings are correct by telnetting onto it:

telnet mail.yourmailrpvider.net 25

If this throws an error, it is very likely that your firewall is blocking smtp traffic and you need to get in contact with your administrator

Marco
  • 22,856
  • 9
  • 75
  • 124
0

pls change the smtpClient.Host and credentials according to the server where you have uploaded the application.

Aarif Qureshi
  • 474
  • 1
  • 3
  • 13