0

I want to send a (view page) pdf sending by email.but when I trying to add an attachment, I found an error below "Add". for that I can't successfully sending mail with attaching my view pdf. Here is my code:

(Ordercontroller)

//other code


            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("Test Project", "pt300@gmail.com"));
            message.To.Add(new MailboxAddress("psm", "p689@gmail.com"));
            message.Subject = "Hi,this is demo email";
            message.Body = new TextPart("plain")
            {
                Text = "Hello,My First Demo Mail it is.Thanks",
            };

            //add attach
            var aa = new ViewAsPdf("Cart")
            {
                FileName = "Invoice.pdf",          //actually, I don't know why this filename is 
                                                    // "Invoice". I found this system on a website.

                PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
            };
         
            message.Attachments.Add(aa);

            //end attach
            using (var client = new SmtpClient())
            {
                client.Connect("smtp.gmail.com", 587, false);
                client.Authenticate("pt300@gmail.com", "MyPassword");

                client.Send(message);
                client.Disconnect(true);

            }

//other code

and these controller's view I trying to pdf and send by mail:

(HomeController)

  public ActionResult Cart()
        {
            List<Product> products = HttpContext.Session.Get<List<Product>>("products");
            if (products == null)
            {
                products = new List<Product>();
            }
            return View(products);
        }

in OrderController I found an error.

enter image description here

how I will solve this problem and successfully send my view's pdf by mail.please help.

Pritom Sarkar
  • 2,154
  • 3
  • 11
  • 26
  • 1
    Does this answer your question? [How to send email with attachment using MimeMessage in asp.net core web API?](https://stackoverflow.com/questions/41392811/how-to-send-email-with-attachment-using-mimemessage-in-asp-net-core-web-api) – Sir Rufo Oct 25 '20 at 09:29
  • @SirRufo here solution used just attachment anything. but I want to send mail cart.cshtml in pdf format. – Pritom Sarkar Oct 25 '20 at 13:39
  • if you consider ```var aa = new ViewAsPdf("Cart")``` line, here I want cart.cshtml pdf format and then send it by mail. but problem doing "Add" which output I posted. – Pritom Sarkar Oct 25 '20 at 13:40

1 Answers1

1

Attachments is a MimeMessage property that has no such method Add, so here comes the error. To add the attachments you should create a BodyBuilder object before, with this class you will be able to add new attachments (each one of them as byte array), usign Attachments Property and its Add method, but always related to BodyBuilder, not to MimeMessage.

Please, take a look to the answer given here, as I guess is what you are looking for: .net Core Mailkit send attachement from array

Or check another example here: https://www.taithienbo.com/send-email-with-attachments-using-mailkit-for-net-core/

In addition, you could get Rotativa PDF as byte array usign this code:

var rotativaAction = new ViewAsPdf("Cart")
{
    FileName = "Invoice.pdf",   // You can change this file name to set whatever you want
    PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
};

byte[] fileAttachmetByteArray = rotativaAction.BuildFile(ControllerContext);

Once you have generated your PDF as a byte array using Rotativa, guessing you have stored the result in a variable called fileAttachmentByteArray (as stated in the previous code sample), you must simply send this variable (the byte array) to the Add method of the BodyBuilder, as the second parameter (first is the attachement file name, which is completely free to use the name you prefer), and then set Mime Message Body, usign the BodyBuilder you have worked with.

So, to explain the process in steps:

  1. On the first line you create a BodyBuilder variable, and initialize it, using the text you want as body for the email.
  2. Then, you call Add method to add a new attachment, sending to it the file name you wish and your previously created bye array
  3. Finally, you asign the value to the Body property of your Mime Message

And your code should look like this:

var builder = new BodyBuilder { HtmlBody = "yourBodyMessage" };   // Change "yourBodyMessage" to the email body you desire
builder.Attachments.Add("filename", fileAttachmentByteArray);   // Once again, you can change this "filename" to set whatever you want
mimeMessage.Body = builder.ToMessageBody();   // Assuming mimeMessage is the same variable you provided in your code
Dave Miller
  • 536
  • 3
  • 19
  • now I understand that I have to follow and add attachment according to your last code. I would be grateful if you update your code to add this attachment using the byte array. – Pritom Sarkar Oct 26 '20 at 12:49
  • 1
    Hi @Shane Watson. Maybe I posted the information in the wrong order, I will edit the answer. Please, check if this is the process flow is clear if this is what you are asking for. Thanks! – Dave Miller Oct 27 '20 at 08:08