C# 利用Jmail 实现邮件收发(可带附件)
标签:pie returns content sub headers 依次 build tty eve
1 首先需要引用 jmail.dll
1 首先需要引用 jmail.dll
下面是 发送邮件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace winMail
{
public partial class Send : Form
{
public Send()
{
InitializeComponent();
}
///
/// 发送邮件
///
public bool Sendmsg()
{
jmail.Message Jmail = new jmail.Message(); //创建一个jmail对象
Jmail.Silent = true; //安静的发送邮件
Jmail.Charset = "GB2312"; //设置发送邮件的字符集
//Jmail.ContentType = "text/html";
string[] To = txtToEmial.Text.ToString().Split(‘,‘);
for (int i = 0; i )
{
Jmail.AddRecipient(To[i]); //添加发送的邮件
}
Jmail.From = txtUname.Text; //从何处发送
Jmail.MailServerUserName = txtUname.Text; //发件人姓名
Jmail.MailServerPassWord = txtPwd.Text; //发件邮件的密码
Jmail.Subject = txtTitle.Text;
if (!string.IsNullOrEmpty(txtFile.Text.Trim()))
{
Jmail.AddAttachment(txtFile.Text, true, null); //添加附件
}
Jmail.Body = txtContens.Text; //添加发送邮件的内容
//Jmail.DeferredDelivery = new DateTime(2012, 9, 11,18,0,0); //设置发送邮件的延迟时间 //这个163不行
bool State= Jmail.Send(txtStmpServer.Text, false); //发送邮件的服务器
Jmail.Close();
return State;
}
private void btnSend_Click(object sender, EventArgs e)
{
if (Sendmsg())
{
MessageBox.Show("发送成功");
this.Close();
}
else
{
MessageBox.Show("发送失败");
}
}
///
/// 选择附件
///
///
///
private void btnOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
DialogResult rst=file.ShowDialog();
txtFile.Text = file.FileName;
}
}
}
下面是收件 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace winMail
{
public partial class index : Form
{
public index()
{
InitializeComponent();//用来初始化窗口
}
///
/// 获取用户帐号密码
///
///
public DataSet GetUser()
{
DataSet ds = new DataSet();
ds = new GetData().GetUser("");
return ds;
}
///
/// 获取邮件
///
public void GetMail()
{
//建立收邮件对象
//jmail.POP3Class popMail =new jmail.POP3Class ();
// jmail.POP3 pop = new jmail.POP3();//用 POP3Class 感觉和pop3没什么不同
///建立邮件信息接口
jmail.Message mailMessage;
///建立附件集接口
jmail.Attachments atts;
///建立附件接口
jmail.Attachment att;
DataTable User = new DataTable();
User = GetUser().Tables[0];
for (int n = 0; n )
{
jmail.POP3 pop = new jmail.POP3();//用 POP3Class 感觉和pop3没什么不同
string EmailAddress = User.Rows[n]["UserName"].ToString();//用户邮件用户名
string Emailpwd = User.Rows[n]["PassWord"].ToString();//密码
int Uid = Convert.ToInt32(User.Rows[n]["Uid"].ToString());//用户id
DateTime Todate = Convert.ToDateTime(new GetData().GetUserMax(Uid));//数据库中自己邮件接收最后的时间
try
{
pop.Connect(EmailAddress, Emailpwd, txtPopServer.Text, Convert.ToInt32(txtPopport.Text));
///如果收到邮件
if (0 pop.Count)
{
//根据取到的邮件数量依次取得每封邮件(倒序)
for (int i = pop.Count; i > 0; i--)
{
//取得一条邮件信息
// mailMessage = popMail.Messages[i];
mailMessage = pop.Messages[i];
///取得该邮件的附件集合
atts = mailMessage.Attachments;
///设置邮件的编码方式
mailMessage.Charset = "GB2312";
///设置邮件的附件编码方式
mailMessage.Encoding = "Base64";
///是否将信头编码成iso-8859-1字符集
mailMessage.ISOEncodeHeaders = false;
TimeSpan span = mailMessage.Date - Todate;//判断邮件的时间是不是大于自己数据库邮件时间最大的值
if (span.Days > 1 || span.Hours > 0 || span.Minutes > 0 || span.Seconds > 0)
{
///插入到数据库 得到刚刚插入数据中的id
int id = new GetData().Insert(mailMessage.Subject, mailMessage.From, mailMessage.FromName, mailMessage.HTMLBody, mailMessage.Date, Uid);
for (int j = 0; j //循环取附件
{
//取得附件
att = atts[j];
//附件名称
string attname = att.Name;
//上传到服务器
string Path =Application.StartupPath+@"\File\"+DateTime.Now.ToString("yyyyMMddHHssss") + new Random().Next(100000000) + attname;
att.SaveToFile(Path);//附件保存地址
string SavaPath =Path;//数据库字段需要保存的值
new GetData().InsertFj(id, SavaPath);//插入附件
}
}
else
{
break;
}
}
att = null;
atts = null;
}
pop.Disconnect();//关闭
pop = null;
}
catch
{
MessageBox.Show("警告:用户:" + EmailAddress + "登录失败!");
timer1.Enabled = false;
}
}
}
///
/// 在任务栏显示
///
///
///
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Visible = true;
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Minimized;
}
}
///
/// 在任务栏显示
///
///
///
private void index_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
///
/// 绑定所有邮件
///
public void BindAllMail()
{
DataSet ds = new DataSet();
StringBuilder sql = new StringBuilder();
if (!string.IsNullOrEmpty(txtTitle.Text.Trim()))
{
sql.Append(" subject like ‘%" + txtTitle.Text.Trim() + "%‘");
}
ds = new GetData().GetMail(sql.ToString());
DataShow.DataSource = ds.Tables[0];
}
///
/// 窗体加载事件
///
///
///
private void index_Load(object sender, EventArgs e)
{
skinEngine1.SkinFile = Application.StartupPath + @"\skin\GlassBrown.ssk";
GetMail();
BindAllMail();
}
///
/// 发送邮件
///
///
///
private void 发送邮件ToolStripMenuItem_Click(object sender, EventArgs e)
{
Send send = new Send();
send.ShowDialog();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定推出应用程序", "提示窗", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
this.Close();
Application.Exit();
}
else
{
Thread New_Messages = new Thread(new ThreadStart(Messages_Show));//创建控制线程
New_Messages.Start();//开始
}
}
///
/// 显示消息
///
public void Messages_Show()
{
}
///
///定时获取
///
///
///
private void timer1_Tick(object sender, EventArgs e)
{
GetMail();
BindAllMail();
}
}
}
C# 利用Jmail 实现邮件收发(可带附件)
标签:pie returns content sub headers 依次 build tty eve
原文地址:https://www.cnblogs.com/zengxh/p/12390150.html
评论