2013-08-02 50 views
-1

我有一個包含html的字符串。我已經能夠將它保存爲一個.doc文件到我的電腦/服務器,但我希望能夠通過電子郵件從C#發送,而不必先保存到服務器。我有什麼是:生成Word文檔並附加/發送電子郵件而不保存?

MailMessage msg = new MailMessage(); 

     msg.From = "[email protected]"; 
     msg.To = "[email protected]"; 
     msg.Subject = "Subject"; 
     msg.Body = "Body"; 

     string body = "<html><head></head><body><p>Word doc body here</p></body></html>"; 
     byte[] data = Encoding.ASCII.GetBytes(body); 
     MemoryStream ms = new MemoryStream(data); 
     Attachment att = new Attachment(ms, "Interview-Questions-Template.doc", "application/application/msword"); 
     msg.Attachments.Add(att); 

     SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["SmtpPrimaryServer"]); 

如果我拉出附件,他們會發送電子郵件。 ,我得到的錯誤是:

System.FormatException: The specified content type is invalid. 
at System.Net.Mime.ContentType.ParseValue() 
at System.Net.Mime.ContentType..ctor(String contentType) 
at System.Net.Mime.MimePart.SetContent(Stream stream, String name, String mimeType) 
at System.Net.Mail.Attachment..ctor(Stream contentStream, String name, String mediaType) 
at EmailClass.sendEmail(String To, String Bcc, String Cc, String From, String Subject, String body) in <i took out the file path> 

錯誤的最後一行所指向的線在具有代碼:

Attachment att = new Attachment(ms, "Interview-Questions-Template.doc", "application/application/msword"); 

任何幫助將不勝感激。

回答

0

好的。我想通了,問題是我把字符串轉換成byte []的地方。通過使用靜態方法:

static byte[] GetData() 
{ 
    string s = "<html><head></head><body><p>Word doc body here</p></body></html>"; 
    byte[] data = Encoding.ASCII.GetBytes(s); 
    return data; 
} 

問題解決了。

相關問題