SendMail的javabean

:send方法重载了一下, 第一个不要认证, 第二个要认证, 加上了USER和PASSWD
/**
* Date : 20010628
* Function : SendMail
* @author : Axman
* 这是一个用Socket直接发信的类,用于没有装SENDMAIL和QMAIL的服器。
* Method :
setSmtpServer(String smtpServer):设置要连结的主机
no return;
createConnect(): SOCKET连结本
本方法對例外進行特殊捕获,如果连结出错则返会假
使信息能被程式员控制。
return boolean
do_command(String s):执行一个输入并返回该输入的响应。

return String
send(String to,String from,String subject,String message)
发信主程序。用于普通SMTP服务器。
return boolean
send(String to,String from,String subject,String message,String username,String passwd)
重载发信程序,用于需要认证的服务器。
return boolean
Base64Encode(String s)
Base64编码方法,可以解决不同平台上的乱码问题。
return String
Note:这是一个JavaBean,加上了main()方法也可以作为APPLICATION运行,便于调试,在作为BEAN使用时可以保留main()方法
不影响类的调用。
*/
CODE:

import java.net.*;
import java.io.*;
public class SendMail {
    private Socket sc;
    private int PORT = 25;
    private BufferedReader in;
    private PrintWriter out;
    private String smtpServer;
    public void setSmtpServer(String smtpServer) {
        this.smtpServer = smtpServer;
    }

    public boolean createConnect() {
        if (smtpServer == null) {
            smtpServer = "localhost";
        }
        try {
            sc = new Socket(smtpServer, PORT);
            in = new BufferedReader(new InputStreamReader(sc.getInputStream()));
            out = new PrintWriter(sc.getOutputStream());
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    public String do_command(String s) throws IOException {
        if (s != null) {
            out.print(s);
            out.flush();
        }
        String line;
        if ((line = in.readLine()) != null) {
            return line;
        } else {
            return "";
        }
    }
    public boolean send(
        String to,
        String from,
        String subject,
        String message) {
        subject = Base64Encode(subject);
        subject = "=?GB2312?B?" + subject + "?=";
        message = Base64Encode(message);

        try {
            String line;
            do_command(null);
            if (-1
                == (line = do_command("HELO " + smtpServer + "
")).indexOf(
                    "250"))
                return false;

            if (-1
                == (line = do_command("MAIL FROM: " + from + "
")).indexOf(
                    "250"))
                return false;
            if (-1
                == (line = do_command("RCPT TO: " + to + "
")).indexOf("250"))
                return false;
            if (-1 == (line = do_command("DATA
")).indexOf("354"))
                return false;
            message = "Content-Type: text/html

" + message;
            message = "Subject: " + subject + "
" + message;
            message = "To: " + to + "
" + message;
            message = "Content-Transfer-Encoding: base64
" + message;
            message = "MIME-Version: 1.0
" + message;
            message = "From: " + from + "
" + message;
            out.print(message + "
");
            if (-1 == (line = do_command("
.
")).indexOf("250"))
                return false;
            in.close();
            out.close();
            sc.close();
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    public boolean send(
        String to,
        String from,
        String subject,
        String message,
        String username,
        String passwd) {
        username = Base64Encode(username);
        passwd = Base64Encode(passwd);
        subject = Base64Encode(subject);
        subject = "=?GB2312?B?" + subject + "?=";
        message = Base64Encode(message);
        try {
            String line;
            do_command(null);
            if (-1 == do_command("EHLO " + smtpServer + "
").indexOf("250"))
                return false;
            while (true) {
                if (-1 != in.readLine().indexOf("250 "))
                    break;
            }
            if (-1 == do_command("AUTH LOGIN
").indexOf("334"))
                return false;
            if (-1 == do_command(username + "
").indexOf("334"))
                return false;
            if (-1 == do_command(passwd + "
").indexOf("235"))
                return false;
            if (-1 == do_command("MAIL FROM: " + from + "
").indexOf("250"))
                return false;
            if (-1 == do_command("RCPT TO: " + to + "
").indexOf("250"))
                return false;
            if (-1 == do_command("DATA
").indexOf("354"))
                return false;
            message = "Content-Type: text/html

" + message;
            message = "Subject: " + subject + "
" + message;
            message = "To: " + to + "
" + message;
            message = "Content-Transfer-Encoding: base64
" + message;
            message = "MIME-Version: 1.0
" + message;
            message = "From: " + from + "
" + message;
            out.print(message + "
");
            if (-1 == do_command("
.
").indexOf("250"))
                return false;
            in.close();
            out.close();
            sc.close();
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    public static String Base64Encode(String s) {
        if (s == null || s.length() == 0)
            return "";
        String EncodingTable =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        byte[] Buffer = s.getBytes();
        int ReadNow, i, res;
        char[] WriteBuf = new char[4];
        char[] Buf = new char[3];
        String EncodedStr = "";
        int ReadIndex = 0;
        int Len = Buffer.length;
        boolean isEnd;
        int BytesWritten = 0;
        ReadNow = 0;
        do {
            isEnd = false;
            for (i = 0; i < 3; i++) {
                if (ReadIndex >= Len) {
                    isEnd = true;
                    ReadNow = i;
                    break;
                }

                Buf[i] = (char) (((byte)Buffer[ReadIndex]) & 0x00FF);
                ReadIndex = ReadIndex + 1;
            }
            if (isEnd)
                break;
            WriteBuf[0] = EncodingTable.charAt((Buf[0] >> 2) & 0x003F);
            WriteBuf[1] =
                EncodingTable.charAt(
                    ((Buf[0] & 3) << 4 | (Buf[1] >> 4)) & 0x003F);
            WriteBuf[2] =
                EncodingTable.charAt(
                    ((Buf[1] & 15) << 2 | (Buf[2] >> 6)) & 0x003F);
            WriteBuf[3] = EncodingTable.charAt((Buf[2] & 63) & 0x003F);

            for (i = 0; i < 4; i++)
                EncodedStr = EncodedStr + WriteBuf[i];

            BytesWritten = BytesWritten + 4;

            if ((BytesWritten % 76) == 0) {
                EncodedStr = EncodedStr + "
";
            }
        } while (ReadNow != 3);

        if (ReadNow < 3) {
            switch (ReadNow) {
                case 1 :
                    WriteBuf[0] = EncodingTable.charAt((Buf[0] >> 2) & 0x003F);
                    WriteBuf[1] =
                        EncodingTable.charAt(((Buf[0] & 3) << 4) & 0x003F);
                    WriteBuf[2] = '=';
                    WriteBuf[3] = '=';

                    for (i = 0; i < 4; i++)
                        EncodedStr = EncodedStr + WriteBuf[i];

                    break;

                case 2 :
                    WriteBuf[0] = EncodingTable.charAt((Buf[0] >> 2) & 0x003F);
                    WriteBuf[1] =
                        EncodingTable.charAt(
                            ((Buf[0] & 3) << 4 | (Buf[1] >> 4)) & 0x003F);
                    WriteBuf[2] =
                        EncodingTable.charAt(((Buf[1] & 15) << 2) & 0x003F);
                    WriteBuf[3] = '=';

                    for (i = 0; i < 4; i++)
                        EncodedStr = EncodedStr + WriteBuf[i];

                    break;
                default :
                    break;
            }
        }
        return (EncodedStr);
    }

    public static void main(String[] args) {
        SendMail sm = new SendMail();
        sm.setSmtpServer("127.0.0.1");
        if (sm.createConnect()) {
            String to = "jinhua@staff.soim.com";
            String from = "axman@soim.com";
            String subject = "中文测试!";
            String message = "大家好啊!";
            String user = "axman";
            String pw = "111111";
            System.out.print(sm.send(to, from, subject, message, user, pw));
        } else {
            System.out.println("怎么连不上SMTP服务器啊?
");
            return;
        }
    }
}

 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值