急求一能:收、发--邮件的组件及调用例子 100分

gyy_qiudao 2009-06-06 04:12:13

若有 请发邮箱 eno.gan.214@gmail.com

别忘加上你在csdn里的昵称,便于给你加分

非常感谢!!!!!!!!!!!!!!
...全文
103 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgf522 2009-08-27
  • 打赏
  • 举报
回复
up
gyy_qiudao 2009-06-27
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 locoasta 的回复:]
[/Quote]

非常感谢! 在你提供帮助的基础上搞定了。
locoasta 2009-06-22
  • 打赏
  • 举报
回复
Gmail用jmail收邮件是有问题的。
所以这个方法是模拟的客户端收邮件 就是根据pop3协议来的
建议你看看pop3协议你就明白了
原理就是模拟pop3的客户端发送命令(实际上jmail也是 不过是封装了)
建议看个东西 http://www.i170.com/user/catwork/Article_44059
看了你就知道了。

string mail = getMailContent(reader); //这个就是邮件的内容 包括附件等等 需要解码
这段代码就能获得邮件的内容 包括发送人 接受人 附件什么的 但是需要解码 具体怎么解码我也没做过。。
y82907966 2009-06-22
  • 打赏
  • 举报
回复
楼主能给我发个么,收发的都要..先谢谢了..
clpengmei@163.com
locoasta 2009-06-22
  • 打赏
  • 举报
回复
不好意思 我在你另外一个帖子回复过你,最近项目综合测试 所以没上CSDN,今天看了那篇帖子
我改了下代码发给你参考参考,没太多时间写全,你需要自己理解。


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Sockets;
using System.IO;
using System.Net.Security;
using System.Text;
using System.Threading;

public partial class mail_GmailTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PopClient("pop.gmail.com", 995, "locoasta1984", "****");
}

static int PopClient(string serverAddr, int port, string userID, string password)
{
TcpClient clientSocket = new TcpClient();
StreamReader reader;
StreamWriter writer;
Stream pop3Stream;
clientSocket.Connect(serverAddr, port);
pop3Stream = new SslStream(clientSocket.GetStream(), false);
((SslStream)pop3Stream).AuthenticateAsClient(serverAddr); //这句是用来客户端验证的
reader = new StreamReader(pop3Stream, Encoding.Default, true);
writer = new StreamWriter(pop3Stream);
writer.AutoFlush = true;

Read(reader);
SendCommand(writer, "USER " + userID);
Read(reader);
SendCommand(writer, "PASS " + password);
Read(reader);
SendCommand(writer, "STAT ");
Read(reader);
SendCommand(writer, "LIST ");
//Read(reader);//这里进入判断有几封邮件 截取字符串
int mailNum = getList(reader);
if (mailNum > 0)
{
for (int i = 1; i <= mailNum; i++)
{
SendCommand(writer, "retr " + i.ToString());
string mail = getMailContent(reader); //这个就是邮件的内容 包括附件等等 需要解码
}
}
SendCommand(writer, "QUIT ");
Read(reader);

reader.Close();
writer.Close();
clientSocket.Close();
return 0;
}

private static string getMailContent(StreamReader reader)
{
string strResponse = string.Empty;
while (!reader.BaseStream.CanRead)
{
Thread.Sleep(1000);//网络好的话用不到这个
}
string strLine = reader.ReadLine();
strResponse = strLine;
while (reader.Peek() > 0)
{
strResponse += reader.ReadLine();
}
return strResponse;
}

private static int getList(StreamReader reader)
{
int num = 0;
string strResponse = string.Empty;
while (!reader.BaseStream.CanRead)
{
Thread.Sleep(1000);
}
strResponse = reader.ReadLine(); ;
while (reader.Peek() > 0)
{
strResponse += reader.ReadLine();
}
num =Int32.Parse(strResponse.Substring(4, 1));
return num;
}
static bool Read(StreamReader reader)
{
string strResponse = string.Empty;
while (!reader.BaseStream.CanRead)
{
Thread.Sleep(1000);//网络好的话用不到这个
}
strResponse = reader.ReadLine();
Console.WriteLine(strResponse);
if (strResponse.Substring(0, 3) != "+OK ")
{
return true;
}
else
{
return false;
}
}
static void SendCommand(StreamWriter writer, string cmd)
{
while (!writer.BaseStream.CanWrite)
{
Thread.Sleep(1000);
}
writer.WriteLine(cmd);
writer.Flush();
return;
}
}
liucuiqiang 2009-06-20
  • 打赏
  • 举报
回复
chilkatDotNet吧,一直用这个
bao520min 2009-06-20
  • 打赏
  • 举报
回复
编程爱好者请加群88718955,大家一起探讨,学习
gan_yy 2009-06-20
  • 打赏
  • 举报
回复
发了,那是我自己写的,如果还有不明白的地方,发邮件。
gyy_qiudao 2009-06-20
  • 打赏
  • 举报
回复
up
gyy_qiudao 2009-06-20
  • 打赏
  • 举报
回复
顶呀
gyy_qiudao 2009-06-08
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 jianlanzq 的回复:]

。不知道了

不过有些邮箱…
[/Quote]

jmail里 使用pop接收 我应经设置好了 但还是一样的错
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 gyy_qiudao 的回复:]
引用 10 楼 jianlanzq 的回复:
楼主我发给你了。

试试看行不行



我引用了一个类似的 但还是出错了。

POP3Class jpop = new POP3Class();

jpop.Connect("eno.gan.214@gmail.com", "gqd_3125", "pop.gmail.com", 995);

但在调用Connet 时报了下面的“重新设置客服端” 的错

Connection reset by client.
[/Quote]

。不知道了

不过有些邮箱不支持Jmail

当时我拿QQ测试收邮件的时候,也是收不到报错。后来在QQ邮箱的设置里面把“启动JMAIL”点上对勾,才能用了
gyy_qiudao 2009-06-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jianlanzq 的回复:]
楼主我发给你了。

试试看行不行
[/Quote]

我引用了一个类似的 但还是出错了。

POP3Class jpop = new POP3Class();

jpop.Connect("eno.gan.214@gmail.com", "gqd_3125", "pop.gmail.com", 995);

但在调用Connet 时报了下面的“重新设置客服端” 的错

Connection reset by client.
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gyy_qiudao 的回复:]
引用 10 楼 jianlanzq 的回复:
楼主我发给你了。

试试看行不行



我要的是收邮件的 但还是非常感谢您了! 如果你有收邮件的 就更好了!
[/Quote]


using System.Data.SqlClient;
using faxapp.dataconn;
using faxapp.common;
using System.Net.Sockets;
using jmail;
using System.IO;

//收邮件完整代码:

public void ReceiveMails()
...{

jmail.Message Msg = new jmail.Message();
jmail.POP3 jpop = new jmail.POP3();
jmail.Attachments atts;
jmail.Attachment att;
//Mime m = new Mime();

//username为用户名,该方法通过用户名获取该用户的pop设置,即用户的POP用户名,密码,POP服务器地址以及端口号这四个参数,这四个参数是连接POP服务器的必用参数.
//SqlDataReader dataReader = this.ExtGetSetting(Username);
//if (dataReader.Read())
//{
// if (dataReader["PopServer"].ToString() != "" && dataReader["PopUsername"].ToString() != "")
// {
//连接POP服务器
MailAddress = System.Configuration.ConfigurationManager.AppSettings.Get("MailAddress");
MailFrom = System.Configuration.ConfigurationManager.AppSettings.Get("MailFrom");
POPAddress = System.Configuration.ConfigurationManager.AppSettings.Get("POPAddress");
STMPAddress = System.Configuration.ConfigurationManager.AppSettings.Get("STMPAddress");
MailUsername = System.Configuration.ConfigurationManager.AppSettings.Get("MailUsername");
MailPass = System.Configuration.ConfigurationManager.AppSettings.Get("MailPass");
Mailpassword=System.Configuration.ConfigurationManager.AppSettings.Get("Mailpassword");
string mailPopPort = System.Configuration.ConfigurationManager.AppSettings.Get("MailPopPort");
MailPopPort = Convert.ToInt32(mailPopPort);
jpop.Connect(MailUsername, Mailpassword, POPAddress, MailPopPort);
//如果服务器上有邮件
if (jpop.Count >= 1)
...{
for (int i = 1; i <= jpop.Count; i++)
...{

Msg = jpop.Messages[i];
atts = Msg.Attachments;

//取数据库中邮件信息中的最大发送时间,即最近接收到的一封邮件的时间
DataTable data = this.GetDataTable("select max(accepttime) from acceptfax where sendtag='1'");

//对服务器上的邮件的发送时间和数据库最近一封邮件的时间进行比较,如果大那么证明该邮件还未被收取,是一封新邮件,这样避免重复收取邮件入库
if (Msg.Date > Convert.ToDateTime(data.Rows[0][4].ToString()))
...{
//将这封新邮件的信息保存到数据库
//this.SaveExtMail(Msg, Username, dataReader["Email"].ToString(), jpop.GetMessageUID(i));
this.SaveExtMail(Msg.FromName, Msg.Subject, Msg.Body, Msg.Date.ToLongDateString());

//获取附件上传到服务器并且将信息存入数据库
if (atts.Count >= 1)
...{
for (int k = 0; k < atts.Count; k++)
...{

att = atts[k];//获得附件

string attname = att.Name;
try
...{

//Random TempNameInt = new Random();
//string NewMailDirName = TempNameInt.Next(100000000).ToString();
//生成随机文件后缀名
string strSaveDir = "\AttachFiles\";
//取得文件名(抱括路径)里最后一个"."的索引
int intExt = attname.LastIndexOf(".");
//取得文件扩展名
string strExt = attname.Substring(intExt);
//取得文件名(不包括路径)
Random objRand = new Random();
System.DateTime date = DateTime.Now;
//生成随机文件名
string str = attname.Substring(1, attname.LastIndexOf(".") - 1);
string saveName = System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString() + Convert.ToString(objRand.Next(99) * 97 + 100);
string strNewName = str + "(" + saveName + ")" + strExt;//取新名字,防止重复

Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(".") + "\AttachFiles\" + strNewName);

string mailPath = strSaveDir + strNewName;

att.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(".") + mailPath);
//获取该封邮件在数据库的ID,以便和附件信息相对应,取邮件表中的最大ID即可
int mailID = this.GetMailID();
/**/////将附件信息存入数据库
this.AttExtSend(mailID, attname, att.Size, mailPath, Msg.From);
}
catch (Exception ex)
...{
throw new Exception(ex.Message);
}

}
}
}
}
}

//删除服务器上的邮件
jpop.DeleteMessages();
//断开连接
jpop.Disconnect();
// }

//}
}

//取数据库中邮件某个条件下的某条信息
public DataTable GetDataTable(string sqlstr)
...{
SqlConnection conn = sqlconn.CreateConn();
SqlDataAdapter da = new SqlDataAdapter(sqlstr , conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
dt.AcceptChanges();
conn.Close();
return dt;
}

//将新邮件的信息保存到数据库acceptfax表中
public void SaveExtMail(string FromName, string Subject, string Body, string Date)
...{
//-----------------------更新到数据库中-------------------------
SqlConnection conn = sqlconn.CreateConn();
SqlDataAdapter da = new SqlDataAdapter("select * from acceptfax", conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];

DataRow dr = dt.NewRow();
dr["faxno"] = FromName;
dr["title"] = Subject;
dr["content"] = Body;
dr["accepttime"] = Date;
dt.Rows.Add(dr);
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
da.Update(dt);
dt.AcceptChanges();
conn.Close();
}

//获取该邮件的在数据库中的ID号,便与和附件相对应
public int GetMailID()
...{
//取数据库中邮件信息中的最大发送时间,即最近接收到的一封邮件的时间,得到该邮件sid号
DataTable data = this.GetDataTable("select max(accepttime) from acceptfax where sendtag='1'");
int result = Convert.ToInt32(data.Rows[0][0].ToString());
return result;
}


//把每个附件的内容更新入数据库
public void AttExtSend(int mailID,string attname,int Size,string mailpath,string format)
...{
//-----------------------更新到数据库中-------------------------
SqlConnection conn = sqlconn.CreateConn();
SqlDataAdapter da = new SqlDataAdapter("select * from attachmentInfo", conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];

DataRow dr = dt.NewRow();
dr["mailID"] = mailID;
dr["attname"] = attname;
dr["attsize"] = Size;
dr["mailpath"] = mailpath;
dr["format"] = format;
dt.Rows.Add(dr);
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
da.Update(dt);
dt.AcceptChanges();
conn.Close();
}



dll文件不是发给你了么,试试这个
gyy_qiudao 2009-06-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jianlanzq 的回复:]
楼主我发给你了。

试试看行不行
[/Quote]

我要的是收邮件的 但还是非常感谢您了! 如果你有收邮件的 就更好了!
  • 打赏
  • 举报
回复
楼主我发给你了。

试试看行不行
gyy_qiudao 2009-06-08
  • 打赏
  • 举报
回复
up
gyy_qiudao 2009-06-08
  • 打赏
  • 举报
回复
gyy_qiudao 2009-06-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhouxingyu_kingstar 的回复:]
这网站的应该你需要

http://www.emailarchitect.net/webapp/downloads.asp

应为我用的VC6.0,但是我下了,里面有例子,看了还挺好,希望是需要的
[/Quote]
我看了一个 比较难改呀
zhouxingyu896 2009-06-06
  • 打赏
  • 举报
回复
这网站的应该你需要

http://www.emailarchitect.net/webapp/downloads.asp

应为我用的VC6.0,但是我下了,里面有例子,看了还挺好,希望是需要的
加载更多回复(4)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧