Este código funciona bien en la cuenta personal de gmail, pero cuando trato de usar la cuenta comercial de gmail, no funciona y sigue apareciendo un error. 5.5.1 Se requiere autenticación.

void SendEmail()
{
    DataTable data = GetData();
    DataTable email_data = GetEmailData();
    data.TableName = "Employee_Data";

    using (XLWorkbook wb = new XLWorkbook())
    {
        wb.Worksheets.Add(data);

        using (MemoryStream memoryStream = new MemoryStream())
        {
            wb.SaveAs(memoryStream);
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();
            String from = "seong@abcd.net";

            for (int i = 0; i < email_data.Rows.Count; i++)
            {
                String to = email_data.Rows[i][0].ToString();

                using (MailMessage mm = new MailMessage(from, to))
                {
                    mm.Subject = "Employees Attachment";
                    mm.Body = "Employees Exported Attachment";

                    mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Employees.xlsx"));
                    mm.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
                    credentials.UserName = "seong@abcd.net";
                    credentials.Password = "1234";
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = credentials;
                    smtp.Port = 587;
                    smtp.Send(mm);
                }
            }
        }
    }
}
1
Scarlett 23 may. 2017 a las 06:25

2 respuestas

La mejor respuesta

Resolví este problema. La cuenta no debe usar la segunda verificación en gmail si desea usar SMTP.

https://support.google.com/accounts/answer/1064203?hl=es&ref_topic=7189195

No puedo controlar esas cosas, le pido al administrador que no permita la segunda verificación. Entonces puedo trabajar con esa cuenta usando SMTP.

0
Scarlett 8 jun. 2017 a las 21:47

Método Smtp para Business Gsuite

require_once('class.phpmailer.php');

$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true; 
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username   = "Enter the user ID"; // SMTP account username
// SMTP account password
$mail->Password   = "Enter your password";

$mail->SetFrom('Enter the User ID', 'Subject');
$mail->AddReplyTo("Enter the User ID", "Subject");

$mail->AddAddress($to, "Name");

$mail->Subject    = "Contact Enquiry";

$message = '';

$mail->MsgHTML($message);

if($mail->Send()){
   $mail->ClearAddresses();  
   echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
} else {
     echo "Mailer Error: " . $mail->ErrorInfo;
}
0
Adrita Sharma 8 abr. 2019 a las 11:08