如何附加采用字符串格式的PDF文档

本文关键字:PDF 文档 格式 字符串 何附加 | 更新日期: 2023-09-27 18:22:17

我从客户兑换MSC Cruises代金券的URL构建PDF文档。我已经测试过了,它构建了PDF,将其显示给用户,并将其保存在文件夹目录中。

DataTable dt = verifyRedemption(VoucherRemption);
        if (dt.Rows.Count > 0)
        {
            string strVCode = dt.Rows[0]["voucherCode"].ToString();
            // save pdf document
            string fileLoc = ""; 
            fileLoc += context.Server.MapPath("PDFs");
            fileLoc += "/";
            fileLoc += strVCode + ".pdf";
            doc.Save(fileLoc);
            // close pdf document
            doc.Close();
            MailHelper SendPdf = new MailHelper();
            SendPdf.SendEmail("MSC Cruises", "Kei@3tier.co.za", "Your voucher email", "Hi, <br/><br/> Please see your voucher ", doc.Pages.ToString());
            context.Response.Redirect(url);
        }

除了当我发送电子邮件时,我必须发送5种字符串格式,有没有办法改变我将其作为pdf附件发送的方式?(当我构建PDF.

如何附加采用字符串格式的PDF文档

时,Doc是在数据表之前声明的

为什么不使用System.Net.Mail命名空间?

它提供以下内容:

MailMessage mail = new MailMessage("MailFrom", "MailTo");
//Then you could do sth to grab your files like before:
List<Attachment> attachments = new List<Attachment>();
foreach(string attachmentPath in docs)
{
    //Here you provide the path where you saved the pdf
    attachments.Add(new Attachment(attachmentPath));
}
mail.Subject = "Here comes the PDF";
mail.BodyEncoding = Encoding.UTF8;
mail.Body = "Your message here";
SmtpClient mailer = new SmtpClient();
mailer.EnableSsl = true; //Depends on your mailserver if he forces to use SSL
//(Same procedure as setting up a new Outlook Account...)
mailer.Host = "mail.gmx.net"; //for example:
mailer.Port = "587"; //Default SMTP-Port is 25
mailer.UseDefaultCredentials = false;
//Just your common LoginData for this server
mailer.Credentials = new NetworkCredential("EmailUserName", "EmailPassword");
mailer.Send(mail);

你所需要做的就是"找到"你之前保存到某个目录的pdf,获取它的路径并从中创建一个附件。

希望能有所帮助。