继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Jmail实现邮件的接收与发送教程

喵喵时光机
关注TA
已关注
手记 213
粉丝 9
获赞 108

1、下载Jmail组件,并安装,将jmail.dll引用到工程中:

https://img1.mukewang.com/5addb3f00001191507780609.jpg

2、定义一个实体保存接受邮件的基本信息,为了简单起见,只定义了四个字段:

    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服务,设置如下图:

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP