Click here to Skip to main content
12,817,112 members (34,450 online)
Rate this:
Please Sign up or sign in to vote.
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 !

    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;
            MailAddress fromAddress = new MailAddress(from, "Mail System");
            message.From = fromAddress;
            if (ccList != null && ccList != string.Empty)
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.Body = body;
            smtpClient.Host = "";   // We use gmail as our smtp client
            smtpClient.Port = 587;
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = new System.Net.NetworkCredential("Mail", "Pass");

            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 = "";
        lblSent.Visible = true;

Posted 30-Jan-13 16:57pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Ahmad Abd-Elghany 30-Jan-13 23:39pm
i turn it to false and did not work
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

use below function
 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);
        SmtpServer.Host = SMTPHost;
        SmtpServer.Port = SMTPPort;
        SmtpServer.Credentials = new NetworkCredential(CredentialEmailId, CredentialPassword);
        SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            return true;
        catch (Exception ex)
            throw ex;
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100