1

我目前正在处理一段代码,它需要在它生成的电子邮件中发送附件。附件是 PDF 文件。由于要求我无法保存 PDF 并发送它,所以我不得不创建一个内存流附件。

我遇到的问题是文件大小为 _500KB。但是将文件保存在我的机器上,它大约是 370KB。

文件大小的这种增加是不可接受的。有没有人遇到过这个问题?如果是这样,他们是如何解决这个问题的。

下面是代码部分。'

Dim memStream As System.IO.MemoryStream = Nothing
'assign number to the PDF name
Dim filename As String = req.GetAccountNumber()
'add file extension
filename &= ".pdf"

'initialise the memory stream
memStream = New System.IO.MemoryStream()

'generate the pdf and put it in a byte array
Dim arrBytData() As Byte = CType(PDFRequest(),Byte()))

'flush the stream
memStream.Flush()

'Create new instances of the message and attachments
'and intialise them
Dim msg As New System.Net.Mail.MailMessage(req.EmailFrom, req.EmailTo)
Dim att As New System.Net.Mail.Attachment(memStream, filename)

With msg
     .Attachments.Add(att)
     .Body = req.EmailBody
     .Subject = req.EmailSubject
End With

'connect to the server and send the message
Dim client As New System.Net.Mail.SmtpClient()
client.Host = PDFService(Of T).mSMTPServer
client.Send(msg)

'Close our stream
memStream.Close()
msg = Nothing
4

2 回答 2

4

问题是附件必须使用 Base64 编码 - 原始二进制数据无法在电子邮件中“存活”,因此必须将其编码为仅使用某些“安全”字符的形式。这增加了大小 - 原始二进制文件有 256 个“字符”,而 Base64 编码只有 64 个。实际上没有任何办法。

于 2009-12-22T10:25:28.533 回答
2

增加可能是由于二进制数据附加到电子邮件时的 base64 编码。AFAIK 对此您无能为力。

于 2009-12-22T10:25:40.163 回答