【发布时间】:2014-09-21 16:25:51
【问题描述】:
我正在使用 PDFsharp 生成工作报告。我需要将该报告自动附加到外发电子邮件中。现在,当用户以角度模式查看工作信息时,他们单击电子邮件并生成报告,并在电子邮件输入字段中出现一个新模式。如何设置电子邮件控制器以在 JobSetupPdfs 文件夹中找到正确的 PDF?
PdfController
public string Get(int id = 1)
{
JobDataAdapter adapter = new JobDataAdapter();
Job job = new Job();
job = adapter.GetJob(id);
if (job == null)
{
return string.Empty;
}
try
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
gfx.DrawString("Texas Exterior Systems", HeadingFont, XBrushes.Black,
new XRect(0, 50, page.Width, page.Height),
XStringFormats.TopCenter);
gfx.DrawString("Job Setup Sheet", BodyFont, XBrushes.Black,
new XRect(0, 80, page.Width, page.Height),
XStringFormats.TopCenter);
var filename = string.Format(@"C:\Users\texas_000\Desktop\TexasExterior\TexasExterior\JobSetupPdfs\{0}.pdf", job.JobName);
document.Save(filename);
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
}
return string.Empty;
}
邮件控制器
[Authorize]
public ActionResult EmailPdf( string To, string Cc, string comments, string Bcc, HttpPostedFileBase fileUploader)
{
try
{
SmtpClient client = new SmtpClient("mail.texasicecarving.com", 8889);
//client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("postmaster@texasicecarving.com", "******");
MailMessage msg = new MailMessage();
if (fileUploader != null)
{
string fileName = Path.GetFileName(fileUploader.FileName);
msg.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
}
msg.To.Add(new MailAddress( "texas697@gmail.com"));
msg.From = new MailAddress("postmaster@texasicecarving.com");
// msg.Subject = name + " " + subject;
msg.Body = comments;
//MailAddress Ccopy = new MailAddress(user.Email);
//msg.CC.Add(Ccopy);
//MailAddress Bcopy = new MailAddress(Bcc);
//msg.Bcc.Add(Bcopy);
client.Send(msg);
Console.WriteLine("Successfully Sent Message.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return View();
}
角度控制器
$scope.EmailPdf = function () {
$http.get('/api/Pdf/{id}').success(function () {
$scope.PrintPreviewModal();
});
}
更新 好的,所以我用正确的路径设置了它。现在如何让它附加刚刚生成的 PDF?我是否需要在 PDF 中添加时间戳并让 msg.Attachments 在该文件夹中附加最新的 PDF?
msg.Attachments.Add(new Attachment(@"C:\Users\texas_000\Desktop\TexasExterior\TexasExterior\JobSetupPdfs\Restoration.pdf"));
【问题讨论】:
-
您可以将流的内容添加为附件。 PDFsharp 可以将 PDF 文件保存到流中。
-
@user3919120 见上文,使用文件流作为附件。出于调试目的,在开发和附加时保存文件可能更容易。然而真正的生产方式是使用流。
标签: c# asp.net-mvc pdfsharp system.net.mail