使用SendMessage通过电子邮件发送文件附件而不保存文件 [英] Using SendMessage to email a file attachment without saving the file

查看:203
本文介绍了使用SendMessage通过电子邮件发送文件附件而不保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以发送电子邮件以及所有内容,但不能创建有效的Attachment()放入电子邮件中.我在网上找到的所有示例均假定该文件以某种方式保存在本地计算机上,并通过路径进行链接,但实际情况并非如此.在我的方法中,我使用Winnovative创建文件,然后将其附加到电子邮件中并发送.不涉及保存.

I can send an email and everything but I cannot create a valid Attachment() to put into my email. All examples I've found online assumes that it is somehow saved locally on my machine and links it via path but that is not the case. In my method, I create the file using Winnovative, and then I want to attach it to the e-mail and send it. No saving involved.

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

这是我完成的解决方案.

This is my finished solution.

                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.有什么办法可以解决这个问题?

Result: The email sends, but the attachment is named application_pdf instead of application.pdf. Is there any way to fix this?

推荐答案

我还没有使用Winnovate的PDF,但是快速浏览一下它们的文档表明应该执行以下操作:

I haven't used Winnovate's PDF, but a quick look at their documentation indicates that something like the following should work:

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

评论后

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

Edit after comments:

If might be that the stream has to remain open in order for the message to be sent. Perhaps use this to add the attachment and send the message:

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();
}

这篇关于使用SendMessage通过电子邮件发送文件附件而不保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆