如何使用imapclient从C#中的gmail读取电子邮件时跳过字符

时间:2019-02-08 07:05:46

标签: c# gmail-imap

我正在使用c#编写批处理工作,并从我的Gmail帐户读取电子邮件并将数据存储到数据库中。我正在使用HigLabo.Net.MailImapClient来阅读电子邮件。我的一种电子邮件格式包含“&”字符,因此在“&”字符之后,它的读取方式类似于HTML或其他我不知道的内容。因此,我无法正确接收电子邮件。谁能帮我解决这个问题?

抱歉,我无法显示整个电子邮件,电子邮件看起来像这样

“在L&T Raintree Boulevard的班加罗尔Hebbal出售的半家具3BHK公寓。”

在阅读时会转换为-

  

“位于班加罗尔希巴的半家具式3BHK公寓待售   Lcolor:#010101;宽度:380px ;; line-height:18px ..........“

由于某些安全隐患,我无法在此处显示整个代码。.代码看起来像这样

using (ImapClient client = new ImapClient("imap.gmail.com"))
        {
            client.Port = 993;
            client.Ssl = true;


            client.UserName = "email";
            client.Password = "password";


            Console.WriteLine("Connecting Mail Server.......");
            Console.WriteLine("Please wait while connecting..........");
            try
            {
                var flag = client.Authenticate();
                if (flag == true)
                {
                    var AllFolder = client.GetAllFolders();

                    MailMessage msg;

                    //Select folder
                    ImapFolder folder = client.SelectFolder("INBOX");

                    // ImapFolder folder = client.SelectFolder("Folder Name");

                    //Search Unread
                    SearchResult list = client.ExecuteSearch("UNSEEN UNDELETED");
                    if (list.MailIndexList.Count > 0)
                    {
                        //Get all unread mail
                        for (int i = list.MailIndexList.Count - 1; i >= 0; i--)
                        {

                            msg = client.GetMessage(list.MailIndexList[i]);
                            bool b = msg.IsHtml;
                            StoreEmailToDatabse(msg);
                            // break;
                        }
                    }




public static void StoreEmailToDatabse(MailMessage msg)
        {
            sender = msg.From.ToString();

        subject = msg.Subject.ToString();
        edate = msg.Date.Day + "-" + msg.Date.Month + "-" + msg.Date.Year;
        body = msg.BodyText.ToString();

        //int index = body.IndexOf("&");
        //body = .Skip(index);
        string propId = "";
        string name = "";
        string email = "";
        string phone = "";
        string message = "";
        string message1 = "";
        string livservLink = "";
        int p;

        try
        {
            // MagicBricks
            if (sender.Contains("@magicbricks.com"))
            {
                if (subject.Contains("Potential Lead :"))
                {
                    // Prop Id
                    body = msg.BodyText.ToString().Replace("\r\n", "").Replace("\t", "");
                    body.ToCharArray();

                    propId = "";
                    p = body.IndexOf("ID:") + 3;
                    for (int i = p; i <= body.Length - 1; i++)
                    {
                        if (body[i] != ',')
                        {

                            {
                                propId = propId + body[i];
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Name

                    name = "";
                    p = body.IndexOf("Name:") + 12;
                    for (int i = p; i <= body.Length - 1; i++)
                    {
                        if (body[i] != '<')
                        {
                            name
                            name = name + body[i];
                        }
                        else
                        {
                            break;
                        }
                    }

0 个答案:

没有答案