C#Core Builder.Attachments,附加Word但附件后原始文件有1页成为4页

时间:2018-02-23 09:27:41

标签: c# asp.net mailkit

我正在使用ASP.NET Core 2.0,我正在尝试将Word文件附加到我的电子邮件中。电子邮件工作正常,但我的附件存在一些问题。

原始word文档只有1页,但在从收件人的邮箱附加和下载后,它开始有4页。第1页的部分文字已经进入第2页和第3页。

这是我的代码:

public string TestSend()
{
  var webRoot = _hostingEnvironment.WebRootPath;
  var pathToFile = "EmailTemplates"
                   + Path.DirectorySeparatorChar.ToString()
                   + "Test.html";

  var builder = new BodyBuilder();
  using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile))
  {    
    builder.HtmlBody = SourceReader.ReadToEnd();    
  }
  string messageBody = string.Format(builder.HtmlBody,
                       "TEST",
                       "TEST",
                       "TEST",
                       "TEST",
                       "TEST" );    

   builder.HtmlBody = messageBody;
   builder.Attachments.Add(@"C:\Users\Test\Desktop\Test\Temp.docx");

   var message = new MimeMessage();
   message.From.Add(new MailboxAddress("Test", Test@test.com));
   message.To.Add(new MailboxAddress("Test", JustDemo@test.com));
   message.Cc.Add(new MailboxAddress("Test", Test@test.com));    

   message.Subject = "Test";
   message.Body = builder.ToMessageBody();               

   using (var client = new SmtpClient())
   {
     client.Connect("smtp.gmail.com", 587, false);
     client.Authenticate("test@test.com", "Test");
     client.Send(message);
     client.Disconnect(true);    
   }  

     return "a";    
}

1 个答案:

答案 0 :(得分:2)

尝试强制Content-Transfer-Encoding为base64:

var attachment = (MimePart) builder.Attachments.Add ("C:\.....");
attachment.ContentTransferEncoding = ContentEncoding.Base64;