import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Message.RecipientType;
import javax.mail.Multipart;
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 org.springframework.mail.javamail.JavaMailSenderImpl;
public class MailSender {
public static class Config{
public static Integer port;
public static String host;
public static String from;
public static String charset;
public static String password;
public static String subject;
public static String content;
public static String receiverSeperator;
public static String[] recipients;
static {
InputStream in = null;
try {
// 加载default.properties配置文件
Properties props = new Properties();
in = Config.class.getClassLoader().getResourceAsStream("javaMail.properties");
props.load(in);
// 获取配置的值
port = Integer.parseInt(props.getProperty("port"));
host = props.getProperty("host");
from = props.getProperty("from");
charset = props.getProperty("charset");
password = props.getProperty("password");
content = new String(props.getProperty("content").getBytes("ISO-8859-1"),charset);
subject = new String(props.getProperty("subject").getBytes("ISO-8859-1"),charset);
receiverSeperator = props.getProperty("receiverSeperator");
recipients = props.getProperty("recipients").split(receiverSeperator);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
public static void main(String argc[]) throws MessagingException
{
Config config = new Config();
System.out.println("开始发送邮件。。。。");
send(config, config.subject, config.content, config.recipients);
System.out.println("发送邮件完毕。。。。");
}
public static void send(Config config, String subject, String content, String ...recipients) throws MessagingException{
JavaMailSenderImpl mailSender = getMailSender(config);
MimeMessage message = mailSender.createMimeMessage();
message.setSentDate(new Date());
message.setFrom(new InternetAddress(config.from));
message.setRecipients(RecipientType.TO, addAddress(recipients));
message.setSubject(subject, config.charset);
Multipart mp = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
bp.setContent(content, "text/html; charset=" + config.charset);
mp.addBodyPart(bp);
message.setContent(mp);
mailSender.send(message);
}
private static InternetAddress[] addAddress(String[] address) throws AddressException {
List<InternetAddress> rets = new ArrayList<InternetAddress>(address.length);
for (int i = 0; i < address.length; i++) {
rets.add(new InternetAddress(address[i]));
}
return rets.toArray(new InternetAddress[rets.size()]);
}
private static JavaMailSenderImpl getMailSender(Config config){
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.timeout", "2500");
mailSender.setJavaMailProperties(properties);
mailSender.setHost(config.host);
mailSender.setPort(config.port);
mailSender.setUsername(config.from);
mailSender.setPassword(config.password);
return mailSender;
}
}
111
网友评论