0
try
{
    MailMessage mail = new MailMessage();

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

    mail.From = new MailAddress("senttoemailaddress@gmail.com");
    mail.To.Add("senttoemailaddress@gmail.com");
    mail.Subject = "Test Mail";
    mail.Body = "This is for testing SMTP mail from GMAIL";

    SmtpServer.Port = 587;

    SmtpServer.Credentials = new System.Net.NetworkCredential("emailaddress.test@gmail.com", "passW0ord");

    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);

    MessageBox.Show("mail Send");
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

I'm trying to run the above code to automatically send emails, the code executes up until it reaches smtpServer.send(mail) then it just stops, the email address that I use is valid and the password is valid.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
Armee
  • 11
  • 5
  • 2
    "...the code executes up until it reaches smtpServer.send(mail) then it just stops...". No exception is thrown? – Jose Alonso Monge Aug 25 '17 at 09:41
  • Have you getting exception? The message box should pop up with exception you've get. – Tetsuya Yamamoto Aug 25 '17 at 09:41
  • Google may block sign in attempts from some apps or devices when you try to login from some app. Check this answer for more details and solution:https://stackoverflow.com/a/32475872/2946329 – Salah Akbari Aug 25 '17 at 09:41
  • @JoseAlonsoMonge No exception is thrown. it doesn't go further than the smtpServer.send(mail) – Armee Aug 25 '17 at 09:42
  • @S.Akbari I have already done that, it still doesn't work – Armee Aug 25 '17 at 09:44
  • "it doesn't go further than the smtpServer.send(mail)". So what, when you step through it in Visual Studio, happens after this line? The application just exits? It should have some sort of error or exit code, or exception or _something_. It won't just stop with no output of any kind. – ADyson Aug 25 '17 at 09:48
  • Have you tried putting a breakpoint on the line *SmtpServer.Send(mail);* then stepping through it? – Master Yoda Aug 25 '17 at 09:52
  • @ADyson it stops without any output for 2 minutes then throws a time out exception. – Armee Aug 25 '17 at 14:46
  • ok thanks. However, that's not the same as "just stops", as you previously stated, is it? Please try to describe things accurately, it will help people to help you. Precision is an absolutely essential skill for a programmer to have. Small details can be vital. In this case, it's a comparatively large detail, so omitting it is not a brilliant idea. Anyway, a timeout potentially indicates some sort of networking issue. Was there any more detailed message than simply "timed out"? or maybe an inner exception? Again, detail could potentially be significant. We need as many clues as possible. – ADyson Aug 25 '17 at 14:49
  • Is there any particular software i need too download to route the smtp server to? Like i saw online that you need to use microsoft IIS, if anyone can help me with the configuration. – Armee Aug 30 '17 at 14:59

3 Answers3

0

Try with SMTP port: 465

Notice : Google limits the amount of mail a user can send, via its portable SMTP server. This limit restricts the number of messages sent per day to 99 emails; and the restriction is automatically removed within 24 hours after the limit was reached.

S. Matsepura
  • 1,673
  • 1
  • 17
  • 24
  • But wouldn't it still return a response that the user would see in their console? OP should still see a response from the server. They claim to receive nothing. – Master Yoda Aug 25 '17 at 09:50
  • after 82 emails i've get an error: 4.7.0 Try again later, closing connection. (MAIL) 3sm979841ljv.70 - gsmtp – Timur Lemeshko Aug 25 '17 at 10:03
0

you need to do some configuration in the gmail account before you can send email via gmail

  1. activate 2 steps authentication

  2. enter this link and allow your email to be as SMTP server

i think this will solve the issue .

moath naji
  • 663
  • 1
  • 4
  • 20
0

U have problem with Login\password. I've try your code and get an error:

internal class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                var mail = new MailMessage();

                var SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("senttoemailaddress@gmail.com");
                mail.To.Add("senttoemailaddress@gmail.com");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";

                SmtpServer.Port = 587;

                SmtpServer.Credentials = new NetworkCredential("emailaddress.test@gmail.com", "passW0ord");

                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);



                Console.Write("mail Send");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

...5.5.1 Authentication Required. Learn more at...

Then i just change login\password to my and it works:

mail Send

Timur Lemeshko
  • 2,747
  • 5
  • 27
  • 39