使用动态创建的pdf作为附件在c#中发送邮件

时间:2014-07-02 07:14:56

标签: asp.net email pdf

我必须在我的Web应用程序(C#,ASP.NET)中发送带有PDF附件(动态创建)的电子邮件。 我们如何使用动态创建的pdf作为附件在c#(Asp.net)中发送邮件?

1 个答案:

答案 0 :(得分:0)

您可以发送包含以下附件的电子邮件:

        List<string> attachments = //set a list of PDF paths here

        SmtpClient smtpClient = new SmtpClient("SmtpHost", SmtpPort);
        smtpClient.Credentials = new NetworkCredential("Username", "Password");
        smtpClient.EnableSsl = false;

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("FromEmail", "FromName", Encoding.UTF8);
        mail.To.Add(new MailAddress("ToEmail", "ToName", Encoding.UTF8));

        //get a list of your file's paths from your program and use it here
        AddAttachments(mail.Attachments, attachments);

        mail.Subject = "Subject";
        mail.SubjectEncoding = Encoding.UTF8;

        mail.IsBodyHtml = true;
        mail.Body = "Message";
        mail.BodyEncoding = Encoding.UTF8;

        //send an email
        smtpClient.Send(mail);

        //clean up
        mail.Dispose();


    void AddAttachments(AttachmentCollection attachment, List<string> attachments)
    {
        if (attachments != null)
        {
            foreach (string filePath in attachments)
                attachment.Add(new Attachment(filePath));
        }
    }