【问题标题】:Sending an email attatchment increases the attatchments file size in .net发送电子邮件附件会增加 .net 中的附件文件大小
【发布时间】:2009-12-22 10:22:58
【问题描述】:

我目前正在编写一段代码,需要在 它生成的电子邮件。附件是 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

【问题讨论】:

  • 那是实际的代码吗?你在哪里填充内存流?

标签: .net vb.net email memorystream


【解决方案1】:

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

【讨论】:

  • 哇,我在那个答案中使用了“很多”引号。对不起。 :)
  • 没关系,我的浏览器的字形包里还有很多引号;)
  • 显然不是我希望的答案,但这确实解释了尺寸的增加。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2013-03-18
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    相关资源
    最近更新 更多