Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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();
 
            try
            {
                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 = "smtp.gmail.com";
 
                //From address will be given as a MailAddress Object
                message.From = fromAddress;
 
                // To address collection of MailAddress
                message.To.Add("shashik@xxxxx.com");
                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("admin1@yoursite.com")
                message.CC.Add("shashik@xxxxx.com");
                //message.CC.Add("admin2@yoursite.com");

                               //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.UseDefaultCredentials=false;
                smtpClient.Credentials = new NetworkCredential("shashixxxx@xxxxx.com", "xxxxxxxxxxxxxx");
 

                // Send SMTP mail
                smtpClient.Send(message);
 
                lblStatus.Text = "Email successfully sent.";
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Response.Write(ex.ToString());
            }
            catch (Exception ex)
            {
                lblStatus.Text = "Send Email Failed.
" + ex.Message;
            }
        }</br>
Posted 18-Oct-11 2:26am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
Add this code to your code
 
smtpclient.EnableSsl = true;
 
 
All the Best
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 @gmail.com etc.
 
[edit]
Changed 589 to 587
[/edit]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

if i use that one
 

 
smtpClient.EnableSsl = true;
 

it say Server doesnot support secure connections
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

client.DeliveryMethod = SmtpDeliveryMethod.Network;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Please refer following url,

Send mail using Google Apps[^]

Hope this may help you...
  Permalink  

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

  Print Answers RSS
0 Marcin Kozub 330
1 OriginalGriff 256
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 12 Mar 2012
Copyright © CodeProject, 1999-2014
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