1

I published the site I completed using ASP.NET Web Forms but I'm having trouble sending mail. My web site does not send mail. There is no problem when I run in local.

My Fonksiyon.cs:

public static bool MailGonder(string gonderenaciklama, string kimemail, string kimeadi, string mailkonu, string mailicerik, string kimdenmail = "", bool IletisimFormuMu = false)
{
    MailAddress From = new MailAddress(IletisimFormuMu ? kimdenmail : "My e-mail address is here", gonderenaciklama); // Gönderen kısmında görünen e-posta adresi.
    MailAddress To = new MailAddress(kimemail, kimeadi); // Mailin gönderileceği adres.
    MailMessage EMail = new MailMessage(From, To);

    EMail.Subject = mailkonu;
    EMail.Body = mailicerik;
    EMail.IsBodyHtml = true;
    EMail.BodyEncoding = Encoding.Unicode;
    SmtpClient MailClient = new SmtpClient();
    MailClient.Port = 587;
    MailClient.Host = "smtp.gmail.com";

    MailClient.EnableSsl = true; // Gmail üzerinden gönderme yapılacaksa veya sunucu kimlik doğrulaması gerektiriyorsa buraya true değerini vereceğiz.
    MailClient.UseDefaultCredentials = true;
    MailClient.Credentials = new System.Net.NetworkCredential("My e-mail address is here", "My password is here"); // Maili göndereceğimiz hesap bilgileri buraya giriyoruz. Mailimiz bu hesap üzerinden gönderilecek.

    MailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

    try
    {
        MailClient.Send(EMail);
        return true;
    }
    catch
    {
        return false;
    }
}

My register.aspx button click:

string guid = Guid.NewGuid().ToString();
Fonksiyon.MailGonder("Ay Tasarım E-Posta Doğrulaması", TxtEPosta.Text, TxtAd.Text + " " + TxtSoyad.Text, "E-Posta Doğrulaması", "Lütfen aşağıdaki aktivasyon kodunu sitemizdeki ilgili alana yazarak üyeliğinizi aktif ediniz!<br />Aktivasyon Kodu: " + guid + "");
Shadouspan
  • 253
  • 2
  • 14
  • 2
    You try to send the mail. If there's an exception, you catch it and return false. So how you expect to find out what the error is if all you do is return false? Log the error! [Serilog](https://serilog.net/), [Nlog](https://github.com/NLog/NLog), [Elmah](https://github.com/NLog/NLog), [SEQ](https://getseq.net/) etc. – mason Sep 30 '17 at 21:40
  • Possible duplicate of [Sending email in .NET through Gmail](https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – mason Sep 30 '17 at 21:40

1 Answers1

1

Nothing to do with your code, this is security feature of your gmail account. these are the reasons that you can check

  • Google's security system has blocked the IP of your server

Google security system is actually pretty cool, if somebody gets his hands on your Gmail's password, well he won't be able to do much, unless he is using your IP address. Why? Because when Google spots an unusual IP address trying to connect to your account it will deny it access and will send you an email and eventually a text message on your mobile phone.

When you send a test email from MailPoet's Settings and you get the following message : " SMTP Error: Could not authenticate. | SMTP Error: Could not connect to SMTP host." then you might be entering this case scenario

The email you will receive to notify you of that unusual access will be as follow :

enter image description here enter image description here

Allow new IP's in Google account

In your case when you setup your site to send with your Gmail account, you want to allow a new IP to use your Gmail's credentials. In order to allow a new unrecognized app simply go to https://security.google.com/settings/security/activity, find the line that concerns you and allow access. enter image description here

Hope this helps to resolve your problem....