如何将生成的PDF附加到.NET SMTP?

时间:2014-09-21 16:25:52

标签: c# asp.net-mvc pdfsharp system.net.mail

我正在使用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"));

1 个答案:

答案 0 :(得分:1)

您可以在邮件消息对象上使用附件集合。

msg.Attachments.Add(新附件(PathToAttachment));

有关详细信息,请查看此MSDN文章:http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx