62,047
社区成员
发帖
与我相关
我的任务
分享
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Private Function Connect(ByVal strServer As String, ByVal strUser As String, ByVal strPwd As String) As NetworkStream
Dim Sender As New TcpClient(strServer, 110)
Dim OutBytes As Byte()
Dim input As String
Dim Ns As NetworkStream = Nothing
Try
Ns = Sender.GetStream()
Dim sr As New StreamReader(Ns, System.Text.Encoding.GetEncoding("gb2312"))
input = "user " & strUser & vbCrLf
OutBytes = System.Text.Encoding.ASCII.GetBytes(input)
Ns.Write(OutBytes, 0, OutBytes.Length)
Console.WriteLine(sr.ReadLine())
input = "pass " & strPwd & vbCrLf
OutBytes = System.Text.Encoding.ASCII.GetBytes(input)
Ns.Write(OutBytes, 0, OutBytes.Length)
Console.WriteLine(sr.ReadLine())
Console.WriteLine("==========================================================")
Return Ns
Catch ex As Exception
MsgBox("无法连接到服务器", MsgBoxStyle.Critical)
Return Ns
End Try
End Function
Private Function GetNumberOfNewMessages() As Integer
Dim OutBytes As Byte()
Dim input As String
Dim strTemp As String
GetNumberOfNewMessages = -1
Try
Dim Ns As NetworkStream = Connect("pop3.163.com", "username", "password") '这里输入你自己的用户名和密码
If (Ns Is Nothing) Then Exit Function
Dim sr As New StreamReader(Ns, System.Text.Encoding.GetEncoding("gb2312"))
input = "stat" & vbCrLf
OutBytes = System.Text.Encoding.ASCII.GetBytes(input)
Ns.Write(OutBytes, 0, OutBytes.Length)
Dim resp As String = sr.ReadLine()
Console.WriteLine(resp)
MsgBox(resp)
Dim tokens As String() = resp.Split(" ")
input = "UIDL 3" & vbCrLf
OutBytes = System.Text.Encoding.ASCII.GetBytes(input)
Ns.Write(OutBytes, 0, OutBytes.Length)
resp = sr.ReadLine
Console.WriteLine(resp)
MsgBox("Uidl:" & resp)
input = "RETR 3" & vbCrLf
OutBytes = System.Text.Encoding.ASCII.GetBytes(input)
RichTextBox1.Text = "请稍候.."
RichTextBox1.Refresh()
Dim wenzhou As String = ""
Do
Ns.Write(OutBytes, 0, OutBytes.Length)
Console.WriteLine(resp)
resp = sr.ReadLine
wenzhou &= resp & vbCrLf
Loop While resp <> "."
RichTextBox1.Text = wenzhou
input = "quit" & vbCrLf
OutBytes = System.Text.Encoding.ASCII.GetBytes(input)
Ns.Write(OutBytes, 0, OutBytes.Length)
Console.WriteLine(sr.ReadLine())
sr.Close()
Ns.Close()
Return Val(tokens(1))
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intNew As Integer
intNew = GetNumberOfNewMessages()
MsgBox("有" & CStr(intNew) & "封新邮件!")
End Sub
End Class
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class Form1
{
private NetworkStream Connect(string strServer, string strUser, string strPwd)
{
TcpClient Sender = new TcpClient(strServer, 110);
byte[] OutBytes = null;
string input = null;
NetworkStream Ns = null;
try {
Ns = Sender.GetStream();
StreamReader sr = new StreamReader(Ns, System.Text.Encoding.GetEncoding("gb2312"));
input = "user " + strUser + Constants.vbCrLf;
OutBytes = System.Text.Encoding.ASCII.GetBytes(input);
Ns.Write(OutBytes, 0, OutBytes.Length);
Console.WriteLine(sr.ReadLine());
input = "pass " + strPwd + Constants.vbCrLf;
OutBytes = System.Text.Encoding.ASCII.GetBytes(input);
Ns.Write(OutBytes, 0, OutBytes.Length);
Console.WriteLine(sr.ReadLine());
Console.WriteLine("==========================================================");
return Ns;
} catch (Exception ex) {
Interaction.MsgBox("无法连接到服务器", MsgBoxStyle.Critical);
return Ns;
}
}
private int GetNumberOfNewMessages()
{
int functionReturnValue = 0;
byte[] OutBytes = null;
string input = null;
string strTemp = null;
functionReturnValue = -1;
try {
NetworkStream Ns = Connect("pop3.163.com", "username", "password");
//这里输入你自己的用户名和密码
if ((Ns == null))
return functionReturnValue;
StreamReader sr = new StreamReader(Ns, System.Text.Encoding.GetEncoding("gb2312"));
input = "stat" + Constants.vbCrLf;
OutBytes = System.Text.Encoding.ASCII.GetBytes(input);
Ns.Write(OutBytes, 0, OutBytes.Length);
string resp = sr.ReadLine();
Console.WriteLine(resp);
Interaction.MsgBox(resp);
string[] tokens = resp.Split(" ");
input = "UIDL 3" + Constants.vbCrLf;
OutBytes = System.Text.Encoding.ASCII.GetBytes(input);
Ns.Write(OutBytes, 0, OutBytes.Length);
resp = sr.ReadLine();
Console.WriteLine(resp);
Interaction.MsgBox("Uidl:" + resp);
input = "RETR 3" + Constants.vbCrLf;
OutBytes = System.Text.Encoding.ASCII.GetBytes(input);
RichTextBox1.Text = "请稍候..";
RichTextBox1.Refresh();
string wenzhou = "";
do {
Ns.Write(OutBytes, 0, OutBytes.Length);
Console.WriteLine(resp);
resp = sr.ReadLine();
wenzhou += resp + Constants.vbCrLf;
} while (resp != ".");
RichTextBox1.Text = wenzhou;
input = "quit" + Constants.vbCrLf;
OutBytes = System.Text.Encoding.ASCII.GetBytes(input);
Ns.Write(OutBytes, 0, OutBytes.Length);
Console.WriteLine(sr.ReadLine());
sr.Close();
Ns.Close();
return Conversion.Val(tokens[1]);
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
return functionReturnValue;
}
private void Button1_Click(System.Object sender, System.EventArgs e)
{
int intNew = 0;
intNew = GetNumberOfNewMessages();
Interaction.MsgBox("有" + Convert.ToString(intNew) + "封新邮件!");
}
}