Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
	protected void Page_Load ( object sender, EventArgs e )
	{
	}

	protected void Button1_Click ( object sender, EventArgs e )
	{
	}

	protected void Button1_Click1 ( object sender, EventArgs e )
	{
		int userid = 0;
		string coone = ConfigurationManager.ConnectionStrings[ "hhConnectionString2" ].ConnectionString;
		SqlConnection con = new SqlConnection( coone );
		SqlCommand cmd = new SqlCommand( ( "admin11" ) );
		SqlDataAdapter adp = new SqlDataAdapter( );
		cmd.CommandType = CommandType.StoredProcedure;
		cmd.Parameters.AddWithValue( "@username", TextBox1.Text.Trim( ) );
		cmd.Parameters.AddWithValue( "@password", TextBox2.Text.Trim( ) );
		cmd.Parameters.AddWithValue( "@email", TextBox3.Text.Trim( ) );
		cmd.Connection = con;
		con.Open( );
		userid = Convert.ToInt32( cmd.ExecuteScalar( ) );
		con.Close( );
		string message = string.Empty;
		switch ( userid )
		{
			case -1:
			message = " username already exist";
			break;

			case -2:
			message = " supply mail address already in use";
			break;

			default:
			message = " registration succesful and userid " + userid.ToString( );
			activationclient( userid );
			break;
		}

		ClientScript.RegisterStartupScript( GetType( ), "alert", "alert('" + message + "');", true );
	}

	private void activationclient ( int userid )
	{
		string honey = ConfigurationManager.ConnectionStrings[ "hhConnectionString3" ].ConnectionString;
		string activationcode = Guid.NewGuid( ).ToString( );
		SqlConnection con = new SqlConnection( honey );
		SqlCommand cmd = new SqlCommand( "insert into active values(@userid, @activationcode)" );
		SqlDataAdapter adp = new SqlDataAdapter( );
		cmd.CommandType = CommandType.Text;
		cmd.Parameters.AddWithValue( "@userid", userid );
		cmd.Parameters.AddWithValue( "@activationcode", activationcode );
		cmd.Connection = con;
		con.Open( );
		cmd.ExecuteNonQuery( );
		con.Close( );
		MailMessage mm = new MailMessage( " sender@gmail.com", TextBox3.Text );
		mm.Subject = " account activation";
		string body = "hello" + TextBox1.Text.Trim( ) + ", ";
		body += " <br /><br /> please link on this for activation";
		body += " <br />&lt; a href = '" + Request.Url.AbsoluteUri.Replace( "cs.aspx", "cs_activation.aspx? + activationcode = " + activationcode ) + "' &gt; click here to activate your account .  ";
		mm.Body = body;
		mm.IsBodyHtml = true;
		SmtpClient smtp = new SmtpClient( );
		smtp.Host = "smtp.gmail.com";
		smtp.EnableSsl = true;
		NetworkCredential nn = new NetworkCredential( "sender@gmail.com", "<password>" );
		smtp.UseDefaultCredentials = true;
		smtp.Credentials = nn;
		smtp.Port = 587;
		smtp.Send( mm );
	}
}

mail is not sending kindly help me guys
Posted
Updated 11-Nov-14 20:21pm
v2
Comments
Kornfeld Eliyahu Peter 12-Nov-14 2:22am    
What and where the error?
Member 10874581 12-Nov-14 2:33am    
geting error sending failed
ZurdoDev 12-Nov-14 7:33am    
1. Reply to comment so that the user is notified instead of adding a new comment to your own question.
2. Post the exact error so we don't waste time guessing what the issue is.
Member 10874581 13-Nov-14 0:06am    
sending failedThe SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.this error is coming.
Thomas Nielsen - getCore 12-Nov-14 4:44am    
drop a try-catch around and then catch the callstack of the exception and post it on your thread here, we'll be better positioned to help :)

Hi,

Try below code snippet for sending an authenticated email.

C#
public bool Send(string fromAddress, string fromText, string toAddress, string toText, string bccAddress, string bccText, string subject, StringBuilder plainBody, StringBuilder htmlBody)
{
    try
    {

        string strUname = "";// Username for Authentication
        string strpw = "";// Password for the Authentication
        string sSmtpHost = "";// SMTP Host


        oMail.From = new MailAddress(fromAddress, fromText);

        oMail.To.Add(new MailAddress(toAddress));

        oMail.Bcc.Add(new MailAddress(bccAddress));

        oMail.Subject = subject;
        oMail.Body = htmlBody.ToString();
        oMail.IsBodyHtml = true;

        SmtpClient oSmtp = new SmtpClient(sSmtpHost);
        oSmtp.Credentials = new System.Net.NetworkCredential(strUname, strpw);

        oSmtp.EnableSsl = false;// is Connection is Secure set true else false
        oSmtp.Send(oMail);


        return true;
    }
    catch (Exception Ex)
    {
        throw new Exception("Sending E-Mail failed." + Ex.Message.ToString(), Ex);
    }
}
 
Share this answer
 
 
Share this answer
 
Please check check the following points.
1)Secure Connection- Some SMTP server requires a secure connection so you should check EnableSsl either tue or false and check it once.

2)Strong Password- SMTP servers uses different types of algorithm to check the password is strong or not, even if you provide correct password it will be rejected so make sure your password is strong and don't forgot to use digits+1 symbol+1 caps letter+6 min letter=10 letters, once you make strong password your mail never goes rejected or authenticatio issues.

3)Domain Providers Mail- If you have purchased mail account from the domain providers like godaddy or bigrock that time you should develop a service and host it on same server, by using WebRequest and WebResponse you can use the service in your application. Purchased account from domain providers server will not allow you to send mail from your application even you provide correct credentials, so you should develop service and should host on their server then only you will be able to send the mails.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900