0
private void MailGonder(string to, string from, string password, string subject, string body, HttpPostedFile ektekidosya)
{
    using (MailMessage mm = new MailMessage(from, to))
    {
        mm.Subject = subject;

        mm.Body = body;

        mm.IsBodyHtml = false;

        SmtpClient smtp = new SmtpClient();

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

        smtp.EnableSsl = true;

        NetworkCredential NetworkCred = new NetworkCredential(from, password);

        smtp.UseDefaultCredentials = false;

        smtp.Credentials = NetworkCred;

        smtp.Port = 587;

        smtp.Send(mm);
    }
}
    protected void BtnGonder_Click(object sender, EventArgs e)
    {
        string to = txtKime.Text;

        string from = txtEmail.Text;

        string password = txtPassword.Text;

        string subject = txtKonu.Text;

        string body = txtMesaj.Text;

        HttpPostedFile ektekidosya = fuAttach.PostedFile;

        try
        {
            MailGonder(to, from, password, subject, body, ektekidosya);
            Response.Write("Mail başarıyla gönderildi");
        }
        catch (Exception ex)
        {
            Response.Write("Mail gönderiminde hata oluştu. Err: " + ex.ToString());
        }
    }

I tried different ports but nothing helped, I keep getting the error.Localserver work fine but Server didnt work. How can i fix this error? I tried everything.. My Hosting is 1und1.

Here Picture from my Website Error

canberkcelik
  • 35
  • 1
  • 9

2 Answers2

1

Try putting the "smtp.UseDefaultCredentials = true;" expression BEFORE the NetworkCredential declaration:

smtp.UseDefaultCredentials = false;
NetworkCredential NetworkCred = new NetworkCredential(from, password);

The order is important as setting SmtpClient.UseDefaultCredentials = false will reset SmtpClient.Credentials to null.

UPDATE:

1) Check first with telnet:

Telnet smtp.gmail.com 587 If you see an empty command prompt window, then the connection was successful

2) Make sure the from address is the same gmail account as that associated with the credentials you're passing

3) If your password is weak, gmail may not send your mails!

4) If this doesn't work, use another mail server

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • 1
    You need to do this anyway. Have you tried all the links suggested by VDWWD? What is the detailed error message - remove the try/catch to get this? Have you tested using telnet? – IrishChieftain Feb 25 '18 at 20:03
  • "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.206.108:587".. i am getting this error. – canberkcelik Feb 25 '18 at 21:37
  • 1
    Have you checked to see if it a DNS issue? You give no details about your own server set-up: https://forums.asp.net/t/1211112.aspx?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 Also, why are you passing a file as a parameter when you are not even attaching it to the email? – IrishChieftain Feb 25 '18 at 21:45
  • how can i check my DNS issue? – canberkcelik Feb 25 '18 at 21:52
  • Ask your admin if it's on an internal network. Try Google! I have updated the answer with some options. – IrishChieftain Feb 25 '18 at 21:55
0

Using gmail make sure allow access for less secure app

Check here Let less secure apps use your account

Zahid Tanveer
  • 60
  • 1
  • 11