C# 打开带有附件的客户端默认邮件
我正在从事asp核心mvc项目。我想打开带有iTextSharp输出作为电子邮件附件的客户端默认邮件,是否可以使用IEmailSender或任何其他参考? 如果我可以清空字段,只保留附件和主题C# 打开带有附件的客户端默认邮件,c#,asp.net-core,itext,C#,Asp.net Core,Itext,我正在从事asp核心mvc项目。我想打开带有iTextSharp输出作为电子邮件附件的客户端默认邮件,是否可以使用IEmailSender或任何其他参考? 如果我可以清空字段,只保留附件和主题 [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> GeneratePDF(int? id) { var webRootPath =
[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 });
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务GeneratePDF(int?id)
{
var webRootPath=\u hostingEnvironment.webRootPath;
var path=path.Combine(webRootPath,“DataDump”);//文件夹名称
var story=await _db.story.Include(s=>s.Child)。Include(s=>s.句子)。然后Include(s=>s.Image)。FirstOrDefaultAsync(s=>s.StoryId==id);
使用(System.IO.MemoryStream MemoryStream=new System.IO.MemoryStream())
{
iTextSharp.text.Document Document=新的iTextSharp.text.Document(iTextSharp.text.PageSize.A4,10,10,10);
PdfWriter writer=PdfWriter.GetInstance(文档,内存流);
document.Open();
字符串usedFont=Path.Combine(webRootPath+“\\Fonts\\”,“Dubai Light.TTF”);
BaseFont bf=BaseFont.CreateFont(usedFont、BaseFont.IDENTITY、BaseFont.EMBEDDED);
iTextSharp.text.Font titleFont=新的iTextSharp.text.Font(bf,20);
iTextSharp.text.Font语句Font=新的iTextSharp.text.Font(bf,15);
iTextSharp.text.Font childNamewFont=新的iTextSharp.text.Font(bf,17);
PdfPTable T=新的PdfPTable(1);
//隐藏表格边框
T.DefaultCell.BorderWidth=0;
T.DefaultCell.HorizontalAlignment=1;
//设置RTL模式
T.RunDirection=PdfWriter.RUN\u DIRECTION\u RTL;
//添加我们的文本
if(story.Title!=null)
{
T.AddCell(新的iTextSharp.text.段落(story.Title,titleFont));
}
if(story.Child!=null)
{
if(story.Child.FirstName!=null&&story.Child.LastName!=null)
{
T.AddCell(新的iTextSharp.text.Phrase(story.Child.FirstName+story.Child.LastName,childNamewFont));
}
}
if(story.句子!=null)
{
foreach(故事中的变量项。句子)
{
如果(item.Image!=null)
{
var file=webRootPath+item.Image.ImageSelected;
byte[]fileBytes=System.IO.File.ReadAllBytes(文件);
iTextSharp.text.Image pic=iTextSharp.text.Image.GetInstance(fileBytes);
图:可缩放溶质宽度(25f);
T.AddCell(pic);
}
其他的
{
T.AddCell(新的iTextSharp.text.Phrase(“无图像”,语句sfont));
}
T.AddCell(新的iTextSharp.text.Phrase(item.SentenceText,sentencesFont));
}
}
文件.添加(T);
document.Close();
byte[]bytes=memoryStream.ToArray();
var fileName=path+“\\PDF”+DateTime.Now.ToString(“yyyyMMdd HHMMss”)+“.PDF”;
使用(FileStream fs=newfilestream(fileName,FileMode.Create))
{
fs.Write(字节,0,字节.长度);
}
memoryStream.Close();
//将生成的pdf作为附件发送
//从根中删除
if(System.IO.File.Exists(fileName))
{
System.IO.File.Delete(文件名);
}
}
返回重定向到操作(“详细信息”,新的{id=id});
}
徖
提前感谢这是不可能的-如果web应用程序可以打开我们的默认电子邮件客户端并附加随机文件(!),这将是一个很大的安全漏洞 协议仅允许您设置以下属性:
:显示在邮件主题行中的文本subject
:显示在邮件正文中的文本正文
:邮件“抄送”(复写)部分包含的地址CC
:要包含在邮件“BCC”(盲拷贝)部分的地址BCC
mailto:
链接,其中包括他们在电子邮件正文中上载的文件的URL