2

I was trying to send email using gmail account on asp.net c#, but I keep getting this 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 [2607:f8b0:400c:c01::6c]:587

I have tried different codes from different resources,but none of them worked with my project More over I've tried those steps but still not working:

  1. trying ports 25, 465, 587 on both VS 2012 server and localhost (iis 7).
  2. Firewall set to off , antivirus set to off.
  3. starting visual studio as administrator.

Please , can anyone tell me what is the reason behind this error. and if there is possibility to change something , where should I start..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var myMailMessage = new System.Net.Mail.MailMessage();
        myMailMessage.From = new System.Net.Mail.MailAddress("serveremail@gmail.com");
        myMailMessage.To.Add("youremail@yahoo.com");// Mail would be sent to this address
        myMailMessage.Subject = "Feedback Form";
        myMailMessage.Body = "Hello";

        var smtpServer = new System.Net.Mail.SmtpClient("smtp.gmail.com");
        smtpServer.Port =587;
        smtpServer.Credentials = new System.Net.NetworkCredential("serveremail@gmail.com", "**YOURPASSWORD**");
        smtpServer.EnableSsl = true;
        smtpServer.Send(MyMailMessage);

        Response.Redirect("Default.aspx");
    }
}
AnFi
  • 10,493
  • 3
  • 23
  • 47
reaz
  • 735
  • 1
  • 10
  • 20

2 Answers2

2

Try this one,
Add namespace

system.net; system.net.mail;

 protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage MyMailMessage = new MailMessage();

        MyMailMessage.From = new MailAddress("from");

        MyMailMessage.To.Add("to");

        MyMailMessage.Subject = "Feedback Form";

        MyMailMessage.Body = "This is the test message from xx for testing mail send";

        MyMailMessage.IsBodyHtml = true;

        SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");

        SMTPServer.Port = 587;

        SMTPServer.Credentials = new System.Net.NetworkCredential("username","password");

        SMTPServer.EnableSsl = true;

        try
        {

            SMTPServer.Send(MyMailMessage);

           //Response.Redirect("Thankyou.aspx");

        }

        catch (Exception ex)
        {



        }
Duk
  • 905
  • 5
  • 15
  • 34
0

you can try this :

 MailMessage mail = new MailMessage();
 mail.Subject = "Your Subject";
 mail.From = new MailAddress("senderMailAddress");
 mail.To.Add("ReceiverMailAddress");
 mail.Body = "Hello! your mail content goes here...";
 mail.IsBodyHtml = true;

 SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
 smtp.EnableSsl = true;
 NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
 smtp.Credentials = netCre;

 try
  {
   smtp.Send(mail);                
  }
  catch (Exception ex)
  {        
  }
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • Thanks for your reply. but I've tried different ways and none of them worked. May be there is a problem with my Internet service provider because recently i found out that gmail login doesn't work even on out look. – reaz Mar 22 '13 at 07:11
  • are you able to connect gmail with the help of browser ? – Mohammad Arshad Alam Mar 22 '13 at 07:13
  • yes i am able to login with browser. but the problem is with using applications and ports ( pop ... etc) – reaz Mar 22 '13 at 07:14
  • Yes I have. and nothing happened :( – reaz Mar 22 '13 at 07:19
  • Tomorrow I am gonna check it on different internet service provider and will see how it goes. – reaz Mar 22 '13 at 07:19
  • since you put exception . the error didn't appear. but i think yea it's the same error – reaz Mar 22 '13 at 07:20
  • yea it's the same error ( A socket operation was attempted to an unreachable network [2607:f8b0:400c:c03::6d]:587 ) – reaz Mar 22 '13 at 07:21
  • problem is with your ip address – Mohammad Arshad Alam Mar 22 '13 at 07:26
  • http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/thread/8ed23110-3612-419c-85fa-0db5a2b3e095/ – Mohammad Arshad Alam Mar 22 '13 at 07:28
  • after I read this from that link ( It's either Firewall or another software product blocking it. We have McAffee AV on our work computers which blocks SMTP and thats the exact message it gives. ) I can tell... the problem is caused by Mcafee Antivirus on the server...because I am using a Local Server ( work server which has Mcafee antivirus) ... – reaz Mar 22 '13 at 07:36
  • as you said you are not able to connect with browser also ? – Mohammad Arshad Alam Mar 22 '13 at 07:40
  • can you try with port `2525` – Mohammad Arshad Alam Mar 22 '13 at 07:41
  • I am able to login to my gmail account using browser. but when I use asp.net to send an email I get that error. And I have tried many ways it didn't work. but the code worked on my friends computer ( He is using a different ISP ) mine is University Server. – reaz Mar 22 '13 at 07:41
  • A socket operation was attempted to an unreachable network [2607:f8b0:400c:c03::6d]:2525 ... the same error – reaz Mar 22 '13 at 07:43
  • have you closed firewall and antivirus – Mohammad Arshad Alam Mar 22 '13 at 07:44
  • http://stackoverflow.com/questions/11717393/a-socket-operation-was-attempted-to-an-unreachable-network-in-python-httplib – Mohammad Arshad Alam Mar 22 '13 at 07:46
  • btw, on telnet also didn't work the port and smtp were not reachable – reaz Mar 22 '13 at 07:49
  • That's why I am thinking the problem is related to my network... I have to try a different network which has a public IP – reaz Mar 22 '13 at 07:50
  • Yes, That's true... The only think i can do is to wait till tomorrow and trying to connect to a different network... Anyway I really appreciate your replys... – reaz Mar 22 '13 at 07:58