日常工作中,常常要将在公司做的东西拷回家,或者要从家里拷东西到公司,但是如果用U盘拷又太麻烦,上web发邮件又有点烦,所以就做了下面的小程序,发送前切版里的内容到指定邮箱来传递文件.
相关技术点:
1.JMail邮件发送
2.剪切板提取
具体代码实现如下:
相关类包: 下载
相关技术点:
1.JMail邮件发送
2.剪切板提取
具体代码实现如下:
/*
* Created on 2008-3-5
*/
package org.dueam.ft;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import sun.misc.BASE64Encoder;
/**
* 剪切板内容发生
* @author <a href="mailto:windonly@gmail.com">Anemone</a>
* hz,zj,china(2008-3-5)
*/
public class ClipboardFileTransmission {
/**
* @param args
* @throws EmailException
* @throws IOException
* @throws UnsupportedFlavorException
* @throws HeadlessException
*/
@SuppressWarnings( " unchecked " )
public static void main(String[] args) throws EmailException, HeadlessException, UnsupportedFlavorException,
IOException {
String context = null ;
List < File > fileList = null ;
/**
* 处理前切版
*/
for (DataFlavor df : Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors()) {
// 如果拷贝的是文本内容
if (df.equals(DataFlavor.stringFlavor)) {
context = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
}
else if (df.equals(DataFlavor.javaFileListFlavor)) {
// 如果拷贝的是文件则当附件发送
fileList = (List < File > ) Toolkit.getDefaultToolkit().getSystemClipboard().getData(
DataFlavor.javaFileListFlavor);
}
}
if (( null == context || "" .equals(context)) && (fileList == null || fileList.isEmpty())) {
return ;
}
if ( null == context || "" .equals(context)) {
context = " 具体资料请看附件 " ;
}
MultiPartEmail email = new MultiPartEmail();
// 发送服务器
email.setHostName( " smtp.163.com " );
// 服务器用户和密码(如果你自己搞了台不用验证的邮件服务器就不用了)
email.setAuthentication( " XXX " , " XXX " );
// 接收的邮箱
email.addTo( " XXX@gmail.com " , " 我的资料库 " );
// 发送服务器的邮件地址,现在很多邮件提供商都有验证这个同用户名是否对应,还是老老实实填真实的吧
email.setFrom( " XXX@163.com " , " Anemone " );
email.setSubject( " [日常资料传递]- " + getTime());
// 文本编码
email.setCharset( " utf-8 " );
email.setMsg(context);
if ( null != fileList)
for (File f : fileList) {
if (f.exists() && f.isFile()) {
// 处理附件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(f.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(getTime() + " By Anemone " );
BASE64Encoder enc = new BASE64Encoder();
// 附件中文名问题
attachment.setName( " =?GBK?B? " + enc.encode(f.getName().getBytes()) + " ?= " );
email.attach(attachment);
}
}
email.send();
}
public static String getTime() {
SimpleDateFormat df = new SimpleDateFormat( " yyyy-MM-dd HH:mm " );
return df.format( new Date());
}
}
以上代码在163和gmail之间测试通过过,建议用exe4j打成EXE文件,然后扔到system32目录下面,这样只要想发送资料的时候,只要复制下资料,再执行下这个命令就一切都OK了.
* Created on 2008-3-5
*/
package org.dueam.ft;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import sun.misc.BASE64Encoder;
/**
* 剪切板内容发生
* @author <a href="mailto:windonly@gmail.com">Anemone</a>
* hz,zj,china(2008-3-5)
*/
public class ClipboardFileTransmission {
/**
* @param args
* @throws EmailException
* @throws IOException
* @throws UnsupportedFlavorException
* @throws HeadlessException
*/
@SuppressWarnings( " unchecked " )
public static void main(String[] args) throws EmailException, HeadlessException, UnsupportedFlavorException,
IOException {
String context = null ;
List < File > fileList = null ;
/**
* 处理前切版
*/
for (DataFlavor df : Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors()) {
// 如果拷贝的是文本内容
if (df.equals(DataFlavor.stringFlavor)) {
context = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
}
else if (df.equals(DataFlavor.javaFileListFlavor)) {
// 如果拷贝的是文件则当附件发送
fileList = (List < File > ) Toolkit.getDefaultToolkit().getSystemClipboard().getData(
DataFlavor.javaFileListFlavor);
}
}
if (( null == context || "" .equals(context)) && (fileList == null || fileList.isEmpty())) {
return ;
}
if ( null == context || "" .equals(context)) {
context = " 具体资料请看附件 " ;
}
MultiPartEmail email = new MultiPartEmail();
// 发送服务器
email.setHostName( " smtp.163.com " );
// 服务器用户和密码(如果你自己搞了台不用验证的邮件服务器就不用了)
email.setAuthentication( " XXX " , " XXX " );
// 接收的邮箱
email.addTo( " XXX@gmail.com " , " 我的资料库 " );
// 发送服务器的邮件地址,现在很多邮件提供商都有验证这个同用户名是否对应,还是老老实实填真实的吧
email.setFrom( " XXX@163.com " , " Anemone " );
email.setSubject( " [日常资料传递]- " + getTime());
// 文本编码
email.setCharset( " utf-8 " );
email.setMsg(context);
if ( null != fileList)
for (File f : fileList) {
if (f.exists() && f.isFile()) {
// 处理附件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(f.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(getTime() + " By Anemone " );
BASE64Encoder enc = new BASE64Encoder();
// 附件中文名问题
attachment.setName( " =?GBK?B? " + enc.encode(f.getName().getBytes()) + " ?= " );
email.attach(attachment);
}
}
email.send();
}
public static String getTime() {
SimpleDateFormat df = new SimpleDateFormat( " yyyy-MM-dd HH:mm " );
return df.format( new Date());
}
}
相关类包: 下载