标签:mod print abc tde public 没有 for trim mail
? ? 近期公司的项目用到了Java Mail来发送注冊邮件,只是。开发的时候都是使用封装好的JAR,曾经也不是非常了解Java Mail的使用原理。
网上非常多代码都是仅仅有一部分,看一看也跑不起来,今天正好有时间,自己写了一个实现。放在了自己的工具JAR里。闲话不多说,我的项目是使用Gradle构建的,依赖的引用例如以下:
//JavaMail
compile(‘javax.mail:mail:1.4.7‘)
构建后应该引用了两个JAR:。假设第二个JAR没有的话。大家能够去Maven库找一下。就不多讲述了。
实现的源代码例如以下,一般直接拿来就能够用了~
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
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;
/**
* Java Mail 工具类
*
* @author XueQi
* @version 1.0
*
*/
public class MailUtils {
private static String host;
private static String username;
private static String password;
private static String from;
private static String nick;
static {
try {
// Test Data
host = "smtp.163.com";
username = "邮箱用户名";
password = "邮箱密码";
from = "xxx@abc.com";
nick = "測试admin";
// nick + from 组成邮箱的发件人信息
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送邮件
*
* @param to
* 收件人列表。以","切割
* @param subject
* 标题
* @param body
* 内容
* @param filepath
* 附件列表,无附件传递null
* @return
* @throws MessagingException
* @throws AddressException
* @throws UnsupportedEncodingException
*/
public static boolean sendMail(String to, String subject, String body,
List<String> filepath) throws AddressException, MessagingException,
UnsupportedEncodingException {
// 參数修饰
if (body == null) {
body = "";
}
if (subject == null) {
subject = "无主题";
}
// 创建Properties对象
Properties props = System.getProperties();
// 创建信件server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true"); // 通过验证
// 得到默认的对话对象
Session session = Session.getDefaultInstance(props, null);
// 创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
nick = MimeUtility.encodeText(nick);
msg.setFrom(new InternetAddress(nick + "<" + from + ">"));
// 创建收件人列表
if (to != null && to.trim().length() > 0) {
String[] arr = to.split(",");
int receiverCount = arr.length;
if (receiverCount > 0) {
InternetAddress[] address = new InternetAddress[receiverCount];
for (int i = 0; i < receiverCount; i++) {
address[i] = new InternetAddress(arr[i]);
}
msg.addRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// 后面的BodyPart将增加到此处创建的Multipart中
Multipart mp = new MimeMultipart();
// 附件操作
if (filepath != null && filepath.size() > 0) {
for (String filename : filepath) {
MimeBodyPart mbp = new MimeBodyPart();
// 得到数据源
FileDataSource fds = new FileDataSource(filename);
// 得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
// 得到文件名称相同至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(body);
mp.addBodyPart(mbp);
// 移走集合中的全部元素
filepath.clear();
// Multipart增加到信件
msg.setContent(mp);
} else {
// 设置邮件正文
msg.setText(body);
}
// 设置信件头的发送日期
msg.setSentDate(new Date());
msg.saveChanges();
// 发送信件
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(msg,
msg.getRecipients(Message.RecipientType.TO));
transport.close();
return true;
} else {
System.out.println("None receiver!");
return false;
}
} else {
System.out.println("None receiver!");
return false;
}
}
public static void main(String[] args) throws AddressException,
UnsupportedEncodingException, MessagingException {
List<String> filepath = new ArrayList<>();
filepath.add("E:\\Resources\\Development Test\\AuctionServer\\src\\main\\java\\com\\auction\\dao\\IBaseDAO.java");
filepath.add("E:\\Resources\\Development Test\\AuctionServer\\src\\main\\java\\com\\auction\\dao\\IMemcacheDAO.java");
sendMail("000000@qq.com,000002@live.cn", "注冊信息邮件", "注冊邮件,有附件",
filepath);
}
}
我这里用的是163的邮箱,測试是成功的~
能够看到两个收件人和两个附件
标签:mod print abc tde public 没有 for trim mail
原文地址:https://www.cnblogs.com/xfgnongmin/p/10640913.html