Send an Email

  • 284 Views
  • Last Post 10 October 2018
Suriati Marpaung posted this 10 October 2018

Hello everyone, I'm a newbie both in C Sharp and in this group discussion. Sorry if I make mistake with my words and grammar (since I'm an Indonesian and not good enough in English)

I am now try to Send an email but I got an error. Please help me.

This is my code :

private void btnSend_Click(object sender, EventArgs e)

        {

            login = new NetworkCredential(txtUsername.Text, txtPassword.Text);

            client = new SmtpClient(txtSmtp.Text);

            client.Port = Convert.ToInt32(txtPort.Text);

            client.EnableSsl = chkSSL.Checked;

            client.Credentials = login;

            msg = new MailMessage { From = new MailAddress(txtUsername.Text + txtSmtp.Text.Replace("smtp.", "@"), "Suriati Marpaung", Encoding.UTF8) };

            msg.To.Add(new MailAddress(txtTo.Text));

            if (!string.IsNullOrEmpty(txtCC.Text))

                msg.To.Add(new MailAddress(txtCC.Text));

            msg.Subject = txtSubject.Text;

            msg.Body = txtMessage.Text;

            msg.BodyEncoding = Encoding.UTF8;

            msg.IsBodyHtml = true;

            msg.Priority = MailPriority.Normal;

            msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallBack);

            string userstate = "Sending..";

            client.SendAsync(msg, userstate);

        }

And the message error is :

the smtp server requires a secure connection or the client was not authenticated.....

Thank you so much.

jack posted this 10 October 2018

You should create an email, then enter your account information as below

For example i'm using gmail

login = new NetworkCredential("[email protected]", "123abc");
client = new SmtpClient("smtp.gmail.com");
client.Port = 465;
client.EnableSsl = true;
client.Credentials = login;

I hope so you can solve your problem !

Close