我了解到 .NET CF 不支持 SmtpClient 类。它最好的是我不想使用的 PocketOutlook 类。
我发现 OpenNETCF 确实有一个 OpenNETCF.Net.Mail 命名空间,它使 SmtpClient 类可用。不幸的是,它只是部分实现,不直接支持附件:http: //community.opennetcf.com/forums/t/11325.aspx
该帖子表明仍然可以使用多部分 MIME 消息添加附件。
更新
在阅读了 ctacke 的建议以查看 w3.org 文章后,我尝试像这样更改我的方法:
using OpenNETCF.Net.Mail;
public void EmailPicture(string picLoc)
{
var smtpClient = new SmtpClient
{
Host = MailProperties.SmtpHost,
Credentials = new SmtpCredential(MailProperties.UserName, MailProperties.Password, MailProperties.Domain),
DeliveryMethod = SmtpDeliveryMethod.Network,
Port = MailProperties.Port
};
var message = new MailMessage();
var fromAddress = new MailAddress(MailProperties.From);
message.To.Add(MailProperties.To);
message.From = fromAddress;
message.Subject = "Requested Picture";
message.IsBodyHtml = false;
message.Headers.Add("MIME-Version", "1.0");
message.Headers.Add("Content-Type", "multipart/mixed; boundary=\"simple boundary\"");
var bodyBuilder = new StringBuilder();
//add text
bodyBuilder.Append("--simple boundary\r\n");
bodyBuilder.Append("Content-type: text/plain; charset=us-ascii\r\n\r\n");
bodyBuilder.Append("Requested Picture is attached.\r\n\r\n");
//add attachment
bodyBuilder.Append("--simple boundary\r\n");
bodyBuilder.Append("Content-type: image/jpg;\r\n\r\n");
var fs = new FileStream(picLoc, FileMode.Open, FileAccess.Read);
var picData = new byte[fs.Length];
fs.Read(picData, 0, picData.Length);
bodyBuilder.Append(picData);
bodyBuilder.Append("\r\n\r\n");
bodyBuilder.Append("--simple boundry--\r\n");
message.Body = bodyBuilder.ToString();
smtpClient.Send(message);
}
我收到的电子邮件最终看起来像这样:
--simple 边界 Content-type: text/plain; charset=us-ascii
附上要求的图片。
--简单边界Content-type: image/jpg;
系统字节[]
--简单的边界--
我有格式问题吗?还是缺少标题?