如何将电子邮件正文保存到文本文件C#中 [英] How to save email body into text file C#

查看:181
本文介绍了如何将电子邮件正文保存到文本文件C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将收件箱电子邮件复制到文本文件,然后在此文本文件中搜索单词。如果这个单词存在,我将使用这个词从db查询email-id,最后将此邮件转发到此email-id。我的问题是我无法将电子邮件复制到文本文件中。



MSG变量始终为空。



我尝试过:



I'm trying to copy my inbox email to text file then search word in this text file. if this word exist i will use this word to query email-id from db finally forward this mail to this email-id. my problem is i m not able to copy email into text file .

the MSG variable is always empty.

What I have tried:

<pre>using (Pop3Client client = new Pop3Client())
{
    client.Connect("pop.gmail.com", 995, true);
    client.Authenticate("mymail@gmail.com", "pasword", AuthenticationMethod.UsernameAndPassword);

    // Get the number of messages in the inbox
    int messageCount = client.GetMessageCount();

    for (int i = messageCount; i > 0; i--)
    {
        string msg = client.GetMessage(i).MessagePart.GetBodyAsText().ToString();

        System.IO.File.WriteAllText(@"d:\\My File2.log", msg.ToString());
        var body = System.IO.File.ReadLines(@"d:\\My File2.log");
        if (body.Contains("cusotmer ID: X"))
        {
            getemailadress();
            sendemail();
        }
    }
}

推荐答案

首先要注意的是你正在呼叫<循环中的code> File.WriteAllText - 该方法将始终覆盖任何现有的文件内容 - 因此在循环之后,您将始终只获得您处理的最终消息。

第二个是看到你在一个字符串上调用Tostring - 它什么都不做。

最后,你需要使用调试器来找出这里发生了什么:我们不能为你做到这一点!首先在行上放置一个断点:

The first thing to note is that you are calling File.WriteAllText inside a loop - and that method will always overwrite any existing file content - so after the loop you will always get only the final message that you process.
Second is to see that you are calling Tostring on a string - which does nothing.
Finally, you need to use the debugger to find out what is going on here: we can't do that for you! Start by putting a breakpoint on the line:
int messageCount = client.GetMessageCount();

并运行您的代码。当它停止时,单步该行,并查看计数 - 它是零吗?如果是,那就是原因。如果没有,继续单步执行并查看每个阶段返回的确切内容。如果你这样做,问题就应该是相当明显的,那应该告诉你从哪里开始寻找修复它。

And run your code. when it stops, single step the line, and look at the count - is it zero? If it is, that's the reason. If not, continue single stepping and look at exactly what is being returned at each stage. It should be fairly obvious where the problem is if you do that, and that should tell you where to start looking to fix it.


这篇关于如何将电子邮件正文保存到文本文件C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆