使用SendMessage通过电子邮件发送文件附件而不保存文件

时间:2014-05-30 20:08:25

标签: c# asp.net email-attachments winnovative

我可以发送电子邮件,但我无法创建有效的附件()以放入我的电子邮件中。我在网上找到的所有例子都假设它以某种方式保存在我的机器本地并通过路径链接它但事实并非如此。在我的方法中,我使用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。有什么方法可以解决这个问题吗?

3 个答案:

答案 0 :(得分:7)

我还没有使用过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();
}

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

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

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

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

相关问题