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



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

[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:显示在邮件正文中的文本
  • CCD_ 4:要包括在";cc";(复写本(部分
  • CCD_ 5:要包括在";bcc";(复写本(部分

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

最新更新