如何在C#Windows窗体中检索电子邮件? [英] How Can I Retrieve Email In C# Windows Form?
问题描述
i希望以C#窗体形式检索电子邮件并下载附件并将电子邮件正文和主题保存在数据库中。
这是我用来查看邮件正文的代码,但它无法下载附件。
string 响应;
string 来自 = null ;
string subject = null ;
int totmessages;
尝试
{
mailclient = new TcpClient(hostname.Text , 110 );
}
catch (SocketException)
{
status.Text = 无法连接到服务器;
return ;
}
ns = mailclient.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
response = sr.ReadLine();
sw.WriteLine( User + username.Text); // 发送用户名
sw.Flush();
response = sr.ReadLine();
if (response.Substring( 0 , 3 )== - ER)
{
status.Text = 无法登录服务器;
return ;
}
sw.WriteLine( Pass + password.Text); // 发送密码
sw.Flush();
尝试
{
response = sr.ReadLine();
}
catch (IOException)
{
status.Text = 无法登录服务器;
return ;
}
if (response.Substring( 0 , 4 )== - ERR)
{
status.Text = 无法登录服务器;
return ;
} sw.WriteLine( stat); // 发送stat命令以获取消息数量
sw.Flush();
response = sr.ReadLine();
string [] nummess = response.Split(' ');
totmessages = Convert.ToInt16(nummess [ 1 ]);
if (totmessages > 0 )
{
status.Text = 你有 + totmessages + messages;
}
其他
{
status.Text = 您没有消息;
}
for ( int i = 1 ; i < = totmessages; i ++)
{
sw.WriteLine( top + i + 0); // 读取每封邮件的标题
sw.Flush();
response = sr.ReadLine();
while ( true )
{
response = sr.ReadLine ();
if (response == 。 )
break ;
if (response.Length > 4 )
{
if (response.Substring( 0 , 5 )== From: )
来自 = response;
if (response.Substring( 0 , 8 )== 主题:)
subject = response;
}
}
messages.Items.Add(i + + 来自 + +主题);
谢谢
< blockquote>您可以使用IMAP( http://en.wikipedia.org/wiki/Internet_Message_Access_Protocol [ ^ ])或POP3(http://en.wikipedia.org/wiki/Post_Office_Protocol [ ^ ])下载电子邮件的协议。
免费解决方案:
使用POP3和C#下载和解析你的邮件。 [ ^ ]
https://code.msdn.microsoft.com/windowsdesktop/Simple-IMAP-CLIENT- b249d2e6 [ ^ ]
专业解决方案:
http://www.limilabs.com/mail [ ^ ]
插入数据库的帮助:
http://www.c-sharpcorner.com/UploadFile/5d065a/insert-data- in-database-in-window-form / [ ^ ]
我将为您提供解决方案!
这是我的问题:为什么这是功能不起作用? (BASIC JAVASCRIPT) [ ^ ]
Hi,
i want to retrieve email in C# windows form and download their attachment and save email body and subject in database.
this is code i used to see the messages body but it cant download attachment.
string response;
string from = null;
string subject = null;
int totmessages;
try
{
mailclient = new TcpClient(hostname.Text, 110);
}
catch (SocketException)
{
status.Text = "Unable to connect to server";
return;
}
ns = mailclient.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
response = sr.ReadLine();
sw.WriteLine("User " + username.Text); //Send username
sw.Flush();
response = sr.ReadLine();
if (response.Substring(0, 3) == "-ER")
{
status.Text = "Unable to log into server";
return;
}
sw.WriteLine("Pass " + password.Text); //Send password
sw.Flush();
try
{
response = sr.ReadLine();
}
catch (IOException)
{
status.Text = "Unable to log into server";
return;
}
if (response.Substring(0, 4) == "-ERR")
{
status.Text = "Unable to log into server";
return;
} sw.WriteLine("stat"); //Send stat command to getnumber of messages
sw.Flush();
response = sr.ReadLine();
string[] nummess = response.Split(' ');
totmessages = Convert.ToInt16(nummess[1]);
if (totmessages > 0)
{
status.Text = "you have " + totmessages + " messages";
}
else
{
status.Text = "You have no messages";
}
for (int i = 1; i <= totmessages; i++)
{
sw.WriteLine("top " + i + " 0"); //read header of each message
sw.Flush();
response = sr.ReadLine();
while (true)
{
response = sr.ReadLine();
if (response == ".")
break;
if (response.Length > 4)
{
if (response.Substring(0, 5) == "From:")
from = response;
if (response.Substring(0, 8) == "Subject:")
subject = response;
}
}
messages.Items.Add(i + " " + from + " " + subject);
thanks
You can use IMAP(http://en.wikipedia.org/wiki/Internet_Message_Access_Protocol[^]) or POP3(http://en.wikipedia.org/wiki/Post_Office_Protocol[^]) protocol to download emails.
Free solutions:
Using POP3 with C# to download and parse your mail.[^]
https://code.msdn.microsoft.com/windowsdesktop/Simple-IMAP-CLIENT-b249d2e6[^]
Professional solution:
http://www.limilabs.com/mail[^]
Help for insertion into the database:
http://www.c-sharpcorner.com/UploadFile/5d065a/insert-data-into-database-in-window-form/[^]
I'll trade you solutions!
Here's my question: Why is this function not working? (BASIC JAVASCRIPT)[^]
这篇关于如何在C#Windows窗体中检索电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!