Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将生成的PDF附加到.NET SMTP?_C#_Asp.net Mvc_Pdfsharp_System.net.mail - Fatal编程技术网

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

C# 如何将生成的PDF附加到.NET SMTP?,c#,asp.net-mvc,pdfsharp,system.net.mail,C#,Asp.net Mvc,Pdfsharp,System.net.mail,我正在使用PDFsharp生成作业报告。我需要将该报告自动附加到发送的电子邮件中。现在,当用户以角度模式查看职务信息时,他们单击“电子邮件”,生成报告,并显示一个新模式和电子邮件输入字段。如何设置电子邮件控制器以在JobSetupPdfs文件夹中找到正确的PDF PDF控制器 public string Get(int id = 1) { JobDataAdapter adapter = new JobDataAdapter(); Job job = n

我正在使用PDFsharp生成作业报告。我需要将该报告自动附加到发送的电子邮件中。现在,当用户以角度模式查看职务信息时,他们单击“电子邮件”,生成报告,并显示一个新模式和电子邮件输入字段。如何设置电子邮件控制器以在JobSetupPdfs文件夹中找到正确的PDF

PDF控制器

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();
    });
}
邮件控制器

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();
    });
}
角度控制器

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"));

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

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


查看这篇MSDN文章了解更多信息:

您可以将流的内容添加为附件。PDFsharp可以将PDF文件保存到流中。@user3919120请参见上文,使用文件流作为附件。出于调试目的,在开发和附加文件时保存文件可能更容易。然而,真正的生产方式是使用流。