[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How to send email in C# from asp.net. The code is not workinfg

Posted on 2010-04-02
4
Medium Priority
?
569 Views
Last Modified: 2012-05-09
//Error - System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

using System;
using System.Configuration;
using System.Data;
using System.Web.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        int port = 587;
        System.Net.Mail.MailMessage objMail = new System.Net.Mail.MailMessage();
        System.Net.Mail.SmtpClient objSmtp = new System.Net.Mail.SmtpClient("smtp.gmail.com",port);


        try
        {
           
            objMail.To.Add(txtTo.Text);
            objMail.From = new System.Net.Mail.MailAddress("abc@gmail.com");
            //This code of line is used for specifying the subject line.
            objMail.Subject = txtSubject.Text;
            objMail.IsBodyHtml = true;
            objMail.Body = txtContent.Value;


            objSmtp.UseDefaultCredentials = false;
            objSmtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "******");
            objSmtp.EnableSsl = true;

            objSmtp.Send(objMail);
            Response.Write("<script>alert('Email sent');</script>");
        }
        catch (Exception ess)
        {
        }

           
 
    }
}
0
Comment
Question by:searchsanjaysharma
4 Comments
 
LVL 3

Expert Comment

by:the_bill
ID: 29489535
hi,

If you've tried sending multiple times, google might have placed a temp block, which puts a "word verification" (captcha). Sign in via web interface, check your credentials and fill in captcha. Has that helped?
0
 
LVL 5

Expert Comment

by:pschrama
ID: 29497083
According to http://mail.google.com/support/bin/answer.py?hl=en&answer=13287, the port for SSL is 465, try that instead.
0
 
LVL 13

Accepted Solution

by:
Ashok earned 2000 total points
ID: 29564413
using System.Net.Mail;

protected void btnSend_Click(object sender, EventArgs e)
{
  MailMessage mail = new MailMessage();
  mail.To.Add("SanjaySharma@gmail.com");
  mail.To.Add("Ashok122@yahoo.com");
  mail.From = new MailAddress("abc@gmail.com");
  mail.Subject = "Email using Gmail";

  string Body = "Hi, this mail is to test sending mail"+
                "using Gmail in ASP.NET";
  mail.Body = Body;

  mail.IsBodyHtml = true;
  SmtpClient smtp = new SmtpClient();
  smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
  smtp.Port = 587;
  smtp.Credentials = new System.Net.NetworkCredential
       ("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
  smtp.EnableSsl = true;
  smtp.Send(mail);
}

If this does not help, see http://csharpdotnetfreak.blogspot.com/2009/08/send-email-using-gmail-in-aspnet.html

HTH
Ashok
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 31710390
Thanks excellent
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

612 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