简体   繁体   中英

Opening the client default mail with attachment

I'm working with asp core mvc project. I would like to open the client default mail with a iTextSharp output as an attachment for the email, is this possible using IEmailSender or any other reference? and if i can empty the fields to,from and keep only the attached file and subject.

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> GeneratePDF(int? id)
        {
            var webRootPath = _hostingEnvironment.WebRootPath;
            var path = Path.Combine(webRootPath, "DataDump"); //folder name
            var story = await _db.Story.Include(s => s.Child).Include(s => s.Sentences).ThenInclude(s => s.Image).FirstOrDefaultAsync(s => s.StoryId == id);


            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
                iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 10);
                PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
                document.Open();

                string usedFont = Path.Combine(webRootPath + "\\Fonts\\", "Dubai-Light.TTF");
                BaseFont bf = BaseFont.CreateFont(usedFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                iTextSharp.text.Font titleFont = new iTextSharp.text.Font(bf, 20);
                iTextSharp.text.Font sentencesFont = new iTextSharp.text.Font(bf, 15);
                iTextSharp.text.Font childNamewFont = new iTextSharp.text.Font(bf, 17);

                PdfPTable T = new PdfPTable(1);
                //Hide the table border
                T.DefaultCell.BorderWidth = 0;
                T.DefaultCell.HorizontalAlignment = 1;
                //Set RTL mode
                T.RunDirection = PdfWriter.RUN_DIRECTION_RTL;

                //Add our text
                if (story.Title != null)
                {
                    T.AddCell(new iTextSharp.text.Paragraph(story.Title, titleFont));
                }
                if (story.Child != null)
                {
                    if (story.Child.FirstName != null && story.Child.LastName != null)
                    {
                        T.AddCell(new iTextSharp.text.Phrase(story.Child.FirstName + story.Child.LastName, childNamewFont));
                    }
                }
                if (story.Sentences != null)
                {
                    foreach (var item in story.Sentences)
                    {
                        if (item.Image != null)
                        {
                            var file = webRootPath + item.Image.ImageSelected;
                            byte[] fileBytes = System.IO.File.ReadAllBytes(file);
                            iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(fileBytes);
                            pic.ScaleAbsoluteWidth(25f);
                            T.AddCell(pic);

                        }
                        else
                        {
                            T.AddCell(new iTextSharp.text.Phrase("no image", sentencesFont));
                        }
                        T.AddCell(new iTextSharp.text.Phrase(item.SentenceText, sentencesFont));
                    }
                }
                document.Add(T);
                document.Close();

                byte[] bytes = memoryStream.ToArray();
                var fileName = path + "\\PDF" + DateTime.Now.ToString("yyyyMMdd-HHMMss") + ".pdf";

                using (FileStream fs = new FileStream(fileName, FileMode.Create))
                {
                    fs.Write(bytes, 0, bytes.Length);
                }
                memoryStream.Close();
                //Send generated pdf as attchment
                
                //Remove form root
                if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                }

            }
            return RedirectToAction("Details", new { id = id });
        }

................................................................................................

Thanks in advance

This is not at all possible - and it would be a big security hole if web applications could open our default email client with random files attached (!).

The mailto protocol allows you to set the following properties only:

  • subject : Text to appear in the subject line of the message.
  • body : Text to appear in the body of the message.
  • CC : Addresses to be included in the "cc" (carbon copy) section of the message.
  • BCC : Addresses to be included in the "bcc" (blind carbon copy) section of the message.

One possible idea would be to allow your users to upload files to your website first, and you create a mailto: link that includes the URL of the file they uploaded in the body of the email message.

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