Javamail简单示例

2017-01-13 15:03:12来源:csdn作者:ksj_j人点击


Javamail代码


代码示例:


public class SendEmail {//发送人
private final static String from = "*******@163.com"; //邮箱发送协议
private final static String PROTOCOL = "smtp";//smtp邮箱服务器
private final static String HOST = "smtp.163.com";//smtp邮箱默认端口
private final static String PORT = "25";//是否要求身份认证
private final static String AUTH = "true";//是否开启调试
private final static String IS_ENABLED_DEBUG_MOD = "true";//连接邮箱服务器会话信息
private static Properties props = null;//初始化会话信息
static{
props = new Properties();
props.setProperty("mail.transport.protocol", PROTOCOL);
props.setProperty("mail.smtp.host", HOST);
props.setProperty("mail.smtp.port", PORT);
props.setProperty("mail.smtp.auth", AUTH);
//调试信息
// props.setProperty("mail.debug",IS_ENABLED_DEBUG_MOD);
}
//发送验证码,to:发送者邮箱
public static void sendEmailCode(String to,String code) throws AddressException, MessagingException{
Session session = Session.getInstance(props, new MyAuthenticator());
//构建html发送格式
MimeMessage message = new MimeMessage(session);
//设置发送人
message.setFrom(new InternetAddress(from));
//设置邮件标题
message.setSubject("ostp邮箱注册", "utf-8");
//设置接收者
message.setRecipient(RecipientType.TO, new InternetAddress(to));
// 抄送CC
//message.setRecipient(RecipientType.CC, new InternetAddress("******@qq.com"));
// 密送 (不会在邮件收件人名单中显示出来)BCC
//message.setRecipient(RecipientType.BCC, new InternetAddress("******@qq.com"));
//设置发送时间
message.setSentDate(new Date());
message.setContent("<span>你好你的邮箱验证码是"+code+"</span>", "text/html;charset=utf-8");
message.saveChanges();
Transport.send(message);
}//推送邮箱,只有图片(推送邮箱)
public static void SendPushMessage(String imgUrl) throws MessagingException{
Session session = Session.getInstance(props, new MyAuthenticator());
//构建html发送格式
MimeMessage message = new MimeMessage(session);
//设置发送人
message.setFrom(new InternetAddress(from));
//设置邮件标题
message.setSubject("XXXX", "utf-8");
//设置接收者
message.setRecipient(RecipientType.TO, new InternetAddress("*******@qq.com"));
MimeMultipart mp = new MimeMultipart("related");
message.setContent(mp);
MimeBodyPart imgBody = new MimeBodyPart();
MimeBodyPart htmlBody = new MimeBodyPart();
mp.addBodyPart(imgBody);
mp.addBodyPart(htmlBody);
DataSource ds = new FileDataSource(imgUrl);
DataHandler dh = new DataHandler(ds);
imgBody.setDataHandler(dh);
imgBody.setContentID("XXX.jpg");
htmlBody.setContent("推送信息<img src='cid:XXX.jpg' style='width:50px;height:50px' />", "text/html;charset=utf-8");
message.saveChanges();
Transport.send(message);
}//发送文件
public static void sendMailAttach(String attachUrl,String imgUrl) throws MessagingException, UnsupportedEncodingException{
Session session = Session.getInstance(props, new MyAuthenticator());
//构建html发送格式
MimeMessage message = new MimeMessage(session);
//设置发送人
message.setFrom(new InternetAddress(from));
//设置邮件标题
message.setSubject("ostp邮箱注册", "utf-8");
//设置接收者
message.setRecipient(RecipientType.TO, new InternetAddress("*******@qq.com"));
//设置一个复杂类型数据(带有附件和图片)
MimeMultipart mp = new MimeMultipart("mixed");
message.setContent(mp);
//附件
MimeBodyPart attach = new MimeBodyPart();
//内容
MimeBodyPart mailBody = new MimeBodyPart();
mp.addBodyPart(attach);
mp.addBodyPart(mailBody);
//附件的设置
DataSource ds = new FileDataSource(attachUrl);
DataHandler dh = new DataHandler(ds);
attach.setFileName(MimeUtility.encodeText("qq.png"));
attach.setDataHandler(dh);
//设置一个带有图片mimemultipart
MimeMultipart bodyMultipart = new MimeMultipart("related");
mailBody.setContent(bodyMultipart);
MimeBodyPart htmlBody = new MimeBodyPart();
MimeBodyPart imgbBody = new MimeBodyPart();
bodyMultipart.addBodyPart(htmlBody);
bodyMultipart.addBodyPart(imgbBody);
// 正文图片
DataSource ds3 = new FileDataSource(imgUrl);
DataHandler dh3 = new DataHandler(ds3);
imgbBody.setDataHandler(dh3);
imgbBody.setContentID("sample.png");
htmlBody.setContent("<span style='color:red'>欢迎来到ostp!" +
"<img src='cid:sample.png' /></span>", "text/html;charset=utf-8");message.saveChanges();
Transport.send(message);
} /**
* 自定义认证信息
*/
static class MyAuthenticator extends Authenticator {private String username = "********@163.com";private String password = "*******";public MyAuthenticator() {
super();
}public MyAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
}@Override
protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);
}
}public static void main(String[] args) throws AddressException, MessagingException, UnsupportedEncodingException {//发送验证码
sendEmailCode("*******@qq.com","123456");
//发送推广图
SendPushMessage("c://mailtmp/aa.jpg");
//发送文件
sendMailAttach("c://mailtmp/qq.png", "c://mailtmp/aa.jpg");
}}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台