首页 > 开发 > 综合 > 正文

利用Jmail发送和接收邮件(C#)

2024-07-21 02:17:05
字体:
来源:转载
供稿:网友
using system;
using jmail;
using system.collections;


namespace tym.com.mymail
{
?///
?/// 邮件发送接收类
?///
?public class mail
?{
??///
??/// 邮件类的构造函数
??///
??public mail()
??{
??}

??///
??/// 邮件模型
??///
??public mailmodel model = new mailmodel();
??///
??/// 发送邮件
??///
??/// 返回值为布尔型,判断发送是否成功
??public? bool sendmail()
??{
???try
???{
????jmail.messageclass mymail = new jmail.messageclass();

????mymail.charset="gb2312";//邮件使用字符集

????mymail.from = model.from; //邮件发送者邮件地址

????mymail.fromname = model.fromname; //邮件发送者名称

????mymail.addrecipient(model.to,model.toname,"");//添加邮件接收者名称以及邮件地址
????
????mymail.subject = model.subject; //邮件主题

????mymail.body = model.body; //邮件内容

????// 判断是否有附件
????if(model.filename != "")
????{
?????mymail.addattachment(model.filename,false,model.filetype);//添加邮件附件
????}

????mymail.priority =model.priority; //邮件的紧急程度

????mymail.mailserverusername = model.mailserverusername; //登陆邮件服务器的用户名

????mymail.mailserverpassword = model.mailserverpassword; //登陆邮件服务器的密码

????return mymail.send(model.maildomain,false); //邮件服务器地址(例:smtp.163.com)
???}
???catch(exception ex)
???{
????throw new exception(this+".sendmail():"+ex.tostring());
???}
??}

??///
??/// 获取邮件列表
??///
??/// 邮件服务器用户名
??/// 邮件服务器用户密码
??/// 服务器地址
??/// 返回邮件列表
??public arraylist getmail(string username,string pwd,string server)
??{
???try
???{
????
????jmail.pop3class mail = new pop3class();

????mail.connect(username,pwd,server,110);//连接pop3服务器

????int i = mail.count;//邮件数量

????arraylist list = new arraylist();

????for(int j=0;j????{
?????jmail.messageclass m = (messageclass)mail.downloadsinglemessage(j+1);

?????list.add(m);
????}

????mail.disconnect();
????
????return list;
???}
???catch
???{
????throw new exception("您的邮箱配置信息出错!");
???}
??}
?}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表