I have a problem trying to send email (gmail) it won't allow me to send email if I didn't have the 'allow less secure app' turned on. What am I doin wrong?
try
{
smtpClient.Host = mServer;
smtpClient.Port = mPort;
smtpClient.EnableSsl = isenableSsl;
//Input new time out
if (mTimeout > 0)
{
smtpClient.Timeout = mTimeout;
}
//Check Authentication Mode
if (isAuthentication)
{
//Create Network Credentail if SMTP Server Turn On Authentication Mode
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = mUserName;
credentials.Password = mPassword;
smtpClient.Credentials = credentials;
smtpClient.UseDefaultCredentials = false;
}
else
{
smtpClient.UseDefaultCredentials = true;
}
//Configuration Mail Information
if (string.IsNullOrEmpty(mDisplay)) mailMessage.From = new MailAddress(mFrom);
else mailMessage.From = new MailAddress(mFrom, mDisplay);
mailMessage.Sender = new MailAddress(mFrom);
mailMessage.ReplyTo = new MailAddress(mFrom);
//Set To Email Information
if (ToEmails.Count != 0)
{
mailMessage.To.Clear();
foreach (string mail in ToEmails)
{
mailMessage.To.Add(mail);
}
}
//Set Cc Email Information
mailMessage.CC.Clear();
foreach (string mail in CcEmails)
{
mailMessage.CC.Add(mail);
}
//Set Bcc Email Information
mailMessage.Bcc.Clear();
foreach (string mail in BccEmails)
{
mailMessage.Bcc.Add(mail);
}
//Set Mail Information
mailMessage.Subject = mSubject;
mailMessage.Body = mBody;
//Configuration Mail Option
mailMessage.IsBodyHtml = isBodyHtml;
mailMessage.SubjectEncoding = mSubjectEncoding;
mailMessage.BodyEncoding = mBodyEncoding;
mailMessage.Priority = mPriority;
mailMessage.DeliveryNotificationOptions = mDeliveryNotificationOptions;
//Clear Attachment File
mailMessage.Attachments.Clear();
//Add Attachments Internal File
AddAttachImage(ref mailMessage);
//Add Attachments External File
if (attachmentFiles.Count > 0)
{
AddAttachFile(ref mailMessage);
}
//Link Event Handler
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
//Send Message Fuction
SetLogfileSendEmail(smtpClient, mailMessage);
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
strInformation.Append("(Error) Method smtpClient_SendCompleted : " + ex.Message);
WriteLogFile();
throw new Exception("SMTP Exception: " + ex.Message);
}
}
It is also giving me this
SMTP Exception: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.
I want to be able to send email without having to turn on the "allow less secure app" option on gmail account.
Is there any other way for third party apps to send emails without having to do 'allow less secure apps' ??