简体   繁体   中英

I'm trying to send email to user's through my web application. But email sending is blocked by gmail. Any solution please?

I am trying to sent email from my web application (an e-commerce site). i enabled "Less secure app access" from my mail account. And enabled IMAP also from mail setting. but still sending email is blocked or failed. What can be the possible reason for this? or i missing something in the code section?

here is code:

var client = new SmtpClient("smtp.gmail.com", 587)
{
    EnableSsl = true,
    Credentials = new NetworkCredential(userName, password)
};
var sender = new MailAddress(senderEmail, senderName);
var receiver = new MailAddress(receiverEmail);
var message = new MailMessage(@sender, receiver);

IIRC then I followed this guidance to enable gmail smtp for my application.

  1. Important is to have set two factor authentication for the account.
  2. Create app password and use it in your application

then you use it as

MailMessage mm = new MailMessage();
mm.From = new MailAddress(<your-gmail>);
mm.To.Add(address);
mm.Subject = subject;
mm.Body = body;

SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";                    
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(<your-gmail>, <app-password>);

smtp.Send(mm);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM