C#接收邮件

下面是接收邮件的主程序代码如下:

using System;
using System.Text;
using System.IO;
using Email.POP3;

namespace TestPOP3
... {
   
class example
   
...{
        [STAThread]
       
static void Main(string[] args)
       
...{
           
//我测试的时候用的是163的邮箱,163的免费POP邮件服务器是pop.163.com。而163官方给出的是                   
           
//pop.126.com在这里不能用,原因是这个邮件服务器是有SSL加密的,GMAIL我也测试了也不能用都是这个原因
            POP3 objPOP3 = new POP3("pop.163.com", 110, "用户名", "密码");
            Console.WriteLine(objPOP3.Connect()
? "Connected" : "Can't connect");
           
try
           
...{
               
if (objPOP3.IsAPOPSupported)
               
...{
                    Console.WriteLine(objPOP3.SecureLogin()
? "Secure Logged in" : "Can't login");
                }

               
else
               
...{
                    Console.WriteLine(objPOP3.Login()
? "Logged in" : "Can't login");
                }

                objPOP3.QueryServer();
                Console.WriteLine(
"Emails count: " + objPOP3.TotalMailCount);
               
//以下的FOR循环是显示出所有收件箱里面的邮件信息
                for (int i = 1; i <= objPOP3.TotalMailCount; i++)
               
...{
                    EmailMessage objEmail
= objPOP3.GetMessage(i, false); // use true to get headers only
                    Console.WriteLine("NEW MESSAGE:------------------");
                    Console.WriteLine(
"FROM: " + objEmail.From);
                    Console.WriteLine(
"TO: " + objEmail.To);
                    Console.WriteLine(
"CC: " + objEmail.Cc);
                    Console.WriteLine(
"SUBJECT: " + objEmail.Subject);
                    Console.WriteLine(
"DATE: " + objEmail.Date);
                    Console.WriteLine(
"CONTENT-TYPE: " + objEmail.ContentType);
                    Console.WriteLine(
"CHARSET: " + objEmail.Charset);
                    Console.WriteLine(
"MESSAGE-ID: " + objEmail.GetCustomHeader("Message-ID"));
                    Console.WriteLine(
"MESSAGE SIZE: " + objEmail.Size);
                   
if (objEmail.IsAnyAttachments)
                   
...{
                       
for (int a = 0; a < objEmail.Attachments.Count; a++)
                       
...{
                           
//调用邮件附件的方法
                            processAttachment((Attachment)objEmail.Attachments[a], 1);
                        }

                    }

                   
else
                   
...{
                        Console.WriteLine(
"BODY: " + Encoding.Default.GetString(Convert.FromBase64String(objEmail.Body)));
                    }

                   
//下面注册掉的代码是删除该邮件
                   
//objPOP3.DeleteMessage(i);

                }

                objPOP3.Close();
            }

           
catch (System.Exception e)
           
...{
                Console.WriteLine(e.Message);
                Console.ReadLine();
                objPOP3.Close();
               
return;
            }


        }


       
static void processAttachment(Attachment att, int nesting)
       
...{
           
for(int i = 0; i < nesting * 2; i++) Console.Write("-");

           
//以下注释掉的代码可以打开,以下都是关于邮件附件的相关信息,因为我只需要得到附件的文件信息^_^

           
//Console.WriteLine("ATT: ");
           
//Console.WriteLine("ContentTransferEncoding: " + att.ContentTransferEncoding);
           
//Console.WriteLine("ContentType: " + att.ContentType);
           
//Console.WriteLine("EstimatedSize: " + att.EstimatedSize);
           
//Console.WriteLine("FileName: " + att.FileName);
           
//processBody("HtmlBody", att.HtmlBody);
           
//processBody("TextBody", att.TextBody);
           
//Console.WriteLine("IsAnyAttachments: " + att.IsAnyAttachments);
           
//Console.WriteLine("IsFileAttachment: " + att.IsFileAttachment);
            if (att.IsAnyAttachments)
           
...{
               
for (int a = 0; a < att.Attachments.Count; a++)
               
...{
                    processAttachment((Attachment)att.Attachments[a], nesting
* 2);
                }

            }

           
if(att.IsFileAttachment)
           
...{
               
//这里说一下在保存邮件附件之前必须"c:/pop3"该文件夹是存在的,否则是保存不了的
                att.Save(@"c:/pop3" + att.FileName);
                Console.WriteLine(
"附件保存成功!附件名称为:" + att.FileName);
            }

        }


       
static void processBody(string bodytype, string body)
       
...{
           
if (body == null)
           
...{
                Console.WriteLine(bodytype
+ ": null");
               
return;
            }

           
if (body.Length > 1000)
           
...{
                Console.WriteLine(bodytype
+ ": " + body.Substring(0, 1000) + "...");
            }

           
else
           
...{
                Console.WriteLine(bodytype
+ ": " + body);
            }

        }

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值