?
Solved

how to send EMail using SMTP in c#.net

Posted on 2012-09-20
3
Medium Priority
?
1,097 Views
Last Modified: 2012-09-24
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,
0
Comment
Question by:giit_shiva
3 Comments
 
LVL 5

Expert Comment

by:mvdeveloper
ID: 38416841
smtpclient.Credentials = new NetworkCredential( someUser, somePass)

before you send.
0
 
LVL 5

Accepted Solution

by:
tlayton earned 375 total points
ID: 38416866
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
 
LVL 15

Assisted Solution

by:Minh Võ Công
Minh Võ Công earned 375 total points
ID: 38416873
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question