我想通过电子邮件发送pdf文件,该文件到了,但它已损坏,不知道为什么?
Dim buffer As Byte() = New Byte(6499) {}
Dim bytesRead As Integer = 0
bytesRead = file.FileByteStream.Read(buffer, 0, buffer.Length)
Dim ms As New MemoryStream(bytesRead)
ms.Seek(0, SeekOrigin.Begin)
mail.Attachments.Add(New Attachment(ms, "test.pdf", "application/pdf"))
mail.IsBodyHtml = False
mail.From = New System.Net.Mail.MailAddress("xxxxx@gmail.com")
mail.To.Add("yyyyyy@gmail.com")
mail.Subject = "test subject"
mail.Body = "test body"
mail.Priority = System.Net.Mail.MailPriority.Normal
Dim smtp As New System.Net.Mail.SmtpClient
smtp.Host = "smtp.gmail.com"
smtp.Credentials = New System.Net.NetworkCredential("mymail@gmail.com", "pass")
smtp.Port = 587
smtp.EnableSsl = True
smtp.Send(mail)enter code here
答案 0 :(得分:1)
我不确定您的问题是否从文件系统中读取了附件,但请尝试更改此行:
Dim ms As New MemoryStream(bytesRead)
到此:
Dim ms = File.OpenRead("my file path")
并丢弃其上方的所有内容。