how to send EMail using SMTP in c#.net

Below is the coding which i am using for sending Mail...

using System.Net.Mail;

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mailmsg = new MailMessage();
                mailmsg.To.Add("to email address");// eg: sss@gmail.com
                mailmsg.From=new MailAddress("from email address");// eg: aaa@gmail.com
                mailmsg.Subject="Test Mail";
                mailmsg.Body="hai";                            
                SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",587);  
                smtpclient.EnableSsl = true;
                smtpclient.Send(mailmsg);
                MessageBox.Show("Mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {

            }

when i execute they show a Authentication error which is asking for password... Where i give the password or pls give any other suggestion or give any other sample code

Thanking you,
giit_shivaAsked:
Who is Participating?
 
tlaytonConnect With a Mentor Commented:
Try this:

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {

            MailMessage mailmsg = new MailMessage("from email address", "to email address");
            mailmsg.Subject = "Test Mail";
            mailmsg.Body = "hai";

            SmtpClient smtpclient = new SmtpClient("smtp.gmail.com", 587);
            System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("user name", "password");
            smtpclient.UseDefaultCredentials = false;
            smtpclient.Credentials = basicAuthenticationInfo;
            smtpclient.EnableSsl = true;
            smtpclient.Send(mailmsg);
            MessageBox.Show("Mail Send");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
        finally
        {

        }
    }

Open in new window

0
 
mvdeveloperCommented:
smtpclient.Credentials = new NetworkCredential( someUser, somePass)

before you send.
0
 
Minh Võ CôngConnect With a Mentor Commented:
you must send your email password before send the email

SmtpClient smtp = new SmtpClient
    {
        Host = "smtp.gmail.com",
        Port = 587,
        UseDefaultCredentials = false,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        Credentials = new NetworkCredential("myid@gmail.com", "mypassword"),
        EnableSsl = true,
        Timeout = 10000
    };

    MailMessage message = new MailMessage();
    message.Body = "hello there";
    message.Subject = "hi!!";
    message.To.Add("myid@gmail.com");
    message.From = new MailAddress("myid@gmail.com");
    smtp.Send(message);
http://stackoverflow.com/questions/9104645/cant-auth-to-gmail-smtp-via-mailmessage-smtpclient
http://www.codeproject.com/Articles/20546/How-to-Send-Mails-from-your-GMAIL-Account-through
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.