一、JavaMail API简介
JavaMail API是读取、撰写、发送电子信息的可选包。
可用它来建立如Eudora、Foxmail、MS Outlook Express一般的邮件用户代理程序(Mail User Agent,简称MUA,而非邮件传输代理MTA)。
从另外一个角度来看,我们这些电子邮件用户日常用MUA程序来读写邮件,而MUA依赖着MTA处理邮件的递送。
二、相关协议一览
不依赖于协议的方式发送接收电子信息,只依赖特定协议SMTP、POP、IMAP、MIME、NNTP协议。
1.SMTP(递送邮件机制
)
简单邮件传输协议
SMTP服务器将邮件转发到接收者的SMTP服务器,直至最后被接收者通过POP或者IMAP协议获取。
2.POP(获取邮件机制)
邮局协议,目前为第3个版本POP3
3.IMAP(多目录共享)
接收信息的高级协议,目前版本为第4版IMAP4
接收新信息,将这些信息递送给用户,维护每个用户的多个目录。
4.MIME
邮件扩展内容格式:信息格式、附件格式等等
5.NNTP
第三方新闻组协议
三、代码实现
public class Email {
/**配置session属性*/
Properties props;
/**发送者*/
String sender;
/**发送者邮箱账号*/
String username;
/**发送者邮箱密码*/
String password;
/**接收者*/
String receiver;
/**标题*/
String title;
/**邮件内容*/
String content;
/**附件*/
List<DataSource> attachments;
/**
* 发送邮件
* @param email 邮件信息
*/
public static void sendEmail(Email email) {
// 邮件配置信息
Properties props = email.getProps();
//有他会打印一些调试信息。
// session.setDebug(true);
String sender = email.getSender();
String recevier = email.getReceiver();
String title = email.getTitle();
String content = email.getContent();
String username = email.getUsername();
String password = email.getPassword();
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
// 根据属性新建一个邮件会话
Session session = Session.getInstance(props, authenticator);
List<DataSource> attachs = email.getAttachments();
// 由邮件会话新建一个消息对象
MimeMessage message = new MimeMessage(session);
try {
// 设置发件人的地址
message.setFrom(new InternetAddress(sender));
// 设置收件人,并设置其接收类型为TO
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recevier));
// 设置标题
message.setSubject(title);
// 设置信件内容 发送 纯文本 邮件 todo
// message.setText(mailContent);
// 发送HTML邮件,内容样式比较丰富
//message.setContent(mailContent, "text/html;charset=utf-8");
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart("mixed");
// 设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
//contentPart.setText(content);
contentPart.setContent(content,"text/html;charset=utf-8");
multipart.addBodyPart(contentPart);
Base64 base64=new Base64();
//BASE64Encoder enc = new BASE64Encoder();
for(DataSource attach : attachs){
// 添加附件
BodyPart messageAttachs = new MimeBodyPart();
// 添加附件的内容
messageAttachs.setDataHandler(new DataHandler(attach));
// 添加附件的标题
messageAttachs.setFileName(MimeUtility.encodeText(attach.getName()));
multipart.addBodyPart(messageAttachs);
}
// 将multipart对象放到message中
message.setContent(multipart);
// 设置发信时间
message.setSentDate(new Date());
// 存储邮件信息
message.saveChanges();
// 发送邮件
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.host", "smtp.163.com");
String sender = "*******@163.com"; //你的邮箱
String receiver = "******@qq.com"; //接收人邮箱
String title = "你好、Hello Mail";
String content = "Hello World!!!";
String username = "*******@163.com"; //你的邮箱
//535 Error:authentication failed解决方法(若开启了授权码,则登陆密码为你的授权码,否则会报如左的错)
String password = "*****"; //授权码是用于登录第三方邮件客户端的专用密码
List<DataSource> attachments = new ArrayList<>();
DataSource dataSource1 = new FileDataSource("src/main/resources/image/你好.txt");
DataSource dataSource2 = new FileDataSource("src/main/resources/image/code.png");
attachments.add(dataSource1);
attachments.add(dataSource2);
Email email = new Email(props, sender, username, password, receiver, title, content, attachments);
EmailUtils.sendEmail(email);
}