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服务,感谢缤纷),设置如下图:
欢迎沟通及共同进步
QQ:1441299
Email:1441299@qq.com
越折腾越快乐!
分类:
sharepoint
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端