我收到不是 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();