3

我可以发送电子邮件和所有内容,但我无法创建有效的 Attachment() 来放入我的电子邮件。我在网上找到的所有示例都假定它以某种方式保存在我的机器上本地并通过路径链接它,但事实并非如此。在我的方法中,我使用 Winnovative 创建文件,然后将其附加到电子邮件中并发送。不涉及储蓄。

protected void SendEmail_BtnClick(object sender, EventArgs a) 
{
    if(IsValidEmail(EmailTextBox.Text)) 
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(EmailTextBox.Text);
            mailMessage.From = new MailAddress("my email");
            mailMessage.Subject = " Your Order";

            string htmlstring = GenerateHTMLString();

            Document pdfDocument = GeneratePDFReport(htmlstring);
            //Attachment attachment = new Attachment("Receipt.pdf",pdfDocument);
            //mailMessage.Attachments.Add();

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
            //needs to change this password
            smtpClient.Credentials = new NetworkCredential("j@gmail.com","password"); //change password to password of email
            smtpClient.EnableSsl = true;
            try 
            {
                smtpClient.Send(mailMessage);
                EmailErrorMessage("Email Sent");
            }
            catch(Exception exc) 
            {
                EmailErrorMessage("Email could not be sent");
            }

        }
        catch (Exception ex)
        {

            EmailErrorMessage("Could not send the e-mail. Error: "+ex.Message);
        }
    }
    else 
    {
        EmailErrorMessage("Please input a valid email.");
    }
}

编辑:这是我完成的解决方案。

                MailMessage mailMessage = CreateMailMessage();
                SmtpClient smtpClient = CreateSMTPClient();
                string htmlstring = GenerateHTMLString();
                Document pdfDocument = GeneratePDFReport(htmlstring);

                // Save the document to a byte buffer
                byte[] pdfBytes;
                using (var ms = new MemoryStream())
                {
                    pdfDocument.Save(ms);
                    pdfBytes = ms.ToArray();
                }
                smtpClient.Send(mailMessage);
                EmailErrorMessage("Email Sent");



                // create the attachment
                Attachment attach;
                using (var ms = new MemoryStream(pdfBytes))
                {
                    attach = new Attachment(ms, "application.pdf");
                    mailMessage.Attachments.Add(attach);
                    try
                    {
                        smtpClient.Send(mailMessage);
                        EmailErrorMessage("Email Sent");
                    }
                    catch (Exception exc)
                    {
                        EmailErrorMessage("Could not send the e-mail properly. Error: " + exc.Message);
                    }
                } 

结果:电子邮件发送,但附件名为 application_pdf 而不是 application.pdf。有没有什么办法解决这一问题?

4

3 回答 3

8

我没有使用过 Winnovate 的 PDF,但快速查看他们的文档表明类似以下内容应该可以工作:

// Save the document to a byte buffer
byte[] pdfBytes;
using (var ms = new MemoryStream())
{
    pdfDocument.Save(ms);
    pdfBytes = ms.ToArray();
}

// create the attachment
Attachment attach;
using (var ms = new MemoryStream(pdfBytes))
{
    attach = new Attachment(ms, "application/pdf");
}

// and add it to the message
mailMessage.Attachments.Add(attach);

评论后编辑:

如果可能是流必须保持打开状态才能发送消息。也许使用它来添加附件并发送消息:

Attachment attach;
var attachStream = new MemoryStream(pdfBytes);
try
{
    attachStream = new Attachment(ms, "application/pdf");
    mailMessage.Attachments.Add(attachStream);
    // do other stuff to message
    // ...
    // and then send it.
    smtpClient.Send(MailMessage)
}
finally
{
    attachStream.Close();
}
于 2014-05-30T20:20:33.403 回答
1

您可以使用流对象来保存 pdf 字符串。您可以使用此流对象进行附件。请在微软网站探索附件课程。

http://msdn.microsoft.com/en-us/library/System.Net.Mail.Attachment(v=vs.110).aspx

于 2014-05-30T20:15:30.957 回答
1

byte[] pdfBytes = pdfDocumnet.GetBytes() //some how figure it out to read the byte array

您可以从中读取字节MemoryStream并将其发送到邮件附件,如下所示

Attachment att = new Attachment(new MemoryStream(pdfBytes), name);

于 2014-05-30T20:16:32.047 回答