利用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("您的郵箱配置信息出錯!");
???}
??}
?}
}