Click here to Skip to main content
11,496,146 members (819 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: ASP.NET C#4.0 SMTP
 protected void btnSendmail_Click(object sender, EventArgs e)
            // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
            // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();
                MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
                // You can specify the host name or ipaddress of your server
                // Default in IIS will be localhost 
                smtpClient.Host = "localhost";
                //Default port will be 25
                smtpClient.Port = 25;
                smtpClient.Host = "";
                //From address will be given as a MailAddress Object
                message.From = fromAddress;
                // To address collection of MailAddress
                message.Subject = "Feedback";
                // CC and BCC optional
                // MailAddressCollection class is used to send the email to various users
                // You can specify Address as new MailAddress("")

                               //Body can be Html or text format
                //Specify true if it  is html message
                message.IsBodyHtml = false;
                // Message body content
                message.Body = txtMessage.Text;

                smtpClient.Credentials = new NetworkCredential("", "xxxxxxxxxxxxxx");

                // Send SMTP mail
                lblStatus.Text = "Email successfully sent.";
            catch (System.Net.Mail.SmtpException ex)
            catch (Exception ex)
                lblStatus.Text = "Send Email Failed.
" + ex.Message;
Posted 18-Oct-11 2:26am
Add this code to your code

smtpclient.EnableSsl = true;

All the Best
Sending emails to Google's server require you to be on port 587 using EnableSSL=true and to be authenticated first on Google ie with an account ending in etc.

Changed 589 to 587
if i use that one

smtpClient.EnableSsl = true;

it say Server doesnot support secure connections
client.DeliveryMethod = SmtpDeliveryMethod.Network;
For Gmail Account SMTP, disable 2 step verification and send email.

Also refer this from codeproject article

Sending an Email in C# with or without attachments: generic routine.[^]
Please refer following url,

Send mail using Google Apps[^]

Hope this may help you...
you might want to set EnableSsl to false.

smtp.EnableSsl = false;
Tomas Takac at 24-Mar-15 3:50am
Do you realize this question is more than 3 years old?
upendra shahi at 24-Mar-15 4:25am
That doesn't matter because someone search in now days also
F-ES Sitecore at 24-Mar-15 5:22am
If people actually searched for answers to their problems they wouldn't be here in the first place :)

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

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 RyanDev 230
2 CHill60 163
3 Sascha Lefèvre 145
4 Sergey Alexandrovich Kryukov 124
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 24 Mar 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100