简单的邮件发送mail.jar

时间:2023-03-10 06:36:53
简单的邮件发送mail.jar
public class MailSender {
final static Logger logger = Logger.getLogger(MailSender.class);
/**
* 发送简单邮件
* @param str_from:发件人地址
* @param str_to:收件人地址
* @param str_title:邮件标题
* @param str_content:邮件正文
* @param emailPwd:邮件密码
*/
@SuppressWarnings("finally")
public static void send(String str_to, String str_title,String str_content){
boolean flog=false;
logger.info("sending... To: " + str_to + " Mail Title: " + str_title + " fileAttachment: " + str_content);
Properties props = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try {
props.load(classLoader.getResourceAsStream("mail.properties")); //使用property读取文件
} catch (IOException e) {
e.printStackTrace();
}
String str_from = props.getProperty("mail.from");
String emailPwd = props.getProperty("mail.password");

final String mailServer_163 = props.getProperty("mail.smtp.163.mailServer");
final String mailServer_qq = props.getProperty("mail.smtp.qq.mailServer");
final String mailServer_Sina = props.getProperty("mail.smtp.sina.mailServer");
String mailServer = "";
String emailType = Tools.subString(str_from, "@"); //截取@前
String eName = Tools.subberString(str_from,"@");
if (emailType.trim() != "" && emailType.equals("qq.com")) {
mailServer = mailServer_qq;
}
if (emailType.trim() != "" && emailType.equals("163.com")) {
mailServer = mailServer_163;
}
if (emailType.trim() != "" && emailType.equals("sina.com.cn")) {
mailServer = mailServer_Sina;
}
if (emailType.trim() != "" && emailType.equals("sina.com")) {
mailServer = mailServer_Sina;
}
// 存储发送邮件服务器的信息
props.put("mail.smtp.host", mailServer);
// 同时通过验证
props.put("mail.smtp.auth", "true");
// 根据属性新建一个邮件会话
Session s = Session.getInstance(props);
s.setDebug(true); // 有他会打印一些调试信息。
// 由邮件会话新建一个消息对象
MimeMessage message = new MimeMessage(s);
// 设置邮件
InternetAddress from = null;
try {
from = new InternetAddress(str_from);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // pukeyouxintest2@163.com
try {
message.setFrom(from);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 设置发件人的地址
// //设置收件人,并设置其接收类型为TO
InternetAddress to = null;
try {
to = new InternetAddress(str_to);
} catch (AddressException e) {
e.printStackTrace();
}
try {
message.setRecipient(Message.RecipientType.TO, to);
// 设置标题
message.setSubject(str_title); // java学习
// 设置信件内容
// message.setText(str_content); //发送文本邮件 //你好吗?
message.setContent(str_content, "text/html;charset=gbk"); // 发送HTML邮件
// 设置发信时间
message.setSentDate(new Date());
// 存储邮件信息
message.saveChanges();
} catch (MessagingException e) {
e.printStackTrace();
} // 发送邮件
Transport transport = null;
try {
transport = s.getTransport("smtp");
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
try {
transport.connect(mailServer, eName, emailPwd) ;
} catch (MessagingException e) {
e.printStackTrace();
}
// 发送邮件,其中第二个参数是所有已设好的收件人地址
try {
transport.sendMessage(message, message.getAllRecipients());
} catch (MessagingException e) {
e.printStackTrace();
}
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
} } /**
* 异步发送邮件的方法*/
public static void sendMailByAsynchronous(final String str_to, final String str_title, final String str_content) throws Exception{
new Thread(new Runnable() {
public void run() {
try {
MailSender.send(str_to, str_title, str_content);
} catch (Exception ex) {
logger.error("mail sender error To: " + str_to
+ " Mail Title: " + str_title + " fileAttachment: "
+ str_content, ex);
}
}
}).start();
} }

 mail.properties

mail.smtp.163.mailServer=smtp.163.com

mail.smtp.qq.mailServer=smtp.qq.com

mail.smtp.sina.mailServer=smtp.sina.com.cn

mail.username=asd

mail.password=123456

mail.from=asd@163.com

 Tool.java

public class Tools {
public static String subString(String content,String start){
String subConetent=content.substring(content.indexOf(start)+1, content.length());
return subConetent;
}
public static String subberString(String content,String end){
String subConetent=content.substring(0,content.indexOf(end));
return subConetent;
}
}

调用方法:

MailSender.sendMailByAsynchronous("123@126.com","topic:"+a.getAskTitle(), "content:"+a.getAskContent()+"/n请你及时回复");

jar包:mai.jar