繁体   English   中英

打开带有附件的客户端默认邮件

[英]Opening the client default mail with attachment

我正在使用 asp 核心 mvc 项目。 我想打开带有 iTextSharp 输出的客户端默认邮件作为电子邮件的附件,是否可以使用 IEmailSender 或任何其他参考? 如果我可以清空字段,从并只保留附件和主题。

[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 });
        }

………………………………………………………………………………………………………………………………………………………… ……………………………………………………………………………………………………………………………………………………………………………………

提前致谢

这根本不可能 - 如果 Web 应用程序可以打开带有随机文件的默认电子邮件客户端(!),这将是一个很大的安全漏洞。

mailto协议仅允许您设置以下属性:

  • subject :出现在邮件主题行中的文本。
  • body : 出现在消息正文中的文本。
  • CC :要包含在邮件“抄送”(抄送)部分中的地址。
  • BCC :要包含在消息的“bcc”(密件抄送)部分中的地址。

一种可能的想法是让您的用户首先将文件上传到您的网站,然后您创建一个mailto:链接,其中包含他们在电子邮件正文中上传的文件的 URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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