前提:
mail.jar导入
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.activation.DataHandler;
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.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import com.sun.mail.util.MailSSLSocketFactory;
/**
* 邮件管理器
* java 实现邮件的发送, 抄送及多附件
*/
public class EmailManager {
private static String smtpHost = "smtp.163.com"; //邮箱服务器
private static String userName = "xxxxxxxx@163.com"; //邮箱地址
private static String userPwd = "xxxxxxx"; //用户密码
private static String sendName = "xxxxxxx"; //发送邮件用户名
private static Session session;
public EmailManager() {
Properties prop=new Properties(); // 配置
prop.put("mail.host",smtpHost); // 设置邮件服务器主机名,这里是163
prop.put("mail.transport.protocol", "smtp"); // 发送邮件协议名称
prop.put("mail.smtp.auth", true); // 是否认证
try {
// SSL加密
MailSSLSocketFactory sf = null;
sf = new MailSSLSocketFactory();
Multipart mp = new MimeMultipart();
// 设置信任所有的主机
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
// 创建会话对象
session = Session.getDefaultInstance(prop, new Authenticator() {
// 认证信息,需要提供"用户账号","授权码"
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, userPwd);
}
});
// 是否打印出debug信息
session.setDebug(false);
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendMail(String toEmail,String main,String[] fileList) throws UnsupportedEncodingException{
Message mimeMsg = new MimeMessage(session); // MIME邮件对象
Multipart mPart = new MimeMultipart(); //// Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
try{
// 邮件发送者
mimeMsg.setFrom(new InternetAddress(userName,sendName));
// 邮件接受者
mimeMsg.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
// 邮件主题
mimeMsg.setSubject("xxxx大学-校友通讯客户端");
BodyPart bodyPart = new MimeBodyPart();
String content = "<html><head></head><body>"+main+"</body></html>";
bodyPart.setContent(content, "text/html;charset=utf-8");
mPart.addBodyPart(bodyPart);
// 设置附件
if (fileList != null && fileList.length > 0) {
for (int i = 0; i < fileList.length; i++) {
bodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileList[i]);
bodyPart.setDataHandler(new DataHandler(fds));
bodyPart.setFileName(MimeUtility.encodeText(fds.getName(), "UTF-8", "B"));
mPart.addBodyPart(bodyPart);
}
}
mimeMsg.setContent(mPart);
mimeMsg.saveChanges();
// 邮件发送
Transport transport = session.getTransport();
transport.connect();
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
transport.close();
} catch (MessagingException e) {
if(e.getMessage().indexOf("SMTP host")>0){
System.out.println("连接不上网络,检查网络连接是否正确!");
}
System.out.println(e.getMessage());
}
}
}
网友评论