The code I am using at the moment is
try
{
//using MailKit.Net.Pop3;
string pathLog = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
string fileNameLog = Path.Combine(pathLog, "pop3.txt");
using (var client = new Pop3Client(new ProtocolLogger(fileNameLog)))
//using (var client = new Pop3Client(new ProtocolLogger("pop3.log")))
{
strProgress = strProgress "c";
client.Connect("outlook.office365.com", 995, SecureSocketOptions.SslOnConnect); //POP3 port is 995
strProgress = strProgress "d";
client.Authenticate("xxx", "yyy");
strProgress = strProgress "e";
for (int i = 0; i < client.Count; i )
{
strProgress = strProgress "f";
var message = client.GetMessage(i);
strProgress = strProgress "g";
//Write the message to a file
message.WriteTo(string.Format("{0}.msg", i)); //<<<<<<<<<error here
numCountEmailsDownloaded = numCountEmailsDownloaded 1;
}
client.Disconnect(true);
}
}
catch (Exception ex)
{
Toast.MakeText(this, ex.Message "\n\nStopped after " strProgress, ToastLength.Long).Show();
}
I get an error message which says 'Access to the path "/0.msg" is denied. Stopped after abcdefg (I use strProgress to mark where the error is).
I've tried getting the email body as a string but the error message says 'Do not use string to serialize ...use WriteTo instead', except that it doesn't tell me what to do with it even if it worked. The idea of receiving emails and then wanting to know what they say must be a common one, but many other questions and answers on the web seem to think it isn't worth actually spelling out. How can I extract the email body text from the above? Thank you.
CodePudding user response:
You are trying to write the message to a file in the current working directory. This will most likely be the application's path.
Applications are generally installed under the Program Files
folder. Regular users do not have permission to write to that folder.
You will need to use a different path to store your files. For example, somewhere under the LocalApplicationData
folder which you're using for your log files.
CodePudding user response:
I've tried getting the email body as a string
If that's what you really want, maybe replace the line with this:
string messageBody = message.GetTextBody(MimeKit.Text.TextFormat.Plain);
as per the docs for the MimeMessage type. See more options for the TextFormat here:
http://www.mimekit.net/docs/html/T_MimeKit_Text_TextFormat.htm
This may also work:
string messageBody = message.TextBody;
In both cases this is looking for the Text
option within a MIME message. It's somewhat common these days to find messages where the text format doesn't exist, and the message only includes an HTML content body. In that situation you need to know how to check both the text and fallback to HTML (or vice versa).
Otherwise, answers and comments suggesting file permissions sound right on the mark.