Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi , i have this code scritp that send message from the contact us form to the mail.
but it work good on my local server , after upload the side it never send it !
C#
public string SendMail(string toList, string from, string ccList, string subject, string body)
{

    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    SmtpClient smtpClient = new SmtpClient();
    string msg = string.Empty;
    try
    {
        MailAddress fromAddress = new MailAddress(from, "Mail System");
        message.From = fromAddress;
        message.To.Add(toList);
        if (ccList != null && ccList != string.Empty)
            message.CC.Add(ccList);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Host = "smtp.gmail.com";   // We use gmail as our smtp client
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("Mail", "Pass");

        smtpClient.Send(message);
        msg = "Successful<BR>";
    }
    catch (Exception ex)
    {
        msg = ex.Message;
    }
    return msg;
}
protected void btnSend_Click(object sender, ImageClickEventArgs e)
{
    string msg = "<center><h2><font color=blue> (Contact Us SYSTEM)</h2></font></center><br>Date: " +
      DateTime.Now.ToString() + "<br />" + "<br>Name: " + txtName.Text + "<br />" +
      "<br>Email " + txtEmail.Text + "<br />" + "<br>Subject: " + drpCat.SelectedItem.Text + "<br />" + "<h3><br>Message:</h3> " +
     "<h3>" + txtMessage.Text + "</h3>";
    //Create Mail Message Object with content that you want to send with mail.
    System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage("Sender Mail", "Recever Mail",
  drpCat.SelectedItem.Text, msg);

    SendMail("Mail", "Mail", null, drpCat.SelectedItem.Text, msg);


    txtEmail.Text = "";
    txtName.Text = "";
    txtMessage.Text = "";
    drpCat.ClearSelection();
    lblSent.Visible = true;
    Response.Redirect("Thanx.aspx");
}
Posted

You set UserDefaultCredentials to True, which tells the SmtpClient to ignore any settings in the Credentials property. This results in the SMTP server getting the credentials of the ASP.NET account your code is running under, NOT the credentials you provided.
 
Share this answer
 
Comments
Ahmad Abd-Elghany 30-Jan-13 23:39pm    
i turn it to false and did not work
use below function
C#
public bool SendEmialWithSecure(string ToEmailId, string FromEmailId, string FromName, string SenderEmailId,
       string SenderName, string Subject, string MailBody, string SMTPHost, Int32 SMTPPort,
       string CredentialEmailId, string CredentialPassword, string strAttachment)
   {
       System.Net.Mail.MailMessage ResetPassMail = new System.Net.Mail.MailMessage();
       SmtpClient SmtpServer = new SmtpClient();
       ResetPassMail.To.Add(new MailAddress(ToEmailId));
       ResetPassMail.From = new MailAddress(FromEmailId, FromName);
       ResetPassMail.Sender = new MailAddress(SenderEmailId, SenderName);
       ResetPassMail.Subject = Subject;
       ResetPassMail.IsBodyHtml = true;
       ResetPassMail.Body = MailBody;
       ResetPassMail.Priority = System.Net.Mail.MailPriority.High;
       Attachment attach = new Attachment(strAttachment);
       ResetPassMail.Attachments.Add(attach);
       SmtpServer.Host = SMTPHost;
       SmtpServer.Port = SMTPPort;
       SmtpServer.Credentials = new NetworkCredential(CredentialEmailId, CredentialPassword);
       SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
       try
       {
           SmtpServer.Send(ResetPassMail);

           return true;
       }
       catch (Exception ex)
       {

           throw ex;
       }

   }
 
Share this answer
 
Comments
Ahmad Abd-Elghany 31-Jan-13 0:24am    
iti didnot work !
Mr. Mahesh Patel 31-Jan-13 7:17am    
My 5 Existing application is using above function and it is working fine till last 8 months, Please check your user Credentials, SMTL Server Setting, Make sure that port is open which is used in sending mail, Firewall of both SMTP and your pc is enable for this port and IP address, also check that you have proper internet connection and your antivirus not restrict your process/application.

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