Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear All,
I am trying to send email code with help of my web page.
i am using my gmail id for that.
i am getting error :

{"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.129.109:25"}

here is my c# code

C#
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;
namespace sendmail
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
         protected void btnSubmit_Click(object sender, EventArgs e)

    {

        MailMessage Msg = new MailMessage();

        Msg.From = new MailAddress("email@gmail.com");

        Msg.To.Add(txtToMail.Text);

        Msg.Subject = txtSubject.Text;

        Msg.Body = txtMessage.Text;

        Msg.IsBodyHtml = true;

 
        SmtpClient smtp = new SmtpClient();

        smtp.Host = "smtp.gmail.com";

        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();

        NetworkCred.UserName = "email@gmail.com";

        NetworkCred.Password = "mypassword";

       // smtp.Timeout = 10000;

        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
   
        smtp.UseDefaultCredentials = false;

        smtp.Credentials = NetworkCred;

        smtp.Port = 25;

        smtp.EnableSsl = false;

        smtp.Send(Msg);
       
        lblMsg.Text = "Email has been successfully sent..!!";
    
     
    }
        
    }

}
Posted
Updated 6-Feb-14 3:26am
v2
Comments
joginder-banger 6-Feb-14 9:30am    
try this code it's may be solve your problem.smtp.EnableSsl = true;
Member 10576515 6-Feb-14 11:31am    
already tried this but not working..
pdoxtader 6-Feb-14 9:43am    
smtp.UseDefaultCredentials should be set to true, and I believe Gmail requires an SSL connection. Try using my class - it'll make this easier for you.

-Pete

Refer my previous answer sending email to gmail from asp.net[^].

You will get the full working code.
 
Share this answer
 
This is the class I use:

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Web;
using System.Net.Mail;
using System.ComponentModel;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

public class EmailRelay
{
    private string smtpServer;
    private string userName;
    private string password;
    private SmtpClient smtp;
    private int connectionLimit;

    public class threadStatus
    {
        public string msg;
        public bool succeeded;
        public string id;
        public string subject;
    }

    public EmailRelay(string _smtpServer, string _userName, string _password, bool useSSL = true, Int16 port = 0, int _connectionLimit = 6)
    {
        smtpServer                          = _smtpServer;
        userName                            = _userName;
        password                            = _password;
        connectionLimit                     = _connectionLimit;
        NetworkCredential NetworkCred       = new System.Net.NetworkCredential(userName , password);

        if (useSSL)
            { if (port == 0) { port = 587; } }
        else
            { if (port == 0) { port = 25;  } }

        System.Net.ServicePointManager.MaxServicePoints = 100;

        smtp                            = new SmtpClient(smtpServer, port);
        smtp.EnableSsl                  = useSSL;
        smtp.UseDefaultCredentials      = true;
        smtp.Credentials                = NetworkCred;

        // Performance tweaks:  
        if (connectionLimit < 10)
        {
            smtp.ServicePoint.MaxIdleTime = (connectionLimit * 6);
        }
        else if (connectionLimit < 20)
        {
            smtp.ServicePoint.MaxIdleTime = (connectionLimit * 2000);
        }
        else
        {
            smtp.ServicePoint.MaxIdleTime = 60000;
        }
        
        smtp.ServicePoint.ConnectionLimit = connectionLimit;
    }

    public bool ServerCertificateValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return sslPolicyErrors == System.Net.Security.SslPolicyErrors.None;
    }

    public void ConnectionLimit(int value)
    {
        connectionLimit = value;
        smtp.ServicePoint.ConnectionLimit = connectionLimit;
    }

    public bool SendMail(string fromAddress, string toAddress, string subject, 
                         string body, ref string errMsg, string fromEmailName = "")
    {

        MailMessage mm                  = new MailMessage();
        MailAddress emailFrom           = new MailAddress(fromAddress, fromEmailName);

        try
        {
            mm.To.Add(new MailAddress(toAddress));
            mm.From                     = emailFrom;
            mm.Subject                  = subject;
            mm.Body                     = body;
            mm.IsBodyHtml               = true;

            smtp.Send(mm);
            mm.Dispose();
        }
        catch (Exception ex)
        {
            errMsg = ex.Message;
            if (!(ex.InnerException == null)) { errMsg += " (" + ex.InnerException.Message + ")"; }
            return false;
        }

        return true;
    }

    public void Close()
    {
        smtp.Dispose();
    }
}



Used like this:

C#
EmailRelay thisMailer = new EmailRelay("server name", "user@gmail.com", "password", useSslOrNotBool, portNumber);

thisMailer.SendMail("from@whatever.com", "to@whatever.com", "Email subject", "Email Body - use html", ref errMsg, "From friendly name");


Set _connectionLimit higher then 6 in the constructor if you are using multithreding to increase performance while sending multiple mails. The higher _connectionLimit is, the faster you will be able to send mails.

But fair warning: gmail has sending limits, and according to their documentation will accept about 1 mail per second. If you send too many faster then that, they will cut you off for a while.

-Pete
 
Share this answer
 
v4

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