🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #23228

SmtpMailSocket(java )

#23228
From: �������
Date: Thu, 07 Aug 2003 01:04
132 lines
5780 bytes
������: hushui (hushui), ����: linux
��  ��: SmtpMailSocket(java )
����վ: �����ˮվ (2003��08��06��16:10:07 ������), վ���ż�

import java.util.*; 
import java.io.*; 
import java.net.*; 


public class SmtpMailSocket { 
    public static void main(String[] args) { 
        
        String to = "test@test.com";            //receiptor 
        String from = "lake.hu@fa-software.com";    //Sender
        
        String host = "127.0.0.1";  //smtp server
    String CRLF="\r\n";

        int port=25;          //SMTP port 
        byte[] reponseBuf=new byte[4096];
        
        //email content
        String content=" just a test"; 
        String responseLine=null;
        
// declaration section:
// smtpClient: our client socket
// os: output stream
// is: input stream

        Socket smtpSocket = null;  
        DataOutputStream os = null;
//        DataInputStream is = null;
        BufferedReader response=null;
// Initialization section:
// Try to open a socket on port 25
// Try to open input and output streams
    System.out.println("Connecting...");

        try {
            smtpSocket = new Socket(host, 25);
            os = new DataOutputStream(smtpSocket.getOutputStream());
            //is = new DataInputStream(smtpSocket.getInputStream());
            response= new BufferedReader(new 
InputStreamReader(smtpSocket.getInputStream()));

        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: hostname");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: 
hostname");
        }

// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on port 25

if (smtpSocket != null && os != null && /*is != null*/response!=null) {
            try {

// The capital string before each colon has a special meaning to SMTP
// you may want to read the SMTP specification, RFC1822/3
//is.readFull(reponseBuf); 
    
        responseLine = response.readLine();
        if (responseLine==null||!responseLine.startsWith("220")) {
            throw new ProtocolException(host+"error:"+responseLine);
        }
        System.out.println(responseLine);
        os.writeBytes("HELO"+" localhost"+CRLF);
        responseLine = response.readLine(); // <------ block here ----->//
        if (responseLine==null||!responseLine.startsWith("250")) {
            throw new ProtocolException(host+"error:"+responseLine);
        }
        System.out.println(responseLine);
                os.writeBytes("MAIL From: "+from+CRLF); 
                
                responseLine = response.readLine();
        if (responseLine==null||!responseLine.startsWith("250")) {
            throw new ProtocolException(host+"error:"+responseLine);
        }
        System.out.println(responseLine);
                os.writeBytes("RCPT To: "+to+CRLF); 
                responseLine = response.readLine();
        if (responseLine==null||!responseLine.startsWith("250")) {
            throw new ProtocolException(host+"error:"+responseLine);
        }
        System.out.println(responseLine);
                os.writeBytes("DATA"+CRLF); 
                responseLine = response.readLine();
        if (responseLine==null||!responseLine.startsWith("354")) {
            throw new ProtocolException(host+"error:"+responseLine);
        }
        System.out.println(responseLine);
                os.writeBytes("From: "+from+CRLF); 
                os.writeBytes("Subject: testing"+CRLF); 
                os.writeBytes("Hi there"+CRLF); // message body
                os.writeBytes(CRLF+"."+CRLF); 
                os.flush();
                responseLine = response.readLine();
        if (responseLine==null||!responseLine.startsWith("250")) {
            throw new ProtocolException(host+"error:"+responseLine);
        }
        System.out.println(responseLine);
        os.writeBytes("QUIT"+CRLF); 
        responseLine = response.readLine();
        if (responseLine==null||!responseLine.startsWith("221")) {
            throw new ProtocolException(host+"error:"+responseLine);
        }
        System.out.println(responseLine);

// clean up:
// close the output stream
// close the input stream
// close the socket

        os.close();
                //is.close();
                response.close();
                smtpSocket.close();   
            } catch (UnknownHostException e) {
                System.err.println("Trying to connect to unknown host: " + 
e);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }           
} 
--
�� ��Դ:�������ˮվ 
--
�� ��Դ:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 61.170.239.107]
Technical Details
Message-ID: <47Ya82$9GM@bbs.sjtu.edu.cn>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-06!sn-xit-08!supernews.com!pln-w!extra.newsguy.com!lotsanews.com!news-hog.berkeley.edu!ucberkeley!cyclone.bc.net!sjc70.webusenet.com!news.webusenet.com!news.neu.edu.cn!SJTU

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,GB
X-Google-Thread: f6cb0,f404c988041a96ae,start
X-Google-Attributes: gidf6cb0,public
X-Google-ArrivalTime: 2003-08-06 18:04:03 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-06!sn-xit-08!supernews.com!pln-w!extra.newsguy.com!lotsanews.com!news-hog.berkeley.edu!ucberkeley!cyclone.bc.net!sjc70.webusenet.com!news.webusenet.com!news.neu.edu.cn!SJTU
From: hushui.bbs@bbs.sjtu.edu.cn (�������)
Newsgroups: cn.bbs.comp.lang.java
Subject: SmtpMailSocket(java )
Date: 07 Aug 2003 01:04:19 GMT
Organization: ��ˮ˼Դ
Message-ID: <47Ya82$9GM@bbs.sjtu.edu.cn>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: Java/M.1060218242.A
Lines: 132