Asp.net mvc 4 MVC4.5:使用SmtpClient发送带有.pdf附件的电子邮件-我在这里缺少什么?

Asp.net mvc 4 MVC4.5:使用SmtpClient发送带有.pdf附件的电子邮件-我在这里缺少什么?,asp.net-mvc-4,sendmail,email-attachments,smtpclient,Asp.net Mvc 4,Sendmail,Email Attachments,Smtpclient,这不应该那么复杂 我只想发送一封带有.pdf附件的电子邮件。电子邮件随附件发送和接收,但长度始终小于1k。当我尝试在Outlook中打开它时,Adobe Reader会告诉我它无法打开它,因为它是“不受支持的文件类型”或“文件已损坏(解码错误)”。代码如下: function Send() { var attachment = new Attachment( input.Data.ReceiptFile.InputStream, "Receipt.pdf", "applicati

这不应该那么复杂

我只想发送一封带有.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;
                 }
             }
函数Send(){
var附件=新附件(
input.Data.ReceiptFile.InputStream,“Receipt.pdf”,“application/octet-stream”/*MediaTypeNames.application.pdf*/);
此.SendSubmitReceiptNotification(新附件[]{Attachment});
}
私有无效SendSubmitReceiptNotification(附件[]附件=null)
{
尝试
{
var submitForSamplesFromEmail=Config.GetSetting(“SubmitForSamples:FromEmail”);
var submitForSamplesToEmails=Config.GetSetting(“SubmitForSamples:ToEmails”);
if(string.IsNullOrWhiteSpace(submitForSamplesFromEmail)| | string.IsNullOrWhiteSpace(submitforsamplestoemail))
{
//从/到失踪。
返回;
}
var toEmails=submitForSamplesToEmails.Split(
新字符[]{“;”},StringSplitOptions.RemoveEmptyEntries);
var to=toEmails.ToArray();
var from=提交,以获取电子邮件样本;
var subject=“Test”;
var body=“你好”;
var isHtml=真;
//
//使电子邮件成为对象
MailMessage=新的MailMessage();
//设置是否为html消息
message.IsBodyHtml=isHtml;
//添加发件人地址
message.From=新邮件地址(From);
//添加主题
message.Subject=Subject;
//添加消息正文
message.Body=Body;
//添加附件
如果(附件!=null&&attachments.Length>0)
{
//有需要附加的附件
foreach(附件中的附件att)
{
//按顺序添加每个附件
邮件.附件.添加(附件);
}
}
//添加要向其发送电子邮件的人的列表
foreach(到中的字符串地址)
{
消息.To.Add(地址);
}
//
//message.HeadersEncoding=System.Text.Encoding.Unicode
SmtpClient SmtpClient=新的SmtpClient();
发送(消息);
//电子邮件。发送(收件人、发件人、主题、正文、isHtml、附件);
}
捕获(例外e)
{
var错误=e.消息;
}
}

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

input.Data.ReceiptFile.InputStream.Seek(0, SeekOrigin.Begin)
有些流不支持查找,这可能会很烦人。在这种情况下,您需要复制到内存中的临时流中,将其查找到开头,然后发送邮件


此外,mimetype应该是“application/pdf”

您是否尝试调试它?也许这种依恋本身并不好。尝试从文件系统读取文件并以相同的方式发送。我认为问题出在这里“var attachment=new attachment(input.Data.ReceiptFile.InputStream,“Receipt.pdf”,“application/octet-stream”/*MediaTypeNames.application.pdf*/)