Share

Generally if you write the code for sendmail in asp.net using gmail account. It may not work on online. That time you have to Unlocking Google’s Gmail account.

Mail Coding:


//-------------------------Start Code----------------------------------------
public bool sendMail(string eidTo, string msgsub, string msgbody) {

	MailMessage greetings = new MailMessage();
	SmtpClient smtp = new SmtpClient();
	try {
	
		greetings.From = new MailAddress("xyz@gmail.com", "demo");
		greetings.To.Add(eidTo);
		greetings.IsBodyHtml = true;
		greetings.Priority = MailPriority.High;
		greetings.Body = msgbody;
		greetings.Subject = msgsub;
		
		smtp.Host = "smtp.gmail.com";
		smtp.Port = 587;
		smtp.EnableSsl = true;
		smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
		smtp.UseDefaultCredentials = false;
		smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "xyzpassword");
		smtp.Send(greetings);
		return true;
		
	}
	catch (Exception) { throw; }

}
//-------------------------End code------------------------------

Google may prevent an application from accessing your account
Here you can enable applications to access google account with your credentials:

https://accounts.google.com/DisplayUnlockCaptcha
(Login gmail account & click the above link)
Set UseDefaultCredentials to false, since you define credentials for the connection.

Set this property to true when this SmtpClient object should, if requested by the server, authenticate using the default credentials of the currently logged on user.

If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server.
Happy coding………………….


Share