• 不使用system.web.mail来发送邮件


    Based on RFC 821 about Simple Mail Transfer Protocol, it's very easy to send mail via SMTP. Here's the handshaking for sending mail:
     
     Receiver: 220 server.com Simple Mail Transfer Service Ready
     Sender  : HELO server.com
     Receiver: 250 server.com
     Sender  : MAIL FROM: <agusk@host.com>
     Receiver: 250 OK
     Sender  : RCPT TO: <myhoney@host.com>
     Receiver: 250 OK
     Sender  : DATA
     Receiver: 354 Start mail input: end with <CRLF>.<CRLF>
     Sender  : all data like From, To, Subject, Body etc.
               ended with <CRLF>.<CRLF>
     Receiver: 250 OK
     Sender  : QUIT
     Receiver: 250 server.com closing transmission channel



    private void SendBtn_Click(object sender, System.EventArgs e)
    {
        // change cursor into wait cursor
        Cursor cr = Cursor.Current;
        Cursor.Current = Cursors.WaitCursor;

        // create server SMTP with port 25
        TcpClient SmtpServ = new TcpClient(ServSMTP.Text,25);
        string Data;
        byte[] szData;
        string CRLF = "\r\n";
        LogList.Items.Clear();            

        try
        {
            // initialization
            NetworkStream NetStrm = SmtpServ.GetStream();
            StreamReader  RdStrm= new StreamReader(SmtpServ.GetStream());
            LogList.Items.Add(RdStrm.ReadLine());

            
            // say hello to server and send response into log report
            Data = "HELLO server " + CRLF;                
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());

            // send sender data
            Data = "MAIL FROM: " + "
    <" + sFrom.Text + ">" + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());

            // send receiver data
            Data = "RCPT TO: " + "
    <" + sTo.Text + ">" + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());

            // send DATA
            Data = "DATA " + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());                

            // send content data
            Data = "SUBJECT: " + sSubject.Text + CRLF + sMessage.Text + CRLF + "." + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());                

            // quit from server SMTP
            Data = "QUIT " + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());                

            // close connection
            NetStrm.Close();
            RdStrm.Close();
            LogList.Items.Add("Close connection");
            LogList.Items.Add("Send mail successly..");

            // back to normal cursor
            Cursor.Current = cr;

        }
        catch(InvalidOperationException err)
        {
            LogList.Items.Add("Error: "+ err.ToString());
        }


    }
  • 相关阅读:
    数据结构习题
    POJ 2965 The Pilots Brothers' refrigerator
    POJ 1753 Flip Game
    HDU 1172 猜数字
    假币问题
    HDU 1425 sort
    Java基础知识
    P1650 田忌赛马
    SQL注入之Sqli-labs系列第十九关(基于头部的Referer POST报错注入)
    SQL注入之Sqli-labs系列第十八关(基于错误的用户代理,头部POST注入)
  • 原文地址:https://www.cnblogs.com/sunbingzibo/p/1095054.html
Copyright © 2020-2023  润新知