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.
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.
Where should I store my data?
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.
Related
I'm trying to print a file with C#. I have made some headway in listing off all the printers and then wrote some simple logic to select the correct printer:
var server = new PrintServer();
var queues = server.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections }).ToList();
int count = 0;
foreach (var q in queues)
{
Console.WriteLine(count++ + " " + q.Name);
}
int iSelection = 0;
while (true)
{
Console.Write("Select printer: ");
string selection = Console.ReadLine();
if (int.TryParse(selection, out iSelection) && iSelection >= 0 && iSelection < queues.Count())
{
break;
}
else
{
Console.WriteLine("Bad selection, try again.");
}
}
The next step, as for the posts I've seen on this site, is that you need to select the specific queue, and then add a job, grab the job stream, and write to the stream (at least that's how I want to try to do it, unless it's wrong?)
var queue = queues[iSelection];
var job = queue.AddJob(#".\Test.txt");
var stream = job.JobStream;
var file = File.ReadBytes(#".\Test.txt");
stream.Write(file, 0, file.Length);
When I do this, the program crashes at the line with AddJob. Specifically,
System.ArgumentNullException: 'Value cannot be null, Parameter name: printingHandler'
Now, I think I understand what the issue is. I had been playing with System.Drawing.Printing.PrintDocument yesterday, but I am trying to find a solution that allows me to print files, rather than manually draw them out and them print them. Ultimately, the goal in the future is to be able to print out text and PDF files (I was hoping that this solution would allow me to open a PDF file and dump the bytes into this stream, but I don't know if that's the correct way to this?)
Anyway, the exception I got I think is something similar to PrintDocument's PrintPageEventHandler, I need to add a callback to the PrintQueue somehow that tells it the font, color, font size, etc. Problem is that I see nothing for PrintQueue that allows me to add a handle for it to fix this issue.
What can I do to fix this exception?
I was having this issue as well. Eventually I found out that we need to call into Refresh() of the selected PrintQueue instance and before calling AddJob().
using following code i have reading msg from my hotmail account . But sometimes the following error coming . -ERR Exceeded the login limit for a 15 minute period. Reduce the frequency of requests to the POP3 server . can anyone tell me whats the reason for this ? Is that server problem or anything else ? other than pop3 anyother protocol can we use for hotmail?
public string hotmail(string username, string password)
{
string result = "";
string str = string.Empty;
string strTemp = string.Empty;
try
{
TcpClient tcpclient = new TcpClient();
tcpclient.Connect("pop3.live.com", 995);
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
sslstream.AuthenticateAsClient("pop3.live.com");
System.IO.StreamWriter sw = new StreamWriter(sslstream);
System.IO.StreamReader reader = new StreamReader(sslstream);
strTemp = reader.ReadLine();
sw.WriteLine("USER" + " " + username);
sw.Flush();
strTemp = reader.ReadLine();
sw.WriteLine("PASS" + " " + password);
sw.Flush();
strTemp = reader.ReadLine();
string[] numbers = Regex.Split(strTemp, #"\D+");
int a = 0;
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
int i = int.Parse(value);
numbers[a] = i.ToString();
a++;
}
}
sw.WriteLine("RETR" + " " + numbers[0]);
sw.Flush();
strTemp = reader.ReadLine();
while ((strTemp = reader.ReadLine()) != null)
{
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
sw.WriteLine("Quit ");
sw.Flush();
result = str;
return result;
}
Catch ( Exception ex)
{}
return result;
}
thanks in advance ..
Any other protocol you can use? Yes, hotmail/outlook.com now supports IMAP.
But the issue with the code here seems to be that you're creating a new TcpClient every time you run this. If you're running it many times in in a row, Outlook.com/Hotmail will eventually complain. It's as if you've got tons of clients from a single source connecting to their server, which is, when it's not testing code, often a sign of email abuse.
TcpClient tcpclient = new TcpClient(); // Hello, new.
tcpclient.Connect("pop3.live.com", 995);
If you've got a lot to do on the server, keep a single connection active longer, and close it up when you're done.
Every time you run the code in your question, you're creating (and not tcpclient.Close()-ing) a connection to pop3.live.com. I usually only get this error when I've had a lot of connections that don't close properly due to errors when I'm messing with my code.
MSDN actually has a decent example for TcpClient, but you might be more interested in another example from SO here. Check out how it uses using, and nests a loop inside.
using (TcpClient client = new TcpClient())
{
client.Connect("pop3.live.com", 995);
while(variableThatRepresentsRunning)
{
// talk to POP server
}
}
By the way, the best advice I can give here is to tell you not to reinvent the wheel (unless you're just having fun playing with the POP server. Throwing commands via TCP can be lots of fun, especially with IMAP).
OpenPop.NET is a great library to handle POP requests in C#, includes a good MIME parser, and, if you're still working on this, should speed you along quite a bit. Its examples page is excellent.
Go to the mail inbox , you may get mail regarding this and accept it. Otherwise Try to give the request after some time. Because google having some restriction to read mail using pop settings.
I am creating a file on ftp server. But Before creating file on server I also check that it does not existing already. However, It is working fine most of the system but one of my client has problem. When he run the application, it throws the system.formatexception i-e input string is not in correct format.
I am unable to understand this problem. Can anybody help me?
The following is the code to create file.
public string createFile(string filename1)
{
StreamWriter sw1 = null;
System.Net.FtpWebRequest tmpReq1;
try
{
tmpReq1 = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create("ftp://ftp.dunyameri.com/pt/" + filename1);
tmpReq1.Credentials = new System.Net.NetworkCredential("naveed#dunyameri.com", "xxxxx");
FtpWebResponse response = (FtpWebResponse)tmpReq1.GetResponse();
}
catch (WebException ex)
{
FtpWebResponse response2 = (FtpWebResponse)ex.Response;
if (response2.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
// I am creating file here
}
else
{
return ex.ToString();
}
}
return "File Created";
}
I haves searched on internet that it might be because of string contain 0 or dots. In this particular system case the file name contain dots and 0. Is it because of this type of file name?
Thanks,
Naveed
It seems that the error does not occur within "createFile(string filename1)". If so, the stack should be similar to this:
...
System.Convert.ToInt32(String value)
e2erta.e2erta1.YourFtpClass.createFile(string filename1) <- I would expect this line!
e2erta.e2erta1..ctor()
My best guess would be that filename1 starts with a / character. You can use the Path.Combine method to handle this case:
var path = Path.Combine("ftp://ftp.dunyameri.com/pt/", filename1);
var tmpReq1 = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(path);
...
Url should be something like "ftp://" + userName + ":" + password + "#" + serverAddress + ":" + serverPort + "/" + file.
Try to add port.
The class FtpWebRequest does not have a Create method seelink http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest_methods
The example shown on link casts WebRequest.Create to FtpWebRequest this might be the issue
Wondering how to best deal with a problem I am having with xsltransform. Long story short, everything works in my test environment, but it crashes when I run it on the server due to the filenames it tries to deal with, which are output from another program, over which I have no control.
For example. "4Copy (2) of Fed_Around_Six__TFVC020-12.mov.xml" a simple # would solve this, but it's actually running on a service, and this service gets all files of that type in the directory and processes them one by one.
string[] filepaths = Directory.GetFiles(path, Filetype);
I keep the file name variable in:
FileInfo f = new FileInfo(filepaths[i]);
But the method I use for the transform:
myXslTransform = new XslCompiledTransform();
myXslTransform.Transform(filename,OutputFileName);
Only accepts (String, String) and thus when it sees "4Copy (2) of Fed_Around_Six__TFVC020-12.mov.xml" it has a heart attack and cuts it off.
I was thinking save the original name, rename, remove whitespace, transform, and rename back. But I think there is a smarter way to handle it out there, just not sure where to look. Is there a way of telling C# to handle a variable as a literal? Or a different transform method that accepts these weird filenames with very bad naming conventions?
Any insight that helps would be great!
The error & exception message I recieve from the Eventvwr is
Cannot Translate
\\9g031\Export\4Copy (2) of Fed_Around_Six__TFVC020-12.mov.xml
OutputName = \\9g031\Export\done\4Copy (2) of Fed_Around_Six__TFVC020-12.mov.xml
XSL LOC = C:\CXS.xsl
System.IO.IOException: The specified path is invalid.
private void PreformTranslation(FileInfo FileName, String OutputFileName , String result)
{
try
{
XslCompiledTransform myXslTransform;
myXslTransform = new XslCompiledTransform();
myXslTransform.Load(XSLname);
EventLog.WriteEntry(FileName.ToString(), OutputFileName);
myXslTransform.Transform(FileName.Name,OutputFileName);
EventLog.WriteEntry("TranslationComplete");
if (File.Exists(path + result))
{
MoveVideoFiles(path + result, outputPath + result);
}
// Rename(OutputFileName, FileName, Out);
}
catch (Exception e)
{
EventLog.WriteEntry("Cannot Translate " + FileName + " OutputName = " + OutputFileName + " \r\n"+
"XSL LOC = " + XSLname + "\r\n" + e);
}
}
The default directory when running a service is something like "windows/system32" and this isn't the directory of the executable.
This is probably the reason the XML file isn't found.
Iv'e recently started a new job as an ICT Technician and im creating an Console application which will consists of stuff that will help our daily tools!
My first tool is a Network Scanner, Our system currently runs on Vanilla and Asset tags but the only way we can find the hostname / ip address is by going into the Windows Console tools and nslookup which to me can be improved
I want to create an application in which I enter a 6 digit number and the application will search the whole DNS for a possible match!
Our hostsnames are like so
ICTLN-D006609-edw.srv.internal the d 006609 would be the asset tag for that computer.
I wish to enter that into the Console Application and it search through every hostname and the ones that contain the entered asset tag within the string will be returned along with an ip and full computer name ready for VNC / Remote Desktop.
Firstly how would I go about building this, shall i start the project of as a console app or a WPF. can you provide an example of how I can scan the hostnames via C#, or if there's an opensource C# version can you provide a link.
Any information would be a great help as it will take out alot of issues in the workpalce as we have to ask the customer to go into there My Computer adn properties etc and then read the Computer name back to use which I find pointless.
Regards.
Updates:
*1 C# Version I made: http://pastebin.com/wBWxyyuh
I would actually go about this with PowerShell, since automating tasks is kinda its thing. In fact, here's a PowerShell script to list out all computers visible on the network. This is easily translatable into C# if you really want it there instead.
function Find-Computer( [string]$assetTag ) {
$searcher = New-Object System.DirectoryServices.DirectorySearcher;
$searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry;
$searcher.SearchScope = 'Subtree';
$searcher.PageSize = 1000;
$searcher.Filter = '(objectCategory=computer)';
$results = $searcher.FindAll();
$computers = #();
foreach($result in $results) {
$computers += $result.GetDirectoryEntry();
}
$results.Dispose(); #Explicitly needed to free resources.
$computers |? { $_.Name -match $assetTag }
}
Here's a way you can accomplish this, although it's not the best. You might consider hitting Active Directory to find the legitimate machines on your network. The code below shows how you might resolve a machine name, and shows how to ping it:
static void Main()
{
for (int index = 0; index < 999999; index++)
{
string computerName = string.Format("ICTLN-D{0:000000}-edw.srv.internal", index);
string fqdn = computerName;
try
{
fqdn = Dns.GetHostEntry(computerName).HostName;
}
catch (SocketException exception)
{
Console.WriteLine(">>Computer not found: " + computerName + " - " + exception.Message);
}
using (Ping ping = new Ping())
{
PingReply reply = ping.Send(fqdn);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine(">>Computer is alive: " + computerName);
}
else
{
Console.WriteLine(">>Computer did not respond to ping: " + computerName);
}
}
}
}
Hope that helps...