0

I am trying to send email with attached pdf, my pdf is in byte array. and when i try to send mail (without pdf) it shows

Message = The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

my code is `string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"].ToString(); string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"].ToString();

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(senderEmail, senderPassword);

            MailMessage mailMassege = new MailMessage(senderEmail, toEmail, subject, body);
            mailMassege.IsBodyHtml = true;
            mailMassege.BodyEncoding = Encoding.UTF8;


            client.Send(mailMassege);`

and my pdf is in byte array byte[] applicationPDFData = actionResult.BuildPdf(ControllerContext); System.IO.File.WriteAllBytes(filePath + "/hello.pdf", applicationPDFData);

I want to to send mail with this pdf. thanks in advance

MD MASUM
  • 49
  • 11
  • have you seen this? https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – derloopkat Nov 10 '18 at 19:32
  • the code is same and i also close 2 step verification but same error – MD MASUM Nov 10 '18 at 19:40
  • It's just a guess, but on your google account, are the [less secure apps](https://support.google.com/accounts/answer/6010255?hl=en) enabled? – LeviTheOne Nov 10 '18 at 19:51
  • I can see that the error is about Authentication. Try using port 465. You can find more details here: https://support.google.com/a/answer/176600?hl=en Also, will it send the email without attachment? – Sergiu Muresan Nov 14 '18 at 13:42

1 Answers1

0

You have to create an attachment and append it to the mail Attachemnts list. here is an example:

byte[] applicationPDFData = actionResult.BuildPdf(ControllerContext);
Attachment attPDF = new Attachment(new MemoryStream(applicationPDFData), name);

EmailMessage emailMessage = new EmailMessage();
emailMessage.To.Add( new EmailRecipient( toEmail ) );
emailMessage.Subject = subject;
emailMessage.Body = body;
emailMessage.Attachments.Add( attPDF );

regrads gy

György Gulyás
  • 1,290
  • 11
  • 37