Java 邮件email 发送, 邮件内容、邮件附件 名称 乱码处理 完整版
2012-12-03 17:42
676 查看
/**
*
*
*
* Copyright (C) Corporation. All rights reserved.
*
*/
package org.email.send;
import java.io.File;
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.ByteArrayDataSource;
/**
* 邮件发送类
* 需要的jar包有:mail.jar ,activation.jar ,
* 如果用到ByteArrayDataSource 类需要引入 commons-email.jar ;
*
*/
public class SendEMailMessage {
private String senderEmailAddr;
//发送者别名
private String displayName;
// 邮件发送者邮箱用户
private String smtpUserName;
// 邮件发送者邮箱密码
private String smtpPassword;
// 邮件发送者邮箱SMTP服务器
private String smtpServerName;
// 邮件发送者邮箱SMTP服务器端口
private String smtpServerPort;
// 传输类型
private String transportType;
//是否验证
private Boolean isAuth;
private int timeout;
// 属性
private static Properties props;
public static void main(String [] args){
SendEMailMessage sem = new SendEMailMessage();
sem.init();
sem.send();
}
public void init() {
// 设置邮件发送者地址
this.senderEmailAddr = "xxyy@163.com";
//发送者别名
this.displayName="管理员";
// 设置邮件发送者邮箱SMTP服务器
this.smtpServerName = "smtp.163.com";
// 设置邮件发送者邮箱SMTP服务器端口号
this.smtpServerPort = "25";
// 设置邮件发送者邮箱用户
this.smtpUserName = "testAdmin@163.com";
// 邮件发送者邮箱密码
this.smtpPassword = "password";
// 设置传输类型
this.transportType = "smtp";
this.isAuth = true;
this.timeout = 300000;
props = new Properties();
// 存储发送邮件服务器的信息
props.put("mail.smtp.host", smtpServerName);
props.put("mail.smtp.port", smtpServerPort);
props.put("mail.transport.protocal", transportType);
props.put("mail.smtp.auth", isAuth ? "true":"false");
props.put("mail.stmp.timeout", timeout);
}
private Session getSession(){
Authenticator authenticator = null;
if(isAuth){
authenticator = new SimpleAuthenticator(smtpUserName,smtpPassword);
}
Session sendMailSession = Session.getDefaultInstance(props,authenticator);
return sendMailSession;
}
/**
* 发送email
* @param msg
* @return
*/
public void send(){
Session session = this.getSession();//获取邮件服务器连接
Message message = new MimeMessage(session);//创建邮件
try {
Address from = new InternetAddress(senderEmailAddr,displayName);
message.setFrom(from);//发件人
message.setSubject("邮件主题");
message.setSentDate(new Date());//发送时间
List<File> attach = new ArrayList<File>();//附件,此处附件没有文件,只是为了方便示例。
Multipart multiPart = new MimeMultipart();
BodyPart context = new MimeBodyPart();
context.setContent("<html><body><a herf='http://www.baidu.com'>This is a test email ! helloworld..</a><body></html>","text/html;charset=utf-8");
multiPart.addBodyPart(context);
for(File att:attach){
MimeBodyPart attachmentPart = new MimeBodyPart();
//1.如果数据源是byte[]
// DataSource fds = new ByteArrayDataSource(byte[] b,"application/octet-stream");
//2.如果内容是file
DataSource fds = new FileDataSource(att);
attachmentPart.setDataHandler(new DataHandler(fds));
attachmentPart.setFileName(MimeUtility.encodeText(att.getName(),"utf-8",null)); // 解决附件名称乱码
multiPart.addBodyPart(attachmentPart);
}
message.setContent(multiPart);
message.setRecipients(RecipientType.TO, new Address[]{new InternetAddress("1111@163.com")});//发送
message.setRecipients(RecipientType.CC, new Address[]{new InternetAddress("2222@163.com")});//抄送
message.setRecipients(RecipientType.BCC, new Address[]{new InternetAddress("333@163.com")});//暗送
//发送邮件
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
//======================================================================
class SimpleAuthenticator extends Authenticator {
private String user;
private String pwd;
public SimpleAuthenticator(String user, String pwd) {
this.user = user;
this.pwd = pwd;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pwd);
}
}
}
*
*
*
* Copyright (C) Corporation. All rights reserved.
*
*/
package org.email.send;
import java.io.File;
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.ByteArrayDataSource;
/**
* 邮件发送类
* 需要的jar包有:mail.jar ,activation.jar ,
* 如果用到ByteArrayDataSource 类需要引入 commons-email.jar ;
*
*/
public class SendEMailMessage {
private String senderEmailAddr;
//发送者别名
private String displayName;
// 邮件发送者邮箱用户
private String smtpUserName;
// 邮件发送者邮箱密码
private String smtpPassword;
// 邮件发送者邮箱SMTP服务器
private String smtpServerName;
// 邮件发送者邮箱SMTP服务器端口
private String smtpServerPort;
// 传输类型
private String transportType;
//是否验证
private Boolean isAuth;
private int timeout;
// 属性
private static Properties props;
public static void main(String [] args){
SendEMailMessage sem = new SendEMailMessage();
sem.init();
sem.send();
}
public void init() {
// 设置邮件发送者地址
this.senderEmailAddr = "xxyy@163.com";
//发送者别名
this.displayName="管理员";
// 设置邮件发送者邮箱SMTP服务器
this.smtpServerName = "smtp.163.com";
// 设置邮件发送者邮箱SMTP服务器端口号
this.smtpServerPort = "25";
// 设置邮件发送者邮箱用户
this.smtpUserName = "testAdmin@163.com";
// 邮件发送者邮箱密码
this.smtpPassword = "password";
// 设置传输类型
this.transportType = "smtp";
this.isAuth = true;
this.timeout = 300000;
props = new Properties();
// 存储发送邮件服务器的信息
props.put("mail.smtp.host", smtpServerName);
props.put("mail.smtp.port", smtpServerPort);
props.put("mail.transport.protocal", transportType);
props.put("mail.smtp.auth", isAuth ? "true":"false");
props.put("mail.stmp.timeout", timeout);
}
private Session getSession(){
Authenticator authenticator = null;
if(isAuth){
authenticator = new SimpleAuthenticator(smtpUserName,smtpPassword);
}
Session sendMailSession = Session.getDefaultInstance(props,authenticator);
return sendMailSession;
}
/**
* 发送email
* @param msg
* @return
*/
public void send(){
Session session = this.getSession();//获取邮件服务器连接
Message message = new MimeMessage(session);//创建邮件
try {
Address from = new InternetAddress(senderEmailAddr,displayName);
message.setFrom(from);//发件人
message.setSubject("邮件主题");
message.setSentDate(new Date());//发送时间
List<File> attach = new ArrayList<File>();//附件,此处附件没有文件,只是为了方便示例。
Multipart multiPart = new MimeMultipart();
BodyPart context = new MimeBodyPart();
context.setContent("<html><body><a herf='http://www.baidu.com'>This is a test email ! helloworld..</a><body></html>","text/html;charset=utf-8");
multiPart.addBodyPart(context);
for(File att:attach){
MimeBodyPart attachmentPart = new MimeBodyPart();
//1.如果数据源是byte[]
// DataSource fds = new ByteArrayDataSource(byte[] b,"application/octet-stream");
//2.如果内容是file
DataSource fds = new FileDataSource(att);
attachmentPart.setDataHandler(new DataHandler(fds));
attachmentPart.setFileName(MimeUtility.encodeText(att.getName(),"utf-8",null)); // 解决附件名称乱码
multiPart.addBodyPart(attachmentPart);
}
message.setContent(multiPart);
message.setRecipients(RecipientType.TO, new Address[]{new InternetAddress("1111@163.com")});//发送
message.setRecipients(RecipientType.CC, new Address[]{new InternetAddress("2222@163.com")});//抄送
message.setRecipients(RecipientType.BCC, new Address[]{new InternetAddress("333@163.com")});//暗送
//发送邮件
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
//======================================================================
class SimpleAuthenticator extends Authenticator {
private String user;
private String pwd;
public SimpleAuthenticator(String user, String pwd) {
this.user = user;
this.pwd = pwd;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pwd);
}
}
}
相关文章推荐
- java发送邮件附件名称乱码问题
- Android4.1.2 Email发送邮件附件中文名乱码问题
- android 原生 电子邮件 应用 发送邮件附带 中文名附件时 附件名称乱码问题解决
- vbs处理数组 和 Jmail发送带附件邮件 乱码
- Java 使用 Velocity 发送邮件中文内容乱码问题的解决办法
- SmtpClient发送邮件时附件名称乱码 推荐
- 关于javaMail的发送邮件之简便方法(邮件带有附件及html内容中携带图标)
- .Net通过OutLook发送邮件,附件的名称太长会显示乱码
- 邮件发送后附件名称或下载附件名称是乱码的解决(C++)
- java发送邮件附件.网页.文本类型的实现(Content的内容设计)
- 实现javaEmail邮件发送功能并带有生成的pdf附件
- javaMail发送邮件,标题/发件人/附件名称乱码
- C# 发送邮件,附件带中文名称的Excel乱码问题
- java实现发送邮件可多个附件内嵌图片-commons-email使用(未测试)
- Java邮件开发(三):解决附件名为乱码及显示友好名称
- 强大的java编程实现邮件发送功能,支持内容,上传多个图片和多个附件
- java发送带附件的邮件/附件图片显示在邮件内容中
- Java邮件开发(三):解决附件名为乱码及显示友好名称
- java mail发送邮件群发 带附件的发送(源代码+详细目录结构)java Email 发送实例
- java实现发送邮件可多个附件内嵌图片-commons-email使用