Click here to Skip to main content
15,920,708 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
        }
    }
}
Posted
Updated 1-May-17 3:46am
Comments
[no name] 1-May-17 8:54am    
So what issue/error you are facing on this?
Member 12140172 1-May-17 9:11am    
my issue is the text file is always empty no data in the variable msg
[no name] 1-May-17 9:12am    
Di you checked while debug if your "msg" object has values coming in...
CHill60 1-May-17 9:15am    
When you debug your code is there anything in msg for any of the iterations of i ? (By the way msg is already a string so you don't need to use msg.ToString()). Why do you start with the last message?
Why bother reading the file again? Just use
if(msg.Contains("customer ID: X"))
... and check your spelling of customer

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900