如何在c#中将非mime消息转换为mime邮件消息

时间:2013-07-15 03:02:53

标签: c# mime

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

1 个答案:

答案 0 :(得分: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);