-1

我有一个包含 html 的字符串。我已经能够将它作为 .doc 文件保存到我的计算机/服务器,但我希望能够从 C# 通过电子邮件发送它,而无需先将其保存到服务器。我所拥有的是:

 MailMessage msg = new MailMessage();

        msg.From = "from@email.com";
        msg.To = "to@email.com";
        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");

任何帮助将不胜感激。

4

1 回答 1

0

好的。我想通了,问题在于我将字符串转换为字节 []。通过使用静态方法:

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;
}

问题解决了。

于 2013-08-02T17:34:38.053 回答