简体   繁体   English

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

[英]Using SendMessage to email a file attachment without saving the file

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. 在我的方法中,我使用Winnovative创建文件,然后我想将它附加到电子邮件并发送它。 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.");
    }
}

EDIT: 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);
                    }
                } 

Result: The email sends, but the attachment is named application_pdf instead of application.pdf. 结果:电子邮件发送,但附件名为application_pdf而不是application.pdf。 Is there any way to fix this? 有没有什么办法解决这一问题?

I haven't used Winnovate's PDF, but a quick look at their documentation indicates that something like the following should work: 我没有使用过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);

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

You can use stream object to hold pdf string. 您可以使用流对象来保存pdf字符串。 You can use this stream object for attachment. 您可以使用此流对象进行附件。 Kindly explore attachment class at microsoft site. 请在微软网站上探索附件类。

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

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM