首頁 > 軟體

C#实现收发邮件功能

2022-05-13 21:11:45

一、伺服器端

    TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 13000);//定义监听器
    server.Start();//启动监听器
    byte[] buffer = new byte[1024]; //建立一个容器用于接受资料
    string data = null;
    while (true)
    {
        //等待用户端连线过来
        TcpClient client = server.AcceptTcpClient();

        //Task<TcpClient> task1 = server.AcceptTcpClientAsync();
        //Socket socket = server.AcceptSocket();
        //Task<Socket> task2 = server.AcceptSocketAsync();
        //server.Pending();

        //取得用户端传送过来的资料 
        int len = client.Available;//获取已经从网路流接收且可供读取的资料量
        NetworkStream nstream = client.GetStream(); //返回一个可供读写的网路流
        int bytesRead;
        do
        {
            bytesRead = nstream.Read(buffer, 0, buffer.Length); //接收讯息
            data = Encoding.UTF8.GetString(buffer, 0, bytesRead); //先将接收到的资料转化可输出的字串型别
            Console.WriteLine("收到讯息:" + data); 输出收到的资讯
            byte[] msg = Encoding.UTF8.GetBytes(data.ToUpper());
            nstream.Write(buffer, 0, buffer.Length);//传送讯息
        } while (bytesRead > 0);
        nstream.Close(); //1.先释放流的资料
        client.Close(); //2.再释放用户端的连线资料
    }
    server.Stop(); //3.关闭监听

二、用户端

    TcpClient client = new TcpClient("127.0.0.1", 13000);//连线伺服器
    //或者
    //    TcpClient client=new TcpClient();
    //    client.Connect("127.0.0.1", 13000);
    NetworkStream nstream = client.GetStream();
    //---其他同伺服器端
    nstream.Close(); //1.先释放流的资料
    client.Close(); //2.再释放用户端的连线资料

三、传送邮件

1、使用MailMessage和SmtpClient传送邮件。

在Wince中,可以使用OpenNetCF.Net.Mail类传送邮件。

    MailMessage mailMessage = new MailMessage
    {
        //发件人
        From = new MailAddress("***@163.com", "您的朋友", Encoding.UTF8),
        Priority = MailPriority.High,
        //邮件主题
        SubjectEncoding = Encoding.UTF8,
        Subject = "Hello",
    };

    //收件人 可以新增多个收件人
    mailMessage.To.Add(new MailAddress("137***126@qq.com"));

    //mailMessage.CC 获取包含此电子邮件的抄送(CC)收件人的地址集合

    //邮件正文            
    mailMessage.IsBodyHtml = true;//如果要传送html格式的讯息,需要设定这个属性
    mailMessage.BodyEncoding = Encoding.UTF8;
    mailMessage.Body = "<p style='color:red;font-size:14px;'>邮件测试</p>";

    //邮件内容即讯息正文中中显示图片 ,需要为图片指明src='cid:idname(资源id)'
    AlternateView htmlBody = AlternateView.CreateAlternateViewFromString("<img src='cid:zfp'/>", null, "text/html");
    //然后在LinkedResource加入档案的绝对地址,和ContentType 例如image/gif,text/html...与http请求的响应报文中的ContentType一致
    LinkedResource lr = new LinkedResource("1.gif", "image/gif");
    //系结上文中指定的idname
    lr.ContentId = "zfp";
    //新增连结资源
    htmlBody.LinkedResources.Add(lr);
    mailMessage.AlternateViews.Add(htmlBody);

    //传送附件 指明附件的绝对地址
    Attachment attachment = new Attachment("1.txt");
    mailMessage.Attachments.Add(attachment);

    //建立邮件传送用户端
    SmtpClient sendClient = new SmtpClient("smtp.qq.com", 25)
    {
        //指定邮箱账号和密码
        Credentials = new NetworkCredential(fromMail, "xmxyldrmtvnrddfe")
    };

    //指定如何传送电子邮件
    sendClient.DeliveryMethod = SmtpDeliveryMethod.Network;

    //指定使用使用安全通讯端ssl加密的连结
    sendClient.EnableSsl = true;
    sendClient.Send(mailMessage);
    //或者可以一步传送
    sendClient.SendAsync(mailMessage, null);

 2、利用TcpClient类收取pop3邮件 

    TcpClient client = new TcpClient("mail.**.com", 443);

    //连线
    NetworkStream ns = client.GetStream();
    StreamReader sr = new StreamReader(ns, Encoding.Default);
    string result = sr.ReadLine(); //返回OK或ER

    //登陆
    StreamWriter sw = new StreamWriter(ns);
    sw.WriteLine("user  b**");
    sw.Flush();
    result = sr.ReadLine();

    sw.WriteLine("pass  **");
    sw.Flush();
    result = sr.ReadLine();
    //获取邮件数量
    sw.WriteLine("stat");
    sw.Flush();
    result = sr.ReadLine();//result.split(' ')[1]表示邮件数量

    //获取邮件资讯
    sw.WriteLine("top 1 0 ");
    sw.Flush();
    result = sr.ReadLine();
    while (true)
    {
        result = sr.ReadLine();
        if (result == ".") break;
        if (result.Length > 4 && result.Substring(0, 8) == "Subject:")
        {
            //result为标题,乱码用Base64解码
            Encoding.GetEncoding("gb2312").GetString(Convert.FromBase64String(result));
        }
    }

到此这篇关于C#实现收发邮件功能的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com