Using SendMessage to email a file attachment witho

2019-07-20 05:53发布

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

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. Is there any way to fix this?

3条回答
啃猪蹄的小仙女
2楼-- · 2019-07-20 06:08

You can use stream object to hold pdf string. 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

查看更多
姐就是有狂的资本
3楼-- · 2019-07-20 06:18

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();
}
查看更多
Luminary・发光体
4楼-- · 2019-07-20 06:28

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

You can read the bytes from the MemoryStream and send it to mail attachment as shown below

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

查看更多
登录 后发表回答