⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jfmemail.java

📁 java邮件源程序
💻 JAVA
字号:
/* * Created on 2004.08.19 * 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.core;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Date;import java.util.Vector;/** * Java bean class. Representing container object for E-mail data. It's quite simple * class consisted of E-mail header and vector for storing E-mail parts. Whole * information is in E-mail header and E-mail parts. E-mail header separated * from this class as POP3 allows downloading only headers. After presenting * headers to user, user chooses which E-mail to download. This prevents too * much network traffic and allows building efficient spam filters.<br><br> * Whay Java bean? I wanted to make E-mail object indenpentend from * protocol implementation. This makes E-mail Java bean much more complicated * for use, but at the same time much more configurable - for evelopers * who understand E-mail format. */public class JfmEmail {	private JfmEmailHeader __header=null;	private Vector __parts=null;		/**	 * Basic E-mail Java bean constructor. Constructs empty E-mail data container.	 * Not recommended for use if you are not familiar with E-mail format. Rather	 * use static Instance constructors for constructing E-mail object.	 *	 */	public JfmEmail() {		__header=new JfmEmailHeader();		__parts=new Vector();	}	/**	 * Constructor for E-mail object with preexisting E-mail header. Not recommended	 * for use if you are not familiar with E-mail format. Rather use static instance	 * constructors.	 * @param header E-mail header.	 */	public JfmEmail(JfmEmailHeader header) {		__header=header;		__parts=new Vector();	}		/**	 * Getter for E-mail header.	 * @return E-mail header.	 */	public JfmEmailHeader getEmailHeader() {		return __header;	}	/**	 * Setter for E-mail header. Allows you to set another E-mail header.	 * @param header E-mail header.	 */	public void setEmailHeader(JfmEmailHeader header) {		__header=header; 	}		/**	 * Getter for E-mail parts count.	 * @return E-mail pasrts count.	 */	public int getEmailPartCount() {		return __parts.size();	}	/**	 * Getter for E-mail parts. This method return object for E-mail part.	 * E-mail may consists of numerous parts. Every part stored separate E-mail	 * information. For example, you could get E-mail with 3 parts: First part	 * contains text message, second part same message in html format,	 * and third part contains binary attachment. Structure of these parts	 * depends on correspodent's E-mail client and has nothing to do with	 * POP3 or SMTP.	 * @param no Index of E-mail part.	 * @return Object for E-mail part. You should check instance of returned	 * object. This metod should return following object types:<br>	 * <strong>JfmEmailTextPart</strong> - contains text message<br>	 * <strong>JfmEmailBinaryPart</strong> - contains binary attachment<br>	 * <strong>JfmEmail</strong> - contains sub E-mail. This could happend	 * when incoming E-mail from mailer-deamon which contains original	 * E-mail you have sent someone. Now you can edit E-mail and send it	 * again!	 * @throws CoreException	 */	public Object getEmailPart(int no) throws CoreException {		if (no<0 || no>=__parts.size()) throw new CoreException("COR_003:Email part index out of range");		return __parts.elementAt(no);	}	/**	 * Setter for adding new E-mail part into E-mail.	 * @param part E-mail part which contains text message.	 * @throws CoreException	 */	public void addEmailPart(JfmEmailTextPart part) throws CoreException {		if (part==null) throw new CoreException("COR_001:No defined email part");		__parts.addElement(part);	}	/**	 * Setter for adding new E-mail part into E-mail.	 * @param part E-mail part which contains binary attachment.	 * @throws CoreException	 */	public void addEmailPart(JfmEmailBinaryPart part) throws CoreException {		if (part==null) throw new CoreException("COR_001:No defined email part");		__parts.addElement(part);	}	/**	 * Setter for adding new E-mail part into E-mail.	 * @param part E-mail part which contains sub E-mail.	 * @throws CoreException	 */	public void addEmailPart(JfmEmail part) throws CoreException {		if (part==null) throw new CoreException("COR_001:No defined email part");		__parts.addElement(part);	}	/**	 * Method for removing E-mail part.	 * @param no Index of E-mail part to remove.	 * @throws CoreException Thrown on index error.	 */	public void removeEmailPart(int no) throws CoreException {		if (no<0 || no>=__parts.size()) throw new CoreException("COR_003:Email part index out of range");		__parts.remove(no);	}		/**	 * Setter for explicit adding binary attachment into E-mail. Recommended	 * for use, doesn't assume any knowledge of E-mail formats.	 * @param name Filename of binary attachment.	 * @param mime_type Mime/type of binary attachment.	 * @param attachment Binary array.	 * @throws CoreException	 */	public void addEmailAttachment(String name,String mime_type,	byte[] attachment) throws CoreException {		if (name==null || name.length()==0) throw new CoreException("COR_001:No attachment name");		if (mime_type==null || mime_type.length()==0) throw new CoreException("COR_001:No mime type. No attachment content type");		if (attachment==null || attachment.length==0) throw new CoreException("COR_001:No attachment");		JfmEmailBinaryPart pa=new JfmEmailBinaryPart();		pa.setContentType(mime_type);		pa.setContentEncoding("base64");		pa.setContentName(name);		pa.setContent(attachment);		__parts.addElement(pa);	}		/**	 * Static E-mail object instantiator. Recommended for use, doesn't assume	 * any knowledge of E-mail format. Content type stored as plain/text.	 * @param sender Sender E-mail address.	 * @param receiver List of receiver E-mail addresses.	 * @param cc List of CC E-mail addresses.	 * @param subject E-mail subject.	 * @param message E-mail message. Strings array.<br>	 * <strong>Important:</strong> Every string of this message array should	 * end with CRLF characters. When reading E-mail message, all lines of	 * E-mail message ends with CRLF:	 * @param charset Subject and message characterset.	 * @return New E-mail object.	 * @throws CoreException	 */	public static JfmEmail CreateTextEmail(String sender,String[] receiver,	String[] cc,String subject,String[] message,String charset) throws CoreException {		if (sender==null || sender.length()==0) throw new CoreException("COR_001:No E-mail sender");		if (receiver==null || receiver.length==0) throw new CoreException("COR_001:No E-mail receiver");		if (subject==null || subject.length()==0) throw new CoreException("COR_001:No E-mail subject");		if (message==null || message.length==0) throw new CoreException("COR_001:No E-mail message");		if (charset==null || charset.length()==0) charset=new String("UTF-8");		JfmEmail em=new JfmEmail();		JfmEmailHeader he=em.getEmailHeader();		he.setFrom(sender);		for(int i=0;i<receiver.length;i++) he.addTo(receiver[i]);		for(int i=0;i<cc.length;i++) he.addCC(cc[i]);		he.setSubjectCharset(charset);		he.setSubject(subject);		he.setEmailDate(new Date());		JfmEmailTextPart pa=new JfmEmailTextPart();		pa.setContentCharset(charset);		pa.setContentEncoding("8bit");		pa.setContentType("text/plain");		pa.setTextContent(message);		em.addEmailPart(pa);				return em;	}		/**	 * Static E-mail object instantiator. Recommended for use, doesn't assume	 * any knowledge of E-mail format. Content type stored as plain/html.	 * @param sender Sender E-mail address.	 * @param receiver List of receiver E-mail addresses.	 * @param cc List of CC E-mail addresses.	 * @param subject E-mail subject.	 * @param message E-mail message. Strings array.<br>	 * <strong>Important:</strong> Every string of this message array should	 * end with CRLF characters. When reading E-mail message, all lines of	 * E-mail message ends with CRLF:	 * @param charset Subject and message characterset.	 * @return New E-mail object.	 * @throws CoreException	 */	public static JfmEmail CreateHtmlEmail(String sender,String[] receiver,	String[] cc,String subject,String[] message,String charset) throws CoreException {		JfmEmail em=JfmEmail.CreateTextEmail(sender,receiver,cc,subject,		message,charset);		((JfmEmailTextPart)em.getEmailPart(0)).setContentType("text/html");		return em;	}		/**	 * Method for replying to E-mail. Subject, text message, sender E-mail address	 * and receiver E-mail address are so changed to rely new E-mail back to	 * origin address. All attachments are removed.	 * @throws CoreException	 */	public void ReplyEmail() throws CoreException {		Vector __new_parts=new Vector();				__header.setFrom(__header.getTo(0));		__header.clearTo();		__header.addTo(__header.getReturnAddr());		__header.setSubject("Re: "+__header.getSubject());		for(int i=0;i<getEmailPartCount();i++) {			Object part=getEmailPart(i);			if (part instanceof JfmEmailTextPart) {				String[] e=((JfmEmailTextPart)part).getContent();				for(int j=0;j<e.length;j++) e[j]=">"+e[j];				__new_parts.addElement(part);			}		}		__parts=__new_parts;	}	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -