Java 发邮件
Java 发邮件
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
package cn.gszhang.common.base.utils;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
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 EmailUtis {
private Properties props; //系统属性
private Session session; //邮件会话对象
private MimeMessage mimeMsg; //MIME邮件对象
/**
* Constructor
* @param smtp 邮件发送服务器
*/
public EmailUtis(String smtp, String username, String password){
props = System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host", smtp);
props.put("username", username);
props.put("password", password);
session = Session.getDefaultInstance(props, null);
session.setDebug(true);
mimeMsg = new MimeMessage(session);
}
public boolean sendMail(String from, String[] to, String[] copyto, String subject, String content, String[] fileName) {
try {
//设置发信人
mimeMsg.setFrom(new InternetAddress(from));
//设置接收人
for (int i = 0; i < to.length; i++) {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i]));
}
//设置抄送人
for (int i = 0; i < copyto.length; i++) {
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyto[i]));
}
//设置主题
mimeMsg.setSubject(subject);
if(null!=fileName){
BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象
mdp.setContent(content, "text/html;charset=UTF-8");// 给BodyPart对象设置内容和格式/编码方式
Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对象
mm.addBodyPart(mdp);// 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
MimeBodyPart filePart ;
FileDataSource fileDataSource ;
for (int j = 0; j < fileName.length; j++) {
mdp = new MimeBodyPart();
filePart = new MimeBodyPart();
fileDataSource = new FileDataSource(fileName[j]);
filePart.setDataHandler(new DataHandler(fileDataSource));
try {
filePart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
} catch (Exception e) {
e.printStackTrace();
}
mm.addBodyPart(filePart);
}
mimeMsg.setContent(mm);
}else{
BodyPart mdp = new MimeBodyPart();
mdp.setContent(content, "text/html;charset=UTF-8");// 给BodyPart对象设置内容和格式/编码方式
Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对象
mm.addBodyPart(mdp);// 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
mimeMsg.setContent(mm);
}
//发送邮件
if(props.get("mail.smtp.auth").equals("true")){
Transport transport = session.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"), (String)props.get("username"), (String)props.get("password"));
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
transport.close();
}else{
Transport.send(mimeMsg);
}
System.out.println("邮件发送成功");
} catch (MessagingException e) {
e.printStackTrace();
}
return true;
}
public static void main(String[] args) {
String smtp = "smtp.163.com";
String username="zhgs_cq@163.com";
String password=""; //填写邮箱的密码
String from = "zhgs_cq@163.com";
String[] to = {"470417164@qq.com,973669404@qq.com"};
String[] copyto = {"973669404@qq.com"};
String subject = "主题";
String content = "邮件内容";
String[] fileName = {"D:\\WI-FI密码.txt","D:\\张高松 Java软件工程师.doc"};
// String[] fileName = null;// {"D:\\WI-FI密码.txt"};
EmailUtis email = new EmailUtis(smtp, username, password);
email.sendMail(from, to, copyto, subject, content, fileName);
}
}