Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL SMTP
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;
        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 30-Jan-13 16:57pm
Rate this: bad
good
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.
  Permalink  
Comments
Ahmad Abd-Elghany at 30-Jan-13 23:39pm
   
i turn it to false and did not work
Rate this: bad
good
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);
        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;
        }
       
    }
  Permalink  
Comments
Ahmad Abd-Elghany at 31-Jan-13 0:24am
   
iti didnot work !
Mr. Mahesh Patel at 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
0 OriginalGriff 605
1 Maciej Los 225
2 Abhinav S 171
3 Richard Deeming 160
4 Suvendu Shekhar Giri 159
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 9,095
2 Peter Leow 5,044
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 2,581


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2015
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