3

这不应该那么复杂。

我只是想发送一封带有 .pdf 附件的电子邮件。电子邮件与附件一起发送和接收,但长度始终小于 1k。当我尝试在 Outlook 中打开它时,Adobe Reader 提示我无法打开它,因为它要么是“不受支持的文件类型”,要么是“文件已损坏(解码错误)”。这是代码:

function Send() {
    var attachment = new Attachment(
    input.Data.ReceiptFile.InputStream, "Receipt.pdf", "application/octet-        stream"/*MediaTypeNames.Application.Pdf*/);

    this.SendSubmitReceiptNotification(new Attachment[] { attachment });
    }

private void SendSubmitReceiptNotification(Attachment[] attachments = null)
             {
                 try
                 {
                     var submitForSamplesFromEmail = Config.GetSetting("SubmitForSamples:FromEmail");
                     var submitForSamplesToEmails = Config.GetSetting("SubmitForSamples:ToEmails");

                     if (string.IsNullOrWhiteSpace(submitForSamplesFromEmail) || string.IsNullOrWhiteSpace(submitForSamplesToEmails))
                     {
                         // From/To missing.
                         return;
                     }

                     var toEmails = submitForSamplesToEmails.Split(
                         new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                     var to = toEmails.ToArray();
                     var from = submitForSamplesFromEmail;
                     var subject = "Test";
                     var body = "<strong>Hi There</strong>";
                     var isHtml = true;

                     //
                     // make the email object
                     MailMessage message = new MailMessage();

                     // set whether or not it's an html message
                     message.IsBodyHtml = isHtml;

                     // add the from address
                     message.From = new MailAddress(from);

                     // add the subject
                     message.Subject = subject;

                     // add the message body
                     message.Body = body;

                     // add the attachments
                     if (attachments != null && attachments.Length > 0)
                     {
                         // there are attachments that need attaching
                         foreach (Attachment att in attachments)
                         {
                             // add each attachemnt in order
                             message.Attachments.Add(att);
                         }
                     }

                     // add the list of who to send the email to
                     foreach (string address in to)
                     {
                         message.To.Add(address);
                     }
                     //
                     //message.HeadersEncoding = System.Text.Encoding.Unicode

                     SmtpClient smtpClient = new SmtpClient();

                     smtpClient.Send(message);

                     //Email.Send(to, from, subject, body, isHtml, attachments);
                 }
                 catch (Exception e)
                 {
                     var error = e.Message;
                 }
             }
4

1 回答 1

0

我认为您的输入流指向末尾,请尝试使用以下命令重置它:

input.Data.ReceiptFile.InputStream.Seek(0, SeekOrigin.Begin)

一些流不支持搜索,这可能很烦人。在这种情况下,您需要复制到内存中的临时流中,从头开始查找,然后发送邮件

mimetype 也应该是“application/pdf” (来源)

于 2015-01-15T01:21:18.647 回答