如何在 C# 中将非 mime 消息转换为 mime 邮件消息
Posted
技术标签:
【中文标题】如何在 C# 中将非 mime 消息转换为 mime 邮件消息【英文标题】:how to convert non-mime message to mime mail message in c# 【发布时间】:2013-07-12 20:09:58 【问题描述】:我收到一封不是 MIME 邮件的邮件 无法获取 Content-Type
无法获取附件
如何将此消息转换为 MIME 消息
foreach (AE.Net.Mail.Lazy<MailMessage> message in messages)
MailMessage m = message.Value;
string sender = m.From.Address;
ICollection<Attachment> cc = m.Attachments;
foreach (Attachment aa in cc)
System.IO.File.WriteAllBytes(@"C:\Users\LAB-User2\Desktop\EmailAttachments\" + aa.Filename, aa.GetData());
更新
此配置是否适用于非 mime 消息?
public string GetDisposition()
return this["Content-Disposition"]["boundary"];
string disposition = Headers.GetDisposition();
if (!string.IsNullOrEmpty(disposition))
//else this is a multipart Mime Message
using (var subreader = new StringReader(line + Environment.NewLine + reader.ReadToEnd()))
ParseMime(subreader, disposition);
else
//SetBody((line + Environment.NewLine + reader.ReadToEnd()).Trim());
我可以向自己发送非 mime 电子邮件吗?这样我就可以收到 MIME 格式的电子邮件了? 当我尝试这个时,我无法给自己发电子邮件
ImapClient imap = new ImapClient("imap.gmail.com", "hello@gmail.com", "pwd", ImapClient.AuthMethods.Login, 993, true, true);
//imap.Connect("imap.gmail.com", 993, true, true);
imap.SelectMailbox("INBOX");
//MailMessage[] mm = imap.GetMessages(imap.GetMessageCount() - 1, imap.GetMessageCount() - 10, false, false);
AE.Net.Mail.Lazy<AE.Net.Mail.MailMessage>[] messages = imap.SearchMessages(SearchCondition.Unseen(), false);
// Run through each message:
foreach (AE.Net.Mail.Lazy<AE.Net.Mail.MailMessage> message in messages)
AE.Net.Mail.MailMessage mm = message.Value;
//create the mail message
//var mail = new MailMessage();
var mail = new System.Net.Mail.MailMessage();
//set the addresses
mail.From = new MailAddress("me@mycompany.com", "demo");
mail.To.Add("hello@gmail.com");
//set the content
mail.Subject = "This is an email";
//first we create the Plain Text part
//var plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
//then we create the Html part
//var htmlView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", null, "text/html");
ICollection<AE.Net.Mail.Attachment> cc = mm.AlternateViews;
foreach (AE.Net.Mail.Attachment aa in cc)
try
System.IO.File.WriteAllBytes(@"C:\Users\LAB-User2\Desktop\EmailAttachments\" + aa.Filename, aa.GetData());
catch (Exception ex)
Console.WriteLine(ex.Message);
//mail.AlternateViews.Add(b);
var plainView = AlternateView.CreateAlternateViewFromString(mm.Raw, null, "text/plain");
mail.AlternateViews.Add(plainView);
//send the message
var smtp = new SmtpClient("smtp.gmail.com", 587); //specify the mail server address
smtp.Credentials = new System.Net.NetworkCredential("hello@gmail.com", "pwd");
smtp.EnableSsl = true;
smtp.Send(mail);
smtp = null;
mail.Dispose();
【问题讨论】:
***.com/q/1000954/34397 是否使用替代视图可以获取附件? 我更新了我的问题,处置是否适用于非 mime 消息?我不明白“哪个不是内联” 【参考方案1】:你可以使用这个sample:
//create the mail message
var mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");
//set the content
mail.Subject = "This is an email";
//first we create the Plain Text part
var plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
//then we create the Html part
var htmlView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", null, "text/html");
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
//send the message
var smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);
【讨论】:
它使用的是 AE.Net.Mail,我不明白这个示例是怎么做的 是否使用 ICollection cc = mm.AlternateViews;那么它适用于非mime消息吗?以上是关于如何在 C# 中将非 mime 消息转换为 mime 邮件消息的主要内容,如果未能解决你的问题,请参考以下文章