?? jfmsmtp.java
字號(hào):
/* * Created on 2004.08.20 * JFreeMail - Java mail component * Copyright (C) 2004 Dalibor Krleza * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.jfreemail.smtp;import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Vector;import org.jfreemail.core.AuthException;import org.jfreemail.core.CoreException;import org.jfreemail.core.JfmAuth;import org.jfreemail.core.JfmConsts;import org.jfreemail.core.JfmEmail;import org.jfreemail.core.JfmEmailTextPart;import org.jfreemail.core.JfmEmailBinaryPart;import org.jfreemail.core.JfmSocketCore;import org.jfreemail.core.JfmCore;/** * Class for working with SMTP, sending E-mails. */public class JfmSMTP { private String __smtp_server=null; private InetAddress __smtp_server_address=null; private int __smtp_server_port=25; private int __smtp_auth_type=0; private Socket __smtp_socket=null; /** * Constructor, needs immediate authetication. On successfully created * object, we don't need to worry about connection with server. * @param hostname Hostname of SMTP server. * @param port Port of SMTP server. Use 0 for default SMTP port (25). * @param auth_type Authentication type. See JfmConsts for details. * @param username Username for authentication. * @param password Password for authentication. * @throws SMTPException Thrown on communication error. * @throws AuthException Thrown on authentication error. */ public JfmSMTP(String hostname,int port,int auth_type,String username, String password) throws SMTPException,AuthException { if (hostname==null) { throw new SMTPException("SMTP_001:No defined hostname"); } else __smtp_server=hostname; if (port>0) __smtp_server_port=port; if (auth_type>=0) __smtp_auth_type=auth_type; try { __smtp_socket=new Socket(__smtp_server,__smtp_server_port); } catch(Exception exc) { throw new SMTPException("SMTP_002:Can't connect to "+__smtp_server+":"+String.valueOf(__smtp_server_port)+", "+exc.getMessage()); } String in=null; try { in=JfmSocketCore.getLine(__smtp_socket)[0]; } catch(IOException exc) { try { __smtp_socket.close(); } catch(IOException exc_sub) { } throw new SMTPException("SMTP_100:Communication error"); } if (!JfmCore.checkSMTPresult(in)) throw new SMTPException("SMTP_100:Communication error"); JfmAuth au=new JfmAuth(__smtp_socket,__smtp_auth_type, username,password,new String()); } /** * Constructor, needs immediate authentication, so we don't need * to worry about connection later. * @param addr IP address of SMTP server. * @param port Port of SMTP server. Use 0 for default (25). * @param auth_type Authentication type. See JfmConsts for details. * @param username Username for authentication. * @param password Password for authentication. * @throws SMTPException Thrown on communication error. * @throws AuthException Thrown on authentication error. */ public JfmSMTP(InetAddress addr,int port,int auth_type,String username, String password) throws SMTPException,AuthException { if (addr==null) { throw new SMTPException("SMTP_001:No defined hostname"); } else __smtp_server_address=addr; if (port>0) __smtp_server_port=port; if (auth_type>=0) __smtp_auth_type=auth_type; try { __smtp_socket=new Socket(__smtp_server_address,__smtp_server_port); } catch(Exception exc) { throw new SMTPException("SMTP_002:Can't connect to "+__smtp_server_address.getHostAddress()+":"+String.valueOf(__smtp_server_port)+", "+exc.getMessage()); } String in=null; try { in=JfmSocketCore.getLine(__smtp_socket)[0]; } catch(IOException exc) { try { __smtp_socket.close(); } catch(IOException exc_sub) { } throw new SMTPException("SMTP_100:Communication error"); } if (!JfmCore.checkSMTPresult(in)) throw new SMTPException("SMTP_100:Communication error"); JfmAuth au=new JfmAuth(__smtp_socket,__smtp_auth_type, username,password,new String()); } /** * Method for closing connection with SMTP server. Use immediate * before object destruction. * @throws SMTPException Thrown on communication error. */ public void close() throws SMTPException { try { String out="QUIT\r\n"; OutputStream os=__smtp_socket.getOutputStream(); os.write(out.getBytes()); String in=JfmSocketCore.getLine(__smtp_socket)[0]; } catch(IOException exc) { throw new SMTPException("SMTP_100:Communication error"); } } /* * Method for resetting protocol stage when sending mail. */ private void resetSMTP() throws SMTPException { try { OutputStream os=__smtp_socket.getOutputStream(); String out="RSET\r\n"; os.write(out.getBytes()); String in=JfmSocketCore.getLine(__smtp_socket)[0]; } catch(Exception exc) { throw new SMTPException("SMTP_004:Error during SMTP reset"); } } /** * Method for sending E-mail. * @param em E-mail to send. * @throws SMTPException Thrown on communication error: * @throws CoreException Thrown on E-mail composing error. */ public void sendMail(JfmEmail em) throws SMTPException,CoreException { try { /* * Calling E-mail composing method. */ String[] email=createEmailData(em); OutputStream os=__smtp_socket.getOutputStream(); /* * Setting up E-mail sender, out own E-mail address. */ String out="MAIL FROM:"+em.getEmailHeader().getFrom()+"\r\n"; os.write(out.getBytes()); String in=JfmSocketCore.getLine(__smtp_socket)[0]; if (!JfmCore.checkSMTPresult(in)) { resetSMTP(); throw new SMTPException("SMTP_003:Error setting E-mail sender"); } /* * Setting up all E-mail receivers from receivers list. */ for(int i=0;i<em.getEmailHeader().getToCount();i++) { out="RCPT TO:"+em.getEmailHeader().getTo(i)+"\r\n"; os.write(out.getBytes()); in=JfmSocketCore.getLine(__smtp_socket)[0]; if (!JfmCore.checkSMTPresult(in)) { resetSMTP(); throw new SMTPException("SMTP_003:Error setting E-mail receiver"); } } /* * Setting up all E-mail receivers from CC list. We must do this * manually. */ for(int i=0;i<em.getEmailHeader().getCCCount();i++) { out="RCPT TO:"+em.getEmailHeader().getCC(i)+"\r\n"; os.write(out.getBytes()); in=JfmSocketCore.getLine(__smtp_socket)[0]; if (!JfmCore.checkSMTPresult(in)) { resetSMTP(); throw new SMTPException("SMTP_003:Error setting E-mail receiver"); } } /* * Sending E-mail. */ out="DATA\r\n"; os.write(out.getBytes()); in=JfmSocketCore.getLine(__smtp_socket)[0]; if (!JfmCore.checkSMTPresult(in)) { resetSMTP(); throw new SMTPException("SMTP_003:Error sending E-mail"); } for(int i=0;i<email.length;i++) { os.write(email[i].getBytes()); } in=JfmSocketCore.getLine(__smtp_socket)[0]; if (!JfmCore.checkSMTPresult(in)) { resetSMTP(); throw new SMTPException("SMTP_003:Error sending E-mail"); } /* * We need to reset protocol in order to prepare server for new * E-mail. */ resetSMTP(); } catch(IOException exc) { throw new SMTPException("SMTP_100:Communication error"); } }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -