最近闲的没事, 写了个使用javaMail 发送邮件的小程序
1:新建maven 项目
2:导入jar包依赖
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.2</version>
</dependency>
3:直接贴代码实现发送邮件了
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;
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 = "邮箱账号";
nick = "admin";
// nick + from 组成邮箱的发件人信息
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean sendMail(String to, String subject, String body,
List<String> filePath) throws UnsupportedEncodingException,
AddressException, MessagingException {
//
if (body == null) {
body = "";
}
if (subject == null) {
subject = "";
}
// 创建properties 对象
Properties props = System.getProperties();
// 创建信件服务器
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);
// 得到附件本身
mbp.setDataHandler(new DataHandler(fds));
mp.addBodyPart(mbp);
}
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(body, "UTF-8");
mp.addBodyPart(mbp);
// 移走集合中的所有元素
filePath.clear();
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 {
return false;
}
}
public static void main(String[] args) throws AddressException,
UnsupportedEncodingException, MessagingException {
List<String> filepath = new ArrayList<String>();
filepath.add("F:/51/1.jpg");
filepath.add("F:/51/2.jpg"); //
sendMail("接收邮件账号,接收邮件账号,接收邮件账号",
"邮箱测试", "测试发送多人,多附件", filepath);
}
}
附:这里使用的是163邮箱, 使用腾讯QQ 邮箱没成功,不是报530 就是535 的 不知道为何,我也开通了POP3/SMTP服务
有成功的小伙伴可以给我留个言,或者直接投稿到我专题页OK ,谢谢,希望互相学习,一起进步