3

I'm getting the above error. I've looked at online solutions and I seem to have done all that is necessary but I'm still getting it. I've included Enablessl = true; Delivery method is Network and I've supplied the credentials. I debugged it and the username and password seem to be correct.

using System;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;

namespace SendMail
{
    public partial class MainWindow : Form
    {
        #region Private variables
        private MailMessage _message = new MailMessage();
        private EmailSender sender = EmailSender.GetInstance();
        private SmtpClient _smtpClient = new SmtpClient();
        #endregion

        public MainWindow()
        {
            InitializeComponent();
        }

        private void PrepareMailMessage()
        {
            // Set the FROM address
            _message.From = new MailAddress(tbFromAddr.ToString().Trim());

            // Set the TO address
            _message.To.Add(new MailAddress(tbToAddr.ToString().Trim()));

            // Set the SUBJECT
            _message.Subject = tbSubject.ToString();

            _message.IsBodyHtml = false;
            _message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            _message.Priority = MailPriority.High;
            _message.SubjectEncoding = System.Text.Encoding.UTF8;
            _message.BodyEncoding = System.Text.Encoding.UTF8;
        }

        private void PrepareServer()
        {
            _smtpClient.Host = "smtp.gmail.com";
            _smtpClient.Port = 587;
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            _smtpClient.UseDefaultCredentials = false;
            _smtpClient.Credentials = new NetworkCredential(tbFromAddr.ToString().Trim(), tbPassword.ToString().Trim());
            _smtpClient.EnableSsl = true;
        }

        private void PrepareSender()
        {
            sender.Message = _message;
            sender.smtpClient = _smtpClient;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            PrepareMailMessage();
            PrepareServer();
            PrepareSender();
            //this.sender.Send();
            _smtpClient.Send(_message);
        }
    }
}
poupou
  • 43,413
  • 6
  • 77
  • 174
s5s
  • 11,159
  • 21
  • 74
  • 121

1 Answers1

3

I checked your code and it worked for me. However, I am unsure about the purpose of the method PrepareSender(). I included message body(_message.Body) on PrepareMailMessage().

Did you tried using the default credentials?

_smtpClient.UseDefaultCredentials = true
NaveenBhat
  • 3,248
  • 4
  • 35
  • 48
  • Also this might be related to Google Mail, see http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail – jCoder Nov 05 '11 at 20:01
  • Yes, I tried using the default credentials too. PrepareSender() is not used. I mean it's used but it does nothing currently. Hmm, strange - as you said it **should** work but it doesn't. Before I posted on stackoverflow I made sure to research it and I thought I've covered all problems. It might be a problem with Gmail after all. – s5s Nov 07 '11 at 13:30
  • hmmm...strange! did you tried by disabling the firewall or antivirus? – NaveenBhat Nov 07 '11 at 13:52