各大公司应该都会有发送邮件的需求。特别是人事系统。java作为后台的王牌语言。在这里我就以java为例向大家介绍一下用java发送邮件。
我们以QQ邮箱发送邮件为例,需要申请一个SMTP密码。登录QQ邮箱
先点击设置
然后点击账户
往下找,找到一个IMAP/SMTP服务
点击开启。发送短信。获取一个SMTP密码。最终填在 SEND_PWD 这个属性值上。
然后不废话了。直接上代码。
package com.yz.product.util;
import java.io.File;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
import org.junit.Test;
public class SendmailUtil {
// 设置服务器
private static String KEY_SMTP = "mail.smtp.host";
private static String VALUE_SMTP = "smtp.qq.com";
// 服务器验证
private static String KEY_PROPS = "mail.smtp.auth";
private static boolean VALUE_PROPS = true;
//QQ邮箱地址
private String SEND_USER = "";
//QQ号
private String SEND_UNAME = "";
//SMTP密码
private String SEND_PWD = "";
// 建立会话
private MimeMessage message;
private Session s;
/*
* 初始化方法
*/
public SendmailUtil() {
Properties props = System.getProperties();
props.setProperty(KEY_SMTP, VALUE_SMTP);
props.setProperty("mail.smtp.starttls.enable", "true");
props.put(KEY_PROPS, "true");
// props.put("mail.smtp.auth", "true");
s = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SEND_UNAME, SEND_PWD);
}
});
s.setDebug(true);
message = new MimeMessage(s);
}
/**
* 发送邮件
*
* @param headName
*
邮件头文件名
* @param sendHtml
*
邮件内容
* @param receiveUser
*
收件人地址
*/
public void doSendHtmlEmail(String headName, String sendHtml, String receiveUser) {
try {
// 发件人
InternetAddress from = new InternetAddress(SEND_USER);
message.setFrom(from);
// 收件人
InternetAddress to = new InternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO, to);
// 邮件标题
message.setSubject(headName);
String content = sendHtml.toString();
// 邮件内容,也可以使纯文本"text/plain"
message.setContent(content, "text/html;charset=GBK");
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
// 发送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("send success!");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送邮件
*
* @param headName
*
邮件头文件名
* @param sendHtml
*
邮件内容
* @param receiveUser
*
多个收件人地址
*/
public void doSendmallHtmlEmail(String headName, String sendHtml, List<String> receiveUser) {
try {
// 发件人
InternetAddress from = new InternetAddress(SEND_USER);
message.setFrom(from);
// 收件人
final int num = receiveUser.size();
InternetAddress[] addresses = new InternetAddress[num];
for (int i = 0; i < num; i++) {
addresses[i] = new InternetAddress(receiveUser.get(i));
}
message.setRecipients(Message.RecipientType.TO, addresses);
// 邮件标题
message.setSubject(headName);
String content = sendHtml.toString();
// 邮件内容,也可以使纯文本"text/plain"
message.setContent(content, "text/html;charset=GBK");
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
// 发送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("send success!");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送邮件带附件
*
* @param subject
*
邮件主题
* @param sendHtml
*
邮件内容
* @param receiveUser
*
收件人地址
* @param attachment
*
附件
*/
public void doSendHtmlAttachEmail(String subject, String sendHtml, String receiveUser, File attachment) {
try {
// 发件人
InternetAddress from = new InternetAddress(SEND_USER);
message.setFrom(from);
// 收件人
InternetAddress to = new InternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO, to);
// 邮件主题
message.setSubject(subject);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 添加邮件正文
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(sendHtml, "text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
// 添加附件的内容
if (attachment != null) {
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
multipart.addBodyPart(attachmentBodyPart);
}
// 将multipart对象放到message中
message.setContent(multipart);
// 保存邮件
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
// 发送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("send success!");
} catch (Exception e) {
e.printStackTrace();
}
}
}