0

I am trying to send email through asp.net C# code. but this code is not sending email and generating error which is given in double quotes below

"System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'smtp.gmail.com' at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at emailtest._Default.Button1_Click(Object sender, EventArgs e)"

the given below lines are the code which i call on button click

    protected void SendMail()
    {
        // Gmail Address from where you send the mail
        var fromAddress = "myemailaddress@gmail.com";
        // any address where the email will be sending
        var toAddress = YourEmail.Text.ToString();
        //Password of your gmail address
        const string fromPassword = "myEmailAddressPassword";
        // Passing the values and make a email formate to display
        string subject = YourSubject.Text.ToString();
        string body = "From: " + YourName.Text + "\n";
        body += "Email: " + YourEmail.Text + "\n";
        body += "Subject: " + YourSubject.Text + "\n";
        body += "Question: \n" + Comments.Text + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            //here on button click what will done 
            SendMail();
            DisplayMessage.Text = "Your Comments after sending the mail";
            DisplayMessage.Visible = true;
            YourSubject.Text = "";
            YourEmail.Text = "";
            YourName.Text = "";
            Comments.Text = "";
        }
        catch (Exception) { }
    }

please help i am stuck.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Mr. Waqas
  • 35
  • 2
  • 10
  • Cannot resolve `smtp.gmail.com`? Can you double verify by ping / nslookup ? – Raptor Apr 17 '15 at 06:52
  • 1
    possible duplicate of [C# Winforms Application Failure in sending Email: The remote name could not be resolved: 'smtp.gmail.com ; Operation Timed out](http://stackoverflow.com/questions/15208097/c-sharp-winforms-application-failure-in-sending-email-the-remote-name-could-not) – Rajeesh Menoth Apr 17 '15 at 06:52
  • Ping to it. If its not working, might be a DNS issue. Also confirm that a firewall is not blocking this. Lastly, confirm if the credentials used are valid and correct. – Dhrumil Apr 17 '15 at 06:54
  • how can i ping to it.? – Mr. Waqas Apr 17 '15 at 06:57
  • i am working on a client machine in network. – Mr. Waqas Apr 17 '15 at 06:58

2 Answers2

0

Here is my code:

// Send an email
            MailMessage EmailMsg = new MailMessage();
            EmailMsg.From = new MailAddress("youremail@outlook.com", "bc photo art lettering");
            EmailMsg.To.Add(new MailAddress("tothisemail@hotmail.com"));
            EmailMsg.Subject = "someone made an order";
            EmailMsg.Body = "<html><body><div style='border-style:solid;border-width:5px;border-radius: 10px; padding-left: 10px;margin: 20px; font-size: 18px;'> <p style='font-family: Vladimir Script;font-weight: bold; color: #f7d722;font-size: 48px;'> blah blah blah</p><hr><div width=40%;> <p  style='font-size: 20px;'>"Hello" +
                "</div></body></html>";
            EmailMsg.IsBodyHtml = true;
            EmailMsg.Priority = MailPriority.Normal;
            SmtpClient MailClient = new SmtpClient("smtp.live.com", 587);
            MailClient.EnableSsl = true;
            MailClient.Credentials = new System.Net.NetworkCredential("yourmail@outlook.com", "yourpassword");
            MailClient.Send(EmailMsg);
Salah Salah
  • 65
  • 1
  • 10
0
    var receiver = new MailAddress("foo@bar.com");
    var sender = new MailAddress("bar@foo.com"); 
    var mail = new MailMessage(receiver , sender ); 
    mail.Subject = "Subject";
    mail.Body = "Body";

    var smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587; //Should be working
    smtp.Credentials = new NetworkCredential("username@gmail.com", password");
    smtp.EnableSsl = true;
    smtp.Send(mail);
Mirko Sbr
  • 92
  • 8