📄 sendemailutil.java
字号:
/**
* @(#) SendEmailUtil.java
* @version 1.0.1
*/
package tools.util;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
public class SendEmailUtil
{
private InternetAddress[] toAddress;
private InternetAddress[] ccAddress;
private InternetAddress[] bccAddress;
private InternetAddress fromAddress;
private InternetAddress[] replyTo;
private String SMTPServer = "smtp.163.com";
private String username = "";
private String password = "";
private String subject = "";
private String content = "";
private Vector attachment = new Vector();
private boolean bHTML = true;
private String[] eaddress;
private String[][] addressArray;
public SendEmailUtil(String to,String from,String username,String password,String subj,String cont){
try{
this.toAddress = InternetAddress.parse(to);
this.fromAddress = new InternetAddress(from);
this.username = username;
this.password = password;
this.subject = subj;
this.content = cont;
}catch(Exception error){
error.printStackTrace();
}
}
public SendEmailUtil(String[] eaddress,String from,String username,String password,String subj,String cont){
try{
this.eaddress = eaddress;
this.fromAddress = new InternetAddress(from);
this.username = username;
this.password = password;
this.subject = subj;
this.content = cont;
String[][] emailArray = new String[this.eaddress.length][2];
for(int i=0;i < this.eaddress.length;i++){
emailArray[i][0] = eaddress[i];
emailArray[i][1] = "false";
}
this.addressArray = emailArray;
}catch(Exception error){
error.printStackTrace();
}
}
public void setSMTPServer(String emailServer){
this.SMTPServer = emailServer;
}
public void setAttachment(String emailAttachment){
this.attachment = explore(emailAttachment,";");
}
public void setHTML(boolean bHTML){
this.bHTML = bHTML;
}
public void setReplyTo(String replyToList){
try{
this.replyTo = InternetAddress.parse(replyToList);
}catch(Exception error){
error.printStackTrace();
}
}
public Vector explore(String handleStr, String pointStr){
Vector v = new Vector();
int pos1,pos2;
try{
if(handleStr.length()>0){
pos1 = handleStr.indexOf(pointStr);
pos2 = 0;
while(pos1 != -1){
v.addElement(handleStr.substring(pos2,pos1));
pos2 = pos1+pointStr.length();
pos1 = handleStr.indexOf(pointStr,pos2);
}
v.addElement(handleStr.substring(pos2));
}
}catch(Exception error){
error.printStackTrace();
}
return v;
}
public String sendEmail() throws MessagingException{
String oneToAddress = this.getOneAddress();
if(oneToAddress!=null){
toAddress = InternetAddress.parse(oneToAddress);
Properties props = new Properties();
Session session;
props.put("mail.smtp.host", SMTPServer);
props.put("mail.smtp.auth", "true");
PopupAuthenticator popA = new PopupAuthenticator(); //邮件安全认证。
PasswordAuthentication pop = popA.performCheck(username, password); //填写用户名及密码
session = Session.getInstance(props, popA);
// session = Session.getDefaultInstance(props, null);
try{
Message msg = new MimeMessage(session);
msg.setFrom(fromAddress);
msg.setRecipients(Message.RecipientType.TO, toAddress);
if(ccAddress!=null)
msg.setRecipients(Message.RecipientType.CC, ccAddress);
if(bccAddress!=null)
msg.setRecipients(Message.RecipientType.BCC, bccAddress);
if(replyTo!=null)
msg.setReplyTo(replyTo);
msg.setSubject(subject);
msg.setSentDate(new Date());
if(attachment.isEmpty()){
if (!bHTML){
msg.setText(content);
}else{
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(content, "text/html; charset=gb2312");
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
msg.setContent(mp);
}
}else{
MimeBodyPart mbp1 = new MimeBodyPart();
if( !bHTML)
mbp1.setText(content);
else
mbp1.setContent(content, "text/html");
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
Enumeration eFile = attachment.elements();
while(eFile.hasMoreElements()){
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource((String)eFile.nextElement().toString());
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
}
msg.setContent(mp);
}
Transport.send(msg);
return "Success";
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
return e.getMessage();
}
}else{
System.out.println("发送完毕");
return "发送完毕";
}
}
public String sendHTMLEmail(String filename) throws MessagingException{
Session session;
Properties props = new Properties();
props.put("mail.smtp.host", SMTPServer);
props.put("mail.smtp.auth", "true");
PopupAuthenticator popA = new PopupAuthenticator();//邮件安全认证。
PasswordAuthentication pop = popA.performCheck("eatba", "05568925952"); //填写用户名及密码
session = Session.getInstance(props, popA);
// Session session = Session.getDefaultInstance(props, null);
try{
Message msg = new MimeMessage(session);
msg.setFrom(fromAddress);
msg.setRecipients(Message.RecipientType.TO, toAddress);
if(ccAddress!=null)
msg.setRecipients(Message.RecipientType.CC, ccAddress);
if(bccAddress!=null)
msg.setRecipients(Message.RecipientType.BCC, bccAddress);
if(replyTo!=null)
msg.setReplyTo(replyTo);
msg.setSubject(subject);
msg.setSentDate(new Date());
//msg with attachment
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setDataHandler(new DataHandler(new FileDataSource(filename)));
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
Transport.send(msg);
return "Success";
}catch(Exception e){
return e.getMessage();
}
}
public class PopupAuthenticator extends Authenticator {
String username = null;
String password = null;
public PopupAuthenticator() {
}
public PasswordAuthentication performCheck(String user, String pass) {
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
public String getOneAddress(){
String email = null;
for(int i=0;i < this.addressArray.length;i++){
if(addressArray[i][1].equals("false")){
email = addressArray[i][0];
addressArray[i][1] = "true";
if(isEmail(email)){
return email;
}
}
}
return email;
}
public void sendMailThreads(int tnum){
try {
Thread[] sendThreadArray = new Thread[tnum];
for(int i = 0;i<tnum;i++){
sendThreadArray[i] = new Thread(this.sendEmail());
}
for(int i = 0;i<tnum;i++){
sendThreadArray[i].start();
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
public boolean isEmail(String str){
str = str.trim();
String regex = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
return str.matches(regex);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -