Click here to Skip to main content
16,004,160 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use following code for sending mail but it throws error

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. s1sm2963114iba.7"

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

namespace email
{
    public partial class Form1 : Form
    {
// creating the objects of smtpclient class and mailmessage class

//smtpclient class is used to setup a smtp server client i am using gmail.

        SmtpClient client = new SmtpClient();
        MailMessage msg = new MailMessage();
        System.Net.NetworkCredential credentials = new 
System.Net.NetworkCredential("your_email@gmail.com", "Your Password");
     
  public Form1()
        {
            InitializeComponent();
        }
// we create a method name sendemail when we want to send email we just pass arguments 

        public void sendemail(string sendto ,string sendfrom, string subject, string body)
        {
            try
            {
// setting up smtp client, port other details

                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;
// you can skip this line it is for html emails if you want to send html emails then set msg.isbodyhtml = true
                msg.IsBodyHtml = true;
                // convert strings to mail address
                MailAddress to = new MailAddress(sendto);
                MailAddress from = new MailAddress(sendfrom);
                msg.Subject = subject;
                msg.Body = body;
                msg.To.Add(to);
                msg.From = from;
                client.Send(msg);
                MessageBox.Show("Email Sent");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "Error");
            }
        }
// on button click event we called sendemail method
        private void btnsendmail_Click(object sender, EventArgs e)
        {
            sendemail(txtto.Text.ToString(), "nishantbcaproject@gmail.com", txtsubject.Text.ToString(), txtbody.Text.ToString());
        }
Posted
Updated 28-Mar-11 4:08am
v2
Comments
HimanshuJoshi 28-Mar-11 10:08am    
Edited to add pre block.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900