1 .依赖库
mail.jar
2. 代码
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.internet.ParseException;
import javax.mail.internet.PreencodedMimeBodyPart;
import org.apache.log4j.Logger;
public class MimeEncoder {
private Logger logger = Logger.getLogger(MimeEncoder.class);
private String encoding = "UTF-8";
private String transferEncoding = "base64";
private String subtype = "mixed";
private MimeBodyPart txtContent;
private MimeBodyPart htmlContent;
private List<MimeBodyPart> inlineList = new ArrayList<MimeBodyPart>(); //内嵌文件
private List<MimeBodyPart> attachmentList = new ArrayList<MimeBodyPart>(); //附件
private Map<String, String> headers = new LinkedHashMap<String, String>();
public MimeEncoder(){
}
public MimeEncoder(String encoding){
this.encoding = encoding;
}
/**
* 构造MIME编码器
* @param encoding String MIME正文默认字符编码
* @param transferEncoding MIME段体传输编码("base64", "quoted-printable", "7bit", "8bit", "binary", "uuencode")
*/
public MimeEncoder(String encoding, String transferEncoding){
this.encoding = encoding;
this.transferEncoding = transferEncoding;
}
public void setSubType(String subtype){
this.subtype = subtype;
}
public void setHeader(String name, String value){
headers.put(name, value);
}
private String readFile(File f)throws IOException, MessagingException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
String s = encodeInputStream(bis);
bis.close();
return s;
}
private String contentEnode(String contentText, String contentType)throws IOException, MessagingException{
String contentEncode = encoding;
if(contentType != null){
ContentType ct = new ContentType(contentType);
if(ct.getParameter("charset") != null){
contentEncode = ct.getParameter("charset");
}
}
return contentEnode(contentText.getBytes(contentEncode));
}
private String contentEnode(byte[] content)throws IOException, MessagingException{
ByteArrayInputStream bais = new ByteArrayInputStream(content);
String s = encodeInputStream(bais);
bais.close();
return s;
}
private String encodeInputStream(InputStream is)throws IOException, MessagingException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = MimeUtility.encode(baos, transferEncoding);
int data = -1;
while((data = is.read()) != -1){
os.write(data);
}
String s = baos.toString(encoding);
os.close();
baos.close();
return s;
}
public void setTxtContent(String content)throws ParseException{
ContentType ct = new ContentType("text/plain");
ct.setParameter("charset", encoding);
setTxtContent(content, ct.toString());
}
public void setTxtContent(String content, String contentType){
PreencodedMimeBodyPart txtContentPart = new PreencodedMimeBodyPart(transferEncoding);
try {
txtContentPart.setText(contentEnode(content, contentType));
if(contentType != null){
txtContentPart.setHeader("Content-Type", contentType);
}
this.txtContent = txtContentPart;
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
public void setHtmlContent(String content)throws ParseException{
ContentType ct = new ContentType("text/html");
ct.setParameter("charset", encoding);
setHtmlContent(content, ct.toString());
}
public void setHtmlContent(String content, String contentType){
PreencodedMimeBodyPart htmlContentPart = new PreencodedMimeBodyPart(transferEncoding);
try {
htmlContentPart.setText(contentEnode(content, contentType));
if(contentType != null){
htmlContentPart.setHeader("Content-Type", contentType);
}
this.htmlContent = htmlContentPart;
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
public void addAttachment(File f, String contentType){
PreencodedMimeBodyPart attachmentPart = new PreencodedMimeBodyPart(transferEncoding);
try {
attachmentPart.setText(readFile(f));
attachmentPart.setDisposition(Part.ATTACHMENT);
attachmentPart.setFileName(nameEncode(f.getName()));
if(contentType != null){
attachmentPart.setHeader("Content-Type", contentType);
}
attachmentList.add(attachmentPart);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
public void addAttachment(String fileName, String contentText, String contentType){
PreencodedMimeBodyPart attachmentPart = new PreencodedMimeBodyPart(transferEncoding);
try {
attachmentPart.setText(contentEnode(contentText, contentType));
attachmentPart.setDisposition(Part.ATTACHMENT);
attachmentPart.setFileName(fileName);
if(contentType != null){
attachmentPart.setHeader("Content-Type", contentType);
}
attachmentList.add(attachmentPart);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
e.printStackTrace();
}
}
public void addAttachment(String fileName, byte[] data, String contentType){
PreencodedMimeBodyPart attachmentPart = new PreencodedMimeBodyPart(transferEncoding);
try {
attachmentPart.setText(contentEnode(data));
attachmentPart.setDisposition(Part.ATTACHMENT);
attachmentPart.setFileName(fileName);
if(contentType != null){
attachmentPart.setHeader("Content-Type", contentType);
}
attachmentList.add(attachmentPart);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
e.printStackTrace();
}
}
public void addInline(File f, String contentType){
addInline(f.getName(), f, contentType);
}
public void addInline(String name, File f, String contentType){
PreencodedMimeBodyPart inlinePart = new PreencodedMimeBodyPart(transferEncoding);
try {
inlinePart.setText(readFile(f));
inlinePart.setDisposition(Part.INLINE);
inlinePart.setContentID(nameEncode(name));
if(contentType != null){
inlinePart.setHeader("Content-Type", contentType);
}
inlineList.add(inlinePart);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
public void addInline(String name, String contentText, String contentType){
PreencodedMimeBodyPart inlinePart = new PreencodedMimeBodyPart(transferEncoding);
try {
inlinePart.setText(contentEnode(contentText, contentType));
inlinePart.setDisposition(Part.INLINE);
inlinePart.setContentID(nameEncode(name));
if(contentType != null){
inlinePart.setHeader("Content-Type", contentType);
}
inlineList.add(inlinePart);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
public void addInline(String name, byte[] data, String contentType){
PreencodedMimeBodyPart inlinePart = new PreencodedMimeBodyPart(transferEncoding);
try {
inlinePart.setText(contentEnode(data));
inlinePart.setDisposition(Part.INLINE);
inlinePart.setContentID(nameEncode(name));
if(contentType != null){
inlinePart.setHeader("Content-Type", contentType);
}
inlineList.add(inlinePart);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
protected String nameEncode(String s)throws UnsupportedEncodingException{
return MimeUtility.encodeText(s, encoding, "B");
}
public static String nameEncode(String s, String encoding)throws UnsupportedEncodingException{
return MimeUtility.encodeText(s, encoding, "B");
}
public String encode(){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
encodeToOutputStream(baos);
try {
return baos.toString("iso-8859-1");
} catch (UnsupportedEncodingException e) {
logger.warn(e.getMessage(), e);
}
return null;
}
public void encodeToOutputStream(OutputStream os){
try {
MimeMultipart multipart = new MimeMultipart(subtype);
if((txtContent != null) && (htmlContent != null)){
MimeMultipart contentPart = new MimeMultipart("alternative");
contentPart.addBodyPart(txtContent);
contentPart.addBodyPart(htmlContent);
MimeBodyPart part = new MimeBodyPart();
part.setContent(contentPart);
multipart.addBodyPart(part);
}else if(txtContent != null){
multipart.addBodyPart(txtContent);
}else if(htmlContent != null){
multipart.addBodyPart(htmlContent);
}
for(MimeBodyPart attachment : attachmentList){
multipart.addBodyPart(attachment);
}
for(MimeBodyPart inline : inlineList){
multipart.addBodyPart(inline);
}
MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
for(String key : headers.keySet()){
message.setHeader(key, headers.get(key));
}
message.setContent(multipart);
message.writeTo(os);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
public void encodeToFile(String f){
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(f));
encodeToOutputStream(bos);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}finally{
if(bos != null){
try {
bos.close();
} catch (IOException e) {
logger.warn(e.getMessage(), e);
}
}
}
}
}
3. 测试
public static void main(String args[])throws Exception{
MimeEncoder encoder = new MimeEncoder("UTF-8");
encoder.setTxtContent("文本内容");
encoder.setHtmlContent("<b>html内容</b><img src='cid:f1'/>");
encoder.addInline("f1", new File("d:/test/内嵌文件.gif"), "image/gif");
encoder.addAttachment(new File("d:/test/附件.txt"), "text/plain");
encoder.encodeToFile("d:/test/mime.eml");
}
执行完测试代码后会生成一个mime.eml文件, 该文件可以使用记事本等文件编辑器打开,也可以使用FoxMail等邮件工具打开。
- 大小: 1.9 KB
分享到:
相关推荐
MIME, 全称为“Multipurpose Internet Mail Extensions”, 比较确切的中文名称为“多用途互联网邮件扩展”。它是当前广泛应用的一种电子邮件技术规范,基本内容定义于RFC 2045-2049 MIME编码的格式
《运用设计模式设计mime编码类》配套代码
原创作品, VC6.0,MFC开发. 支持常用的MIME编解码, 如base64, quoted-printable, UUENCODE, UTF-7, UTF-8, 简繁转换, MD5计算等.
MIME的编码介绍(由网上资料和实践经验整合)
C# 类库来实现MIME的编码和解码 MimeMessage mail new MimeMessage ; mail SetDate ; mail Setversion ; mail SetFrom "sender@local com" null ; mail SetTo "recipient1@server1 com Nick Name...
enmime是Go的MIME编码和解码库,专注于生成和解析MIME编码的电子邮件。 它与电子邮件服务一起开发。 enmime包括一个流畅的界面生成器,用于生成MIME编码的消息,请参阅Wiki中的示例。 有关示例和API使用信息,请...
mime64编码修正 因为有些软件(e.g.Foxmail)对一些 的字符不编码,此函数作了一些修改
其支持的压缩格式有: Zip (.zip) 包括加密的 zip ,RAR (.rar),Arc (.arc, .pak), Bzip (.bz, .tbz, tbz2),Gzip (.gz, tgz),Lha (.lha, .lzh), Unix 压缩 (.Z),Tar (.tar),Base64/MIME 编码 (.b64, .mim, ....
其支持的压缩格式有: Zip (.zip) 包括加密的 zip ,RAR (.rar),Arc (.arc, .pak), Bzip (.bz, .tbz, tbz2),Gzip (.gz, tgz),Lha (.lha, .lzh), Unix 压缩 (.Z),Tar (.tar),Base64/MIME 编码 (.b64, .mim, ....
支持MIME编码和解码(包括字符转换); 支持SMTP和ESMTP协议 支持HTTP 0.9、1.0和1.1协议; 支持SNMP协议; 支持NTP和SNTP协议; 支持POP3协议; 支持Base64和Quoted-pritable编码与解码;...
MIME协议分析 第1 章. MIME概述 MIME,全称为“Multipurpose Internet Mail Extensions”,比较确切的中文 名称为“多用途互 联网邮件扩展”。 它是当前广泛应用的一种电子邮件技术规范,基本内容定义于RFC 2045-2049...
1、FORM标签enctype属性表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能...
下例是一个邮件的标准MIME头: Mime-Version: 1.0 //版本号:1.0 Content-Type: multipart/mixed; boundary="IMA.Boundary...Content-Transfer-Encoding: 7bit //编码方式:7位 Content-Description: cc:Mail note part
6.4 MIME编码解码与发送附件 6.4.1 RFC822的局限 6.4.2 UUENCODE编码与解码 6.4.3 MIME及其编码 6.4.4 构造MIME信件 6.4.5 MIME信件的语法分析 6.5 E-mail乱码 6.5.1 乱码的常见形式及形成原因 6.5.2 避免...
因为apk是zip的mime编码类型,使用winzip或winrar直接拖入到apk改名为zip的压缩包中即可。 5. 不要忘记了签名,使用jdk中的arsigner和keytool打包重新签名即可。 主意:1.2.2版本的用不起的,会报错。
odex和apk合成dex 一、APK生成odex文件方法: ...因为apk是zip的mime编码类型,使用winzip或winrar直接拖入到apk改名为zip的压缩包中即可。 5. 不要忘记了签名,使用jdk中的arsigner和keytool打包重新签名即可。
把一封邮件转换成MIME编码 正如它的名字一样.这个类可以把一封电子邮件转换成MIME编码. 并对转换后的代码进行测试
·UU/ XX/ MIME编码/解码 ·创建多卷集 ·合并多卷集 ·UnSFX(转换成自解压(SFX)。EXE文件到标准档案) ·使用Rijandael加密文件 - AES(256位)加密(WinZip的9兼容) ·解密(.ize)文件 Izarc2go截图:
MIME结构 一、 RFC822协议 RFC822 文档定义了邮件内容的主体结构和各种邮件头字段的详细细节,但是,它没有定义邮件体的格式,RFC822文档定义的邮件体部分通常都只能用于表述一段普通的文本,而无法表达出图片、声音...
10.4 MIME编码解码与发送附件 281 10.4.1 MIME概述 281 10.4.2 MIME定义的新的信头字段 282 10.4.3 MIME邮件的内容类型 283 10.4.4 MIME邮件的编码方式 292 10.5 POP3与接收电子邮件 294 10.5.1 POP3 ...