java 发送邮件
来源:互联网 发布:单页面应用网站源码 编辑:程序博客网 时间:2024/04/30 08:06
package com.xu.email.send;
import java.util.Properties;
import java.util.regex.Pattern;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class XEmail {
private static final String EMAIL="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";//邮箱
private String ACCOUNT="************@163.com";//发送者的邮箱账号
private String PASSWORD="**********"; //发送者的邮箱密码
private String HOST = "smtp.163.com"; //邮箱服务器
private String PROTOCOL = "smtp"; //邮箱协议
// private int PORT = 25; //服务器端口
private Session session;
private Message message ;
private boolean debug=false;//默认关闭debug
private boolean isSuccess=false;
/**
* 构造方法
*/
public XEmail() {
Properties properties=new Properties();//Properties 属性文件
properties.setProperty("mail.debug",debug+"");//开启debug模式显示发送详情 可以不写
properties.setProperty("mail.smtp.auth","true");// 发送服务器需要身份验证
properties.setProperty("mail.host",HOST); // 设置邮件服务器主机名
properties.setProperty("mail.transport.protocol",PROTOCOL);// 发送邮件协议名称
session =Session .getInstance(properties);// 设置环境信息
message =new MimeMessage(session); // 创建邮件对象
}
/**
* @param debug 是否开启debug调试
*/
public void setDebug(boolean debug) {
this.debug = debug;
}
/**
* 验证邮箱
* @param email 邮箱账号
*/
private boolean isEmail(String email){
boolean result=false;
if(!Pattern.matches(EMAIL, email)){
result= false;
throw new RuntimeException("The type of email is wrong! Please check it until suitable.");
}else{
result= true;
}
return result;
}
/**
* 发送邮件
* @param subject 要发送的邮件的主题
* @param content 要发送的邮件的类容
* @param email 接受者邮箱的账号
* @return 是否发送成功
* @throws MessagingException 异常
*/
public boolean sendEmail(String subject,String content,String email){
if(isEmail(email)){
try {
message.setSubject(subject);// 设置发送主题
message.setText(content); // 设置邮件内容
message.setFrom(new InternetAddress(ACCOUNT));// 设置发件人
Transport transport =session.getTransport();// 连接邮件服务器
transport.connect(ACCOUNT,PASSWORD);// 登录邮件服务器 账号/密码
transport.sendMessage(message, new Address[] {new InternetAddress(email)});//设置邮件发送人邮箱-email
transport.close(); // 关闭连接
isSuccess=true;
} catch (Exception e) {
isSuccess=false;
throw new RuntimeException(e.getMessage());
}
}
return isSuccess;
}
}
import java.util.Properties;
import java.util.regex.Pattern;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class XEmail {
private static final String EMAIL="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";//邮箱
private String ACCOUNT="************@163.com";//发送者的邮箱账号
private String PASSWORD="**********"; //发送者的邮箱密码
private String HOST = "smtp.163.com"; //邮箱服务器
private String PROTOCOL = "smtp"; //邮箱协议
// private int PORT = 25; //服务器端口
private Session session;
private Message message ;
private boolean debug=false;//默认关闭debug
private boolean isSuccess=false;
/**
* 构造方法
*/
public XEmail() {
Properties properties=new Properties();//Properties 属性文件
properties.setProperty("mail.debug",debug+"");//开启debug模式显示发送详情 可以不写
properties.setProperty("mail.smtp.auth","true");// 发送服务器需要身份验证
properties.setProperty("mail.host",HOST); // 设置邮件服务器主机名
properties.setProperty("mail.transport.protocol",PROTOCOL);// 发送邮件协议名称
session =Session .getInstance(properties);// 设置环境信息
message =new MimeMessage(session); // 创建邮件对象
}
/**
* @param debug 是否开启debug调试
*/
public void setDebug(boolean debug) {
this.debug = debug;
}
/**
* 验证邮箱
* @param email 邮箱账号
*/
private boolean isEmail(String email){
boolean result=false;
if(!Pattern.matches(EMAIL, email)){
result= false;
throw new RuntimeException("The type of email is wrong! Please check it until suitable.");
}else{
result= true;
}
return result;
}
/**
* 发送邮件
* @param subject 要发送的邮件的主题
* @param content 要发送的邮件的类容
* @param email 接受者邮箱的账号
* @return 是否发送成功
* @throws MessagingException 异常
*/
public boolean sendEmail(String subject,String content,String email){
if(isEmail(email)){
try {
message.setSubject(subject);// 设置发送主题
message.setText(content); // 设置邮件内容
message.setFrom(new InternetAddress(ACCOUNT));// 设置发件人
Transport transport =session.getTransport();// 连接邮件服务器
transport.connect(ACCOUNT,PASSWORD);// 登录邮件服务器 账号/密码
transport.sendMessage(message, new Address[] {new InternetAddress(email)});//设置邮件发送人邮箱-email
transport.close(); // 关闭连接
isSuccess=true;
} catch (Exception e) {
isSuccess=false;
throw new RuntimeException(e.getMessage());
}
}
return isSuccess;
}
}
0 0
- [java]Java发送邮件
- java 邮件发送 --- 简单的邮件发送
- JAVA发送邮件及springboot发送邮件
- 用java发送邮件
- 用Java发送邮件
- java发送邮件源码
- java发送邮件
- Java发送邮件
- Java发送邮件程序
- java发送Email邮件
- 怎样用java发送邮件
- 利用java发送邮件
- java 邮件发送
- java邮件发送
- java邮件发送
- java定时发送邮件
- java发送邮件代码
- Java Mail 发送邮件
- Insertion Sort List ---LeetCode
- 一种监听键盘弹起放下和横竖屏切换的方法
- CentOS学习21_ CentOS 配置防火墙操作实例(启、停、开、闭端口)
- Wins10系统忘记开机密码快速解锁方法(图文教程)
- CVPR 2016-11-23
- java 发送邮件
- 类的构造方法
- 新闻发布项目——业务逻辑层(commentServiceImpl)
- [NOIP2014]飞扬的小鸟
- linux
- java中静态代码块的用法 static用法详解
- 深度学习--基于卷积神经网络的歌唱嗓音识别
- Java系列(十五)__Java常用类库(1)
- 创建CSS 样式表有三种方式