以编程方式阅读邮件 [英] Reading email programmatically
问题描述
嗨朋友们,
我第一次发布我的问题,
Iam尝试使用IMAP设置读取电子邮件设置它是console
使用 System.Collections.Generic;
使用 System.Text;
命名空间 EmailConsole
{
class Program
{
static System.IO.StreamWriter sw = null ;
static System.Net.Sockets.TcpClient tcpc = null ;
static System.Net.Security.SslStream ssl = null ;
static string 用户名,密码;
static string 路径;
static int bytes = -1;
static byte [] buffer;
static StringBuilder sb = new StringBuilder();
static byte [] dummy;
静态 void Main( string [] args)
{
try
{
// path = Environment.CurrentDirectory +\\emailresponse.txt;
path = @ D:\ emailresponse.txt;
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
sw = new System.IO.StreamWriter(System.IO.File.Create(path));
tcpc = new System.Net.Sockets.TcpClient( ,);
ssl = new System.Net.Security.SslStream(tcpc.GetStream());
ssl.AuthenticateAsClient( outlook.office365.com);
receiveResponse( );
Console.WriteLine( username:);
username = Console.ReadLine();
Console.WriteLine( password:);
password = Console.ReadLine();
receiveResponse( $ LOGIN + username + + password + \\ \\r\\\
跨度>);
Console.Clear();
receiveResponse( $ LIST + \\ + \* \ + \\\\ n 跨度>);
receiveResponse( $ SELECT INBOX \\\\ n) ;
receiveResponse( $ STATUS INBOX(MESSAGES)\\\\ n跨度>);
Console.WriteLine( 输入要获取的电子邮件号码:< /跨度>);
int number = int .Parse(Console.ReadLine());
receiveResponse( $ FETCH + number + body [header] \\\\ n);
receiveResponse( $ FETCH + number + body [text] \\\\ nn);
receiveResponse( $ LOGOUT \\\\ n跨度>);
}
catch (例外情况)
{
Console.WriteLine( 错误: + ex.Message);
}
最后
{
如果(sw != null )
{
// sw.Close();
// sw.Dispose();
}
if (ssl!= null )
{
// ssl.Close();
// ssl.Dispose();
}
if (tcpc!= null )
{
// tcpc.Close();
}
}
Console.ReadKey ();
}
静态 void receiveResponse( string 命令)
{
try
{
if (command!= )
{
if (tcpc.Connected)
{
dummy = Encoding.ASCII.GetBytes(command);
ssl.Write(dummy, 0 ,dummy.Length);
}
其他
{
throw new ApplicationException( TCP CONNECTION DISCONNECTED);
}
}
ssl.Flush();
buffer = new byte [ 2048 跨度>];
bytes = ssl.Read(buffer, 0 , 2048 );
// byte [] buffer = new byte [client.ReceiveBufferSize];
sb.Append(Encoding.ASCII.GetString(buffer));
Console.WriteLine(sb.ToString());
sw.WriteLine(sb.ToString());
sb = new StringBuilder();
}
catch (例外情况)
{
throw new ApplicationException(ex.Message);
}
}
}
}
但是iam反复出现以下错误:
无法将数据写入传输连接:已建立的连接已被主机中的软件中止。
我尝试了什么:
我搜索时发现了以下想法,
最有可能由于某些底层代理系统关闭了您的连接。
您计算机上的某些内容阻止了您的连接。尝试在禁用防火墙/防病毒的情况下运行它。这不是永久性的,仅用于测试是否存在设置问题或代码问题。如果它在没有防火墙/防病毒软件的情况下通过,那么你必须找到一种允许它通过它们的方法 - 将它添加到防火墙除外等等。
问题在禁用防病毒时解决了
我尝试过这些想法,但仍然没有修复错误。请帮助我。
提前谢谢。
登录 +用户名+ +密码+ \ r \\ n \\ n);
Console.Clear();
receiveResponse(
LIST + \ \ + \* \ + \\\\ n);
receiveResponse(
SELECT INBOX \\\\ n);
receiveResponse(
Hi Friends,
For the first time iam posting my problem,
Iam trying to read Email Programmatically using IMAP setting its is console
using System.Collections.Generic; using System.Text; namespace EmailConsole { class Program { static System.IO.StreamWriter sw = null; static System.Net.Sockets.TcpClient tcpc = null; static System.Net.Security.SslStream ssl = null; static string username, password; static string path; static int bytes = -1; static byte[] buffer; static StringBuilder sb = new StringBuilder(); static byte[] dummy; static void Main(string[] args) { try { //path = Environment.CurrentDirectory + "\\emailresponse.txt"; path = @"D:\emailresponse.txt"; if (System.IO.File.Exists(path)) System.IO.File.Delete(path); sw = new System.IO.StreamWriter(System.IO.File.Create(path)); tcpc = new System.Net.Sockets.TcpClient("", ); ssl = new System.Net.Security.SslStream(tcpc.GetStream()); ssl.AuthenticateAsClient("outlook.office365.com"); receiveResponse(""); Console.WriteLine("username : "); username = Console.ReadLine(); Console.WriteLine("password : "); password = Console.ReadLine(); receiveResponse("$ LOGIN " + username + " " + password + " \r\n"); Console.Clear(); receiveResponse("$ LIST " + "\"\"" + " \"*\"" + "\r\n"); receiveResponse("$ SELECT INBOX\r\n"); receiveResponse("$ STATUS INBOX (MESSAGES)\r\n"); Console.WriteLine("enter the email number to fetch :"); int number = int.Parse(Console.ReadLine()); receiveResponse("$ FETCH " + number + " body[header]\r\n"); receiveResponse("$ FETCH " + number + " body[text]\r\n"); receiveResponse("$ LOGOUT\r\n"); } catch (Exception ex) { Console.WriteLine("error: " + ex.Message); } finally { if (sw != null) { //sw.Close(); //sw.Dispose(); } if (ssl != null) { //ssl.Close(); //ssl.Dispose(); } if (tcpc != null) { //tcpc.Close(); } } Console.ReadKey(); } static void receiveResponse(string command) { try { if (command != "") { if (tcpc.Connected) { dummy = Encoding.ASCII.GetBytes(command); ssl.Write(dummy, 0, dummy.Length); } else { throw new ApplicationException("TCP CONNECTION DISCONNECTED"); } } ssl.Flush(); buffer = new byte[2048]; bytes = ssl.Read(buffer, 0, 2048); //byte[] buffer = new byte[client.ReceiveBufferSize]; sb.Append(Encoding.ASCII.GetString(buffer)); Console.WriteLine(sb.ToString()); sw.WriteLine(sb.ToString()); sb = new StringBuilder(); } catch (Exception ex) { throw new ApplicationException(ex.Message); } } } }
But iam getting repeatedly following error :
Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.
What I have tried:
when I searched I found following ideas,
That's most likely due to some underlying proxy system closing your connection.
something on your machine is blocking your connection. Try running it with firewall/antivirus disabled. This is not permanent, just for testing if this is setup issue or code issue. If it passes without the firewall/antivirus then you have to find a way to allow it through them - add it to the firewall exceptons, etc.
problem solved when disabled the antivirus
I have tried with these ideas but still the error was not fixed. Kindly Help me.
Thanks in advance.解决方案LOGIN "+ username + " " + password + " \r\n"); Console.Clear(); receiveResponse("
LIST " + "\"\"" + " \"*\"" + "\r\n"); receiveResponse("
SELECT INBOX\r\n"); receiveResponse("
这篇关于以编程方式阅读邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!