C# 接收邮件(LumiSoft.Net.dll)

1 首先需要用NuGet将LumiSoft.Net.dll添加到引用

2 顺便给出官方的例子链接

LumiSoft例子下载

3 接收邮件的代码,需要的库都是系统可以提示的,就不多提了。

代码简要说明:传入结构体之前,需要将mailserver,mailserverport,userName(收件人邮箱地址),userPass(收件人邮箱密码)初始化,程序中未做判断是否为空,使用时需要注意。邮件信息都在结构体内,附件信息在maliAttachMents里面,存放的是附件接收后的本地文件目录,一般默认为当前目录下的mailInfo/AttachMents里面。

注意:此例子接收了所有邮件,并向邮件服务器发送了接收完邮件并删除邮件的指令!具体代码: message.MarkForDeletion();

   struct MailInfo
        {
            public string mailServer;
            public int mailServerPort;
            public string userName;
            public string userPass;
            public string mailSender;
            public string mailTittle;
            public string mailSubject;
            public string mailBodyText;
            public List<string> mailAttachMents;

            public MailInfo(string mails,int mailp, string un,string up)
            {
                mailServer = mails;
                mailServerPort = mailp;
                userName = un;
                userPass = up;
                mailSender = "";
                mailTittle = "";
                mailSubject = "";
                mailBodyText = "";
                mailAttachMents = new List<string>();
            }
        };

        void MailReceive(MailInfo mailInfo)
        {
            using (POP3_Client pop3 = new POP3_Client())
            {
                try
                {

                    pop3.Connect(mailInfo.mailServer, mailInfo.mailServerPort, false);
                    pop3.Login(mailInfo.userName,mailInfo.userPass);

                    try
                    {
                        int msgNum = pop3.Messages.Count;
                        foreach (POP3_ClientMessage message in pop3.Messages)
                        {
                            Mail_Message mime = Mail_Message.ParseFromByte(message.HeaderToByte());

                            //邮件发送者
                            if (mime.From != null)
                            {
                                mailInfo.mailSender = mime.From[0].Address.ToString();
                            }
                          
                            //邮件主题
                            if (string.IsNullOrEmpty(mime.Subject))
                            {
                                mailInfo.mailSubject = "无主题";
                            }
                            else
                            {
                                mailInfo.mailSubject = mime.Subject;
                            }
                            mime = Mail_Message.ParseFromByte(message.MessageToByte());
                            //邮件正文
                            if (mime.BodyText != null)
                            {
                                mailInfo.mailBodyText = mime.BodyText;
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(mime.BodyHtmlText))
                                {
                                    mailInfo.mailBodyText = mime.BodyHtmlText;
                                }
                            }

                            //邮件附件
                            foreach (MIME_Entity entity in mime.Attachments)
                            {

                                if (entity.ContentDisposition != null && entity.ContentDisposition.Param_FileName != null)
                                {
                                    string fileName = entity.ContentDisposition.Param_FileName;
                                    mailInfo.mailAttachMents.Add(fileName);
                                    string filePath = Directory.GetCurrentDirectory() + @"\MailInfo\Attachments";//本机邮件内容保存路径
                                    if (!Directory.Exists(filePath))
                                        Directory.CreateDirectory(filePath);

                                    string str_PathAttachMent = filePath + "\\" + fileName;
                                    File.WriteAllBytes(str_PathAttachMent, ((MIME_b_SinglepartBase)entity.Body).Data);
                                }
                            }
                            //删除邮件服务器上的邮件,使用删除标志位
                            message.MarkForDeletion();
                        }
                    }
                    catch (Exception x)
                    {
                       // ShowError.MsgBox("Error: " + x.Message);
                    }
                }
                catch (Exception ex)
                {
                    //ShowError.MsgBox(ex.Message);
                }
            }


        }

//调用
        private void button_Receive_Click(object sender, EventArgs e)
        {
            MailInfo mi = new MailInfo("192.168.1.1",110, "111@qq.com", "111111");
            
            MailReceive(mi);
        }

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值