如何将MailMessage对象保存到磁盘?MailMessage对象不公开任何Save()方法。
如果它以任何格式保存,*。eml或* .msg,我都没有问题。任何想法如何做到这一点?
如何将MailMessage对象保存到磁盘?MailMessage对象不公开任何Save()方法。
如果它以任何格式保存,*。eml或* .msg,我都没有问题。任何想法如何做到这一点?
Answers:
为了简单起见,我只引用Connect项目的说明:
您实际上可以将SmtpClient配置为将电子邮件发送到文件系统而不是网络。您可以使用以下代码以编程方式执行此操作:
SmtpClient client = new SmtpClient("mysmtphost"); client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; client.PickupDirectoryLocation = @"C:\somedirectory"; client.Send(message);
您还可以像这样在应用程序配置文件中进行设置:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
</smtp>
</mailSettings>
</system.net>
</configuration>
发送电子邮件后,您应该会看到电子邮件文件已添加到您指定的目录中。然后,您可以让一个单独的过程以批处理方式发送电子邮件。
您应该能够使用空的构造函数而不是列出的构造函数,因为它无论如何都不会发送它。
这是将MailMessage转换为包含EML数据的流的扩展方法。它使用文件系统时显然有点hack,但是它可以工作。
public static void SaveMailMessage(this MailMessage msg, string filePath)
{
using (var fs = new FileStream(filePath, FileMode.Create))
{
msg.ToEMLStream(fs);
}
}
/// <summary>
/// Converts a MailMessage to an EML file stream.
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static void ToEMLStream(this MailMessage msg, Stream str)
{
using (var client = new SmtpClient())
{
var id = Guid.NewGuid();
var tempFolder = Path.Combine(Path.GetTempPath(), Assembly.GetExecutingAssembly().GetName().Name);
tempFolder = Path.Combine(tempFolder, "MailMessageToEMLTemp");
// create a temp folder to hold just this .eml file so that we can find it easily.
tempFolder = Path.Combine(tempFolder, id.ToString());
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = tempFolder;
client.Send(msg);
// tempFolder should contain 1 eml file
var filePath = Directory.GetFiles(tempFolder).Single();
// stream out the contents
using (var fs = new FileStream(filePath, FileMode.Open))
{
fs.CopyTo(str);
}
if (Directory.Exists(tempFolder))
{
Directory.Delete(tempFolder, true);
}
}
}
然后,您可以获取返回的流并根据需要进行处理,包括保存到磁盘上的另一个位置或存储在数据库字段中,甚至通过电子邮件发送附件。
由于一个或另一个原因,client.send失败(在使用该方法实际发送之后),因此我插入了良好的ole CDO和ADODB流。在设置.Message值之前,我还必须使用template.eml加载CDO.message。但这有效。
由于上面的是C,这里是VB的一个
MyMessage.From = New Net.Mail.MailAddress(mEmailAddress)
MyMessage.To.Add(mToAddress)
MyMessage.Subject = mSubject
MyMessage.Body = mBody
Smtp.Host = "------"
Smtp.Port = "2525"
Smtp.Credentials = New NetworkCredential(------)
Smtp.Send(MyMessage) ' Actual Send
Dim oldCDO As CDO.Message
oldCDO = MyLoadEmlFromFile("template.eml") ' just put from, to, subject blank. leave first line blank
oldCDO.To = mToAddress
oldCDO.From = mEmailAddress
oldCDO.Subject = mSubject
oldCDO.TextBody = mBody
oldCDO.HTMLBody = mBody
oldCDO.GetStream.Flush()
oldCDO.GetStream.SaveToFile(yourPath)
试试这个
请使用这两个参考(使用MailBee;)(使用MailBee.Mime;)
public static string load(string to,string from,string cc,string bcc,string subject,string body, List<string> reportList,string path, bool HtmlbodyType)
{
try
{
MailBee.Mime.MailMessage msg = new MailBee.Mime.MailMessage();
msg.From.AsString = from;
msg.Subject = subject;
if (HtmlbodyType == true)
{
msg.BodyHtmlText = body;
}
else
{
msg.BodyPlainText = body;
}
string[] receptionEmail = to.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
string[] ccEmail = cc.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
string[] bccEmail = bcc.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
string message = "";
foreach (string To in receptionEmail)
{
msg.To.Add(To);
}
foreach (string CC in ccEmail)
{
msg.Cc.Add(CC);
}
foreach (string Bcc in bccEmail)
{
msg.Bcc.Add(Bcc);
}
for (int x = 0; x < reportList.Count; x++)
{
string fileName = reportList[x];
msg.Attachments.Add(fileName);
}
msg.SaveMessage(path);
return "Success";
}
catch (Exception ex)
{
return ex.Message;
}
}