JAvaメール送信ツール
詳細
注意:メール送信のサーバーアドレスとポートは自分の実際の状況に応じて選択する必要があります.qqメールボックスはオープンサービスを設定する必要があります.
メール・セッションのプロパティを設定するには、次の手順に従います.
TestEmail.rar (1.6 MB) ダウンロード回数:0
注意:メール送信のサーバーアドレスとポートは自分の実際の状況に応じて選択する必要があります.qqメールボックスはオープンサービスを設定する必要があります.
package com.lw.email.util;
import java.io.File;
import java.io.StringWriter;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
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;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
/**
*
*/
public class MailSendUtils {
/**
* ,
*/
public static void sendTemplateEmail(String email, String userName) {
//
String type = "2";
//
String toEmail = email;
//
String title = " ";
//
String content = " ,Hello!";
//
// String[] files = { "D:\\123.txt" };
//
String templateName = "template.vm";
//
VelocityContext ctx = new VelocityContext();
ctx.put("userName", userName);
ctx.put("url", "https://www.baidu.com/");
ctx.put("title", title);
// , contents
try {
content = MailSendUtils.getMailContent(templateName, ctx);
} catch (Exception e) {
e.printStackTrace();
}
//
MailSendUtils.sendEmail(type, toEmail, title, content, null);
}
/**
*
*
* @param type
* 1: ;2:HTML ,
* @param toEmail
*
* @param title
*
* @param content
*
* @param files
*
*/
public static boolean sendEmail(String type, String toEmail, String title, String content, String[] files) {
//
MailBody mailInfo = new MailBody();
mailInfo.setValidate(true);
mailInfo.setToAddress(toEmail);
mailInfo.setSubject(title);
mailInfo.setContent(content);
mailInfo.setAttachFileNames(files);
//
MailSendUtils sms = new MailSendUtils();
try {
if ("1".equals(type)) {
return sms.sendTextMail(mailInfo);
} else {
return sms.sendHtmlMail(mailInfo);
}
} catch (Exception e) {
System.out.println(" , !!!");
e.printStackTrace();
return false;
}
}
/**
*
*
* @param mailInfo
*
*/
public boolean sendTextMail(MailBody mailInfo) throws Exception {
//
MailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// ,
authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
// logBefore(logger, " session");
// session
Message mailMessage = new MimeMessage(sendMailSession);
// , outlook
mailMessage.addHeader("X-Priority", "3");
mailMessage.addHeader("X-MSMail-Priority", "Normal");
mailMessage.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
mailMessage.addHeader("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
mailMessage.addHeader("ReturnReceipt", "1");
//
Address from = new InternetAddress(mailInfo.getFromAddress());
//
mailMessage.setFrom(from);
// ,
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
//
mailMessage.setSubject(mailInfo.getSubject());
//
mailMessage.setSentDate(new Date());
//
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
//
Transport.send(mailMessage);
System.out.println(" , !");
return true;
}
/**
* HTML
*
* @param mailInfo
*
*/
public boolean sendHtmlMail(MailBody mailInfo) throws Exception {
//
MailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
// ,
if (mailInfo.isValidate()) {
authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
// session ,
// sendMailSession.setDebug(true);
// session
Message mailMessage = new MimeMessage(sendMailSession);
// , outlook
mailMessage.addHeader("X-Priority", "3");
mailMessage.addHeader("X-MSMail-Priority", "Normal");
mailMessage.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
mailMessage.addHeader("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
mailMessage.addHeader("ReturnReceipt", "1");
//
Address from = new InternetAddress(mailInfo.getFromAddress());
//
mailMessage.setFrom(from);
// ,
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO TO
mailMessage.setRecipient(Message.RecipientType.TO, to);
//
mailMessage.setSubject(mailInfo.getSubject());
//
mailMessage.setSentDate(new Date());
// MiniMultipart , MimeBodyPart
Multipart mainPart = new MimeMultipart();
//
if (mailInfo.getAttachFileNames() != null) {
for (String fileName : mailInfo.getAttachFileNames()) {
// MimeBodyPart
MimeBodyPart file = new MimeBodyPart();
//
DataHandler dataHandler = new DataHandler(new FileDataSource(fileName));
//
file.setDataHandler(dataHandler);
//
file.setFileName(MimeUtility.encodeText(dataHandler.getName()));
mainPart.addBodyPart(file);
}
}
// HTML MimeBodyPart
MimeBodyPart html = new MimeBodyPart();
// HTML
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// MiniMultipart
mailMessage.setContent(mainPart);
//
Transport.send(mailMessage);
System.out.println(" , !");
return true;
}
/**
*
*
* @param templateName
*
* @param velocityContext
*
* @return
*/
public static String getMailContent(String templateName, VelocityContext velocityContext) throws Exception {
//
String templatePath = "\\template\\vm";
StringWriter stringWriter = new StringWriter();
// velocity
VelocityEngine velocityEngine = new VelocityEngine();
//
Properties properties = new Properties();
//
String path = MailSendUtils.class.getResource("/").getPath();
String templateFolder = new File(path).getParentFile().getParentFile().getCanonicalPath() + templatePath;
//
properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, templateFolder);
//
properties.setProperty(Velocity.ENCODING_DEFAULT, "utf-8");
properties.setProperty(Velocity.INPUT_ENCODING, "utf-8");
properties.setProperty(Velocity.OUTPUT_ENCODING, "utf-8");
properties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
properties.setProperty("runtime.log.logsystem.log4j.category", "velocity");
properties.setProperty("runtime.log.logsystem.log4j.logger", "velocity");
//
velocityEngine.init(properties);
//
Template template = velocityEngine.getTemplate(templateName, "utf-8");
//
template.merge(velocityContext, stringWriter);
return stringWriter.toString();
}
}
メール・セッションのプロパティを設定するには、次の手順に従います.
/**
*
*/
public Properties getProperties() {
this.mailServerHost = "mail.junsisoft.com.cn"; //
this.mailServerPort = "25"; // ,465 ssl
this.fromAddress = "[email protected]"; //
this.userName = "[email protected]"; //
this.password = "111111"; //
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
//
p.setProperty("mail.transport.protocol", "smtp");
return p;
}