2013-08-02 53 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; 
} 

问题解决了。

相关问题