.net jMail收邮件(含写入数据库及收取附件)
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using jMailDemo2;
using jmail;
using System.IO;
namespace JmailDemo
{
public class MailAPI
{
#region 收邮件
public void ReceiveMail(string Username, string Password, string ServerS)
{
//建立接收邮件对象
jmail.POP3 popMail = new POP3Class();
try
{
//配置接收邮件
popMail.Connect(Username, Password, ServerS, Convert.ToInt32(110));
//如果收到邮件
if (popMail.Count > 0)
{
//根据取到的邮件数量依次取得每封邮件
for (int i = 1; i < popMail.Messages.Count; i++)
{
var curtMail = popMail.DownloadSingleMessage(i);
MailDBEntities db = new MailDBEntities();
jReceiveMail jReceiveMail = new jReceiveMail();
jReceiveMail.jbody = curtMail.HTMLBody;
jReceiveMail.jreceiveDate = Convert.ToDateTime(curtMail.Date);
jReceiveMail.jsendMail = curtMail.From;
string temp = "";
if (popMail.DownloadSingleMessage(i).Attachments.Count > 0)
{
for (int j = 0; j < popMail.DownloadSingleMessage(i).Attachments.Count; j++)
{
temp += curtMail.Attachments[j].Name + ";";
string root = "E:\\receiveMail\\" + DateTime.Now.ToString() + curtMail.Attachments[j].Name;
popMail.DownloadSingleMessage(i).Attachments[j].SaveToFile(root);
}
}
jReceiveMail.jreceiveMail1 = Username;
jReceiveMail.jattachMentRout = temp;
jReceiveMail.jsubject = curtMail.Subject;
jReceiveMail.jActive = "Y";
db.AddTojReceiveMail(jReceiveMail);
db.SaveChanges();
}
//return mailStator.正常接收;
}
else
{
//return mailStator.无邮件;
}
//popMail.DeleteMessages();
popMail.Disconnect();
popMail = null;
}
catch (Exception)
{
//return mailStator.接收异常;
throw;
}
}
#endregion
}
}
调用:
[csharp]
protected void Button1_Click(object sender, EventArgs e)
{
MailAPI mail = new MailAPI();
mail.ReceiveMail("aaa@qq.com", "qtt", "pop.qq.com");
}
建数据库表:
[sql]
create table userMessage (
id int identity(1,1) primary key,
userId varchar(30),
userMail nvarchar(100),
userName nvarchar(50),
userPermission nvarchar(10) null,
createDate datetime default(getdate()),
isActive nvarchar(2) default('Y')
)
create table smtpServiceList(
id int identity(1,1) primary key,
jsmtpService nvarchar(16),
jcreateDate datetime default(getdate()),
jcreateUser nvarchar(30),
jcreateUserId varchar(20),
jupdateUserId varchar(20) null,
jupdateUser varchar(30) null,
isActive nvarchar(2) default('Y')
)
create table jSendMail(
id int identity(1,1) primary key,
jsendMail nvarchar(50),
jreceiveMail nvarchar(300),
jsubject nvarchar(100),
jbody text null,
jattachMentRout nvarchar(500) null,
jcc nvarchar(100) null,
jbc nvarchar(100) null,
jzt nvarchar(100) null,
jsendService nvarchar(16),
joperUser nvarchar(30) null,
joperUserId nvarchar(20) null,
joperDate datetime default(getdate()),
jcurrentGroup nvarchar(50) null,
jDescription nvarchar(200) null,
jtrming datetime default(getdate()),
jActive nvarchar(2) default('Y')
)
create table jReceiveMail(
id int identity(1,1) primary key,
jattachMentRout nvarchar(500) null,
jsendMail nvarchar(100),
jsubject nvarchar(100),
jbody text null,
jsize int null,
jreceiveMail nvarchar(60),
jtype nvarchar(40) null,
jreceiveDate datetime null,
jActive nvarchar(2) default('Y')
)
上一篇: Google X实验室迎来了新成员:机器人和无人机
下一篇: 客车车联网更多偏向于运用管理与协同