C#使用Jmail实现邮件的接收与发送详细示例
本文使用很出名的jmail来发送与接收邮件做个详细示例,不使用.net内置的发送程序,以前在asp时代都是使用jmail组件来发送邮件的。
1、下载Jmail组件,并安装,将jmail.dll引用到工程中:
2、定义一个实体保存接受邮件的基本信息,为了简单起见,只定义了四个字段:
MailInfo
- class MailInfo
- {
- public string FromMail
- {
- get;
- set;
- }
- public string MailSubject
- {
- get;
- set;
- }
- public string MailMessage
- {
- get;
- set;
- }
- public List<string> atts
- {
- get;
- set;
- }
- }
3、邮件发送代码:
- jmail.Message MailObj = new jmail.MessageClass();
- MailObj.From = parentform.txtMail.Text; //发件人的地址
- MailObj.Logging = true;
- MailObj.MailServerUserName = parentform.txtMail.Text; //发件人用户名
- MailObj.MailServerPassWord = parentform.txtPassWord.Text; //服务器验证
- MailObj.HTMLBody = txtMessage.Text;
- MailObj.Charset = "gb2312";
- MailObj.Subject = txtSubject.Text;
- MailObj.FromName = parentform.txtMail.Text;
- MailObj.AddRecipient(parentform.txtMail.Text, "User", "A"); //添加接收人
- MailObj.Priority = 3;
- if (txtAtt.Text != "")
- {
- MailObj.AddAttachment(txtAtt.Text, false, "image/jpg");
- }
- try
- {
- bool bool_OK = MailObj.Send(parentform.txtServer.Text, false);
- if (bool_OK == true)
- MessageBox.Show("发送成功!");
- if (bool_OK == false)
- MessageBox.Show("发送失败!");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
4、邮件接收代码:
- ///建立收邮件对象
- jmail.POP3Class popMail = new POP3Class();
- ///建立邮件信息接口
- jmail.Message mailMessage;
- ///建立附件集接口
- jmail.Attachments atts;
- ///建立附件接口
- jmail.Attachment att;
- try
- {
- popMail.Connect(parentform.txtMail.Text.Trim(), parentform.txtPassWord.Text.Trim(), parentform.txtServer.Text.Trim(), Convert.ToInt32(parentform.txtPoint.Text.Trim()));
- lsvMail.View = View.Details;
- // Allow the user to edit item text.
- lsvMail.LabelEdit = false;
- // Allow the user to rearrange columns.
- lsvMail.AllowColumnReorder = true;
- // Select the item and subitems when selection is made.
- lsvMail.FullRowSelect = true;
- // Display grid lines.
- lsvMail.GridLines = true;
- // Sort the items in the list in ascending order.
- ///如果收到邮件
- if (0 < popMail.Count)
- {
- ///根据取到的邮件数量依次取得每封邮件
- for (int i = 1; i <= popMail.Count; i++)
- {
- ///取得一条邮件信息
- mailMessage = popMail.Messages[i];
- ///取得该邮件的附件集合
- atts = mailMessage.Attachments;
- ///设置邮件的编码方式
- mailMessage.Silent = true;
- mailMessage.EnableCharsetTranslation = true;
- mailMessage.ContentTransferEncoding = "Base64";
- mailMessage.Encoding = "Base64";
- mailMessage.Charset = "gb2312";
- mailMessage.ContentType = "text/html";
- mailMessage.ISOEncodeHeaders = false;
- ///是否将信头编码成iso-8859-1字符集
- MailInfo MailInfoObj = new MailInfo();
- MailInfoObj.MailSubject = mailMessage.Subject;
- MailInfoObj.MailMessage = mailMessage.HTMLBody;
- MailInfoObj.FromMail = mailMessage.From;
- MailInfoObj.atts = new List<string>();
- for (int j = 0; j < atts.Count; j++)
- {
- ///取得附件
- att = atts[j];
- ///附件名称
- string atturl= "e:\\" + att.Name;
- ///上传到服务器
- if (!File.Exists(atturl))
- {
- att.SaveToFile(atturl);
- }
- MailInfoObj.atts.Add(atturl);
- }
- ListViewItem item = new ListViewItem();
- item.Text = mailMessage.From;
- item.Tag = MailInfoObj;
- lsvMail.Items.Add(item);
- }
- }
- else
- {
- lbMsg.Text = "没有新邮件!";
- }
- //popMail.DeleteMessages();
- popMail.Disconnect();
- popMail = null;
- }
- catch (Exception ex)
- {
- lbMsg.Text = "请检查邮件服务器的设置是否正确!";
- }
5、测试:
1)、在server中填写:smtp.163.com , mail:***@163.com password:******
2)邮件发送:填写收件人、主题、内容、附件。
3)将server改写成:pop3.163.com , mail:***@163.com password:******,收取的邮件和附件,如下图:
6、总结:
这个非常的简单,网上也有很多代码,这里提供一个完整的Demo下载,偶也是整理网上的代码。
使用网易的邮箱测试的时候,请注意开启POP3/SMTP服务(网易自2006年11月16日起,网易免费邮件系统的POP3及SMTP服务将暂停开通,但是2010-01-05 20点40分开始,网易免费邮箱重新免费开放POP3/SMTP服务,感谢缤纷),设置如下图: