1

I have attempted to send an email using the GMail SMTP, and followed the guides on various other questions but I still cannot get emails to send from my GMail account.

This is the code I'm using:

protected void emailSend_Click(object sender, EventArgs e)
        {
            var fromAddress = new MailAddress(inputEmail.Text, inputName.Text);
            var toAddress = new MailAddress("spikey666@live.co.uk", "Liane Stevenson");
            const string fromPassword = "*********";
            const string subject = "Web Dev Wolf Message";
            var body = inputMessage.Text;

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential("webdevelopwolf@gmail.com", fromPassword),
                Timeout = 20000
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
            {
                smtp.Send(message);
            }
        }

These are the things I've checked so far:

  1. Turning on less secure apps on GMail
  2. Checked the Gmail Username and Password are correct
  3. Debugged and checked that all text fields have values and are loaded into variables
  4. Check other port numbers suggested by Gmail help
  5. Turned on POP/IMAP functionality on Gmail

Is there anything else I could be missing?

Web Develop Wolf
  • 5,996
  • 12
  • 52
  • 101
  • I use SmtpClient to send emails from a C# app, and while I can't remember why, I needed to set "smtp.UseDefaultCredentials = false;" before sending the message. – Question Marks Apr 15 '15 at 17:37
  • You said it doesn't send. But do you get an exception? Why is `smtp.Send(message);` in its own code block? – mason Apr 15 '15 at 17:38
  • smtp.Send can throw an exception - you are not catching it. And it appears the message variable is outside of your using block. – Mike Weber Apr 15 '15 at 17:38
  • To be totally honest I copy and pasted the code over from an answer to a previous question and I'll confess that I'm not sure how a using statement works so I'm not 100% sure how to fix it if it's wrong lol - also there's no error message it's just not working – Web Develop Wolf Apr 15 '15 at 17:43
  • possible duplicate of [Send e-mail via SMTP using C#](http://stackoverflow.com/questions/9201239/send-e-mail-via-smtp-using-c-sharp) – Question Marks Apr 15 '15 at 17:44
  • 1
    @LianeStevenson Well, clearly you should read up on the [`using`](https://msdn.microsoft.com/en-us/library/yh598w02.aspx) statement. – mason Apr 15 '15 at 17:46
  • So from looking at that web page, does that mean that the smtp.Send(message); should be inside the using statement and not in a block on it's own? – Web Develop Wolf Apr 15 '15 at 17:50
  • 1
    @LianeStevenson Putting the send command in its own block like what you have doesn't do anything different than not having it in the block. The new block changes the scope, but since nothing is declared within that block, you should remove the curly brackets around it. Since smtp.Send() can throw exceptions though, it wouldn't be a bad idea to wrap it in a try/catch block :) – Question Marks Apr 15 '15 at 17:53
  • 1
    @QuestionMarks "smtp.UseDefaultCredentials = false;" worked! If you pop it in as the answer I'll mark it to help others ahving this issue ;) – Web Develop Wolf Apr 15 '15 at 18:03
  • It looks like the send is within the scope of the using. The formatting is just unconventional so it isn't too easy to read. – BDH Apr 15 '15 at 18:23

1 Answers1

1

Before calling SmtpClient.Send(), add:

smtp.UseDefaultCredentials = false;

According to the MSDN SmtpClient page, UseDefaultCredentials is set to false by default, but there seems to be a bug somewhere that is setting it to true. Explicitly set it to false before sending the message and it should be all set.

Question Marks
  • 176
  • 1
  • 9
  • 17