Search code examples
c#smtpgmail

Gmail recipients are not getting emails sent using C#


I had an working email sending functionality using c#. Suddenly gmail recipients are not getting the mails(neither in inbox nor spam). But other recipients (like outlook or any domain) are getting mails in their inbox. Here is my code:

            SmtpClient SmtpMail = new SmtpClient();
            MailMessage myMail = new MailMessage();
            System.Text.Encoding myEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");
            myMail.SubjectEncoding = myEncoding;
            myMail.BodyEncoding = myEncoding;
            myMail.From = (new MailAddress("[email protected]", "User Name"));
            myMail.To.Add("[email protected]");
            myMail.Subject = "Subject";
            myMail.Priority = MailPriority.High;
            SmtpMail.Port = 587;
            myMail.IsBodyHtml = true;
            myMail.Body = "<h1>Hi This is robin</h1>";
            SmtpMail.Host = "mail17.ezhostingserver.com";
            SmtpMail.EnableSsl = true;
            SmtpMail.UseDefaultCredentials = false;
            System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("useremail", "password");
            SmtpMail.Credentials = basicAuthenticationInfo;
            SmtpMail.Send(myMail);

However, I tried to manually send message from their Mail Application to a Gmail user. In that case the recipient is getting the mail. Mails only sent from the code to any gmail user is the main problem here. NB: I have already tried port:26, SmtpMail.UseDefaultCredentials = true. Still no luck


Solution

  • In my case, google was considering my emails as spammy and blocked the emails for reducing the amount of spam sent to Gmail. Please visit https://support.google.com/mail/?p=UnsolicitedMessageError for more information