package com.miaozhen.sitedna.util;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
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 org.apache.log4j.Logger;
public class MailSend {
private static final Logger _logger = Logger.getLogger(MailSend.class.getName());
private MimeMessage mimeMsg;
private Session session;
private Properties props;
private String username = "";
private String password = "";
private Multipart mp;
public MailSend(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
private void setSmtpHost(String hostName) {
System.out.println("mail.smtp.host = " + hostName);
if (props == null)
props = System.getProperties();
props.put("mail.smtp.host", hostName);
}
private boolean createMimeMessage()
{
try {
session = Session.getDefaultInstance(props, null);
}
catch (Exception e) {
return false;
}
try {
mimeMsg = new MimeMessage(session);
mp = new MimeMultipart();
return true;
}
catch (Exception e) {
return false;
}
}
/**
*
* @param need
* boolean
*
*/
private void setNeedAuth(boolean need) {
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
/**
*
* @param name
* String
*
* @param pass
* String
*
*/
private void setNamePass(String name, String pass) {
username = name;
password = pass;
}
/**
*
* @param mailSubject
* String
*
* @return boolean
*
*/
private boolean setSubject(String mailSubject) {
try {
mimeMsg.setSubject(mailSubject);
return true;
}
catch (Exception e) {
return false;
}
}
/**
*
* @param mailBody
* String
*
*/
private boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent("" + mailBody, "text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
}
catch (Exception e) {
System.err.println("error:" + e);
return false;
}
}
/**
*
* @param name
* String
*
* @param pass
* String
*
*/
private boolean addFileAffix(String filename) {
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
return true;
} catch (MessagingException e) {
return false;
}
}
/**
*
* @param name
* String
*
* @param pass
* String
*
*/
private boolean setFrom(String from) {
try {
mimeMsg.setFrom(new InternetAddress(from));
return true;
} catch (Exception e) {
return false;
}
}
/**
*
* @param name
* String
*
* @param pass
* String
*
*/
private boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
/**
*
* @param name
* String
*
* @param pass
* String
*
*/
private boolean setBcc(String bcc) {
if (bcc == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress
.parse(bcc));
return true;
}
catch (Exception e)
{
return false;
}
}
/**
*
* @param name
* String
*
* @param pass
* String
*
*/
private boolean setCopyTo(String copyto)
{
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
}
catch (Exception e)
{
return false;
}
}
/**
*
* @param name
* String
*
* @param pass
* String
*
*/
private boolean sendout(boolean needAuth)
{
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("start send email....");
_logger.info("start send email....");
if (needAuth){
SmtpAuth sa = new SmtpAuth();
sa.setUserinfo(username, password);
System.out.println("------" + username + "------" + password);
_logger.info("------" + username + "------" + password);
session = Session.getInstance(props, sa);
}
session.setDebug(true);
Transport transport = session.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
System.out.println("sucess to send email");
_logger.info("sucess to send email");
transport.close();
return true;
} catch (Exception e)
{
System.err.println("error" + e);
_logger.info(e.getMessage());
_logger.info(e.getStackTrace().toString());
return false;
}
}
/**
*
* @param name
* String
*
* @param pass
* String
*
*/
private boolean sendout()
{
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("start send email....");
SmtpAuth sa = new SmtpAuth();
sa.setUserinfo(username, password);
System.out.println("------" + username + "------" + password);
Session mailSession = Session.getInstance(props, sa);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
System.out.println("sucess to send email");
transport.close();
return true;
} catch (Exception e)
{
System.err.println("error" + e);
return false;
}
}
class SmtpAuth extends javax.mail.Authenticator {
private String user, password;
public void setUserinfo(String getuser, String getpassword) {
user = getuser;
password = getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user, password);
}
}
public static void main(String[] args) {
String mailbody = "<html>"
+ "<head>"
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />"
+ "<title></title>"
+ "<style type=\"text/css\"></style></head>"
+ "<body>"
+ "<table width=\"769\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
+ "<tr><td width=\"769\" height=\"32\" valign=\"top\"><!--DWLayoutEmptyCell--> </td></tr>"
+ "<tr><td height=\"33\" valign=\"top\"></td></tr>"
+ "<tr><td height=\"34\" valign=\"top\"></td></tr>"
+ "<tr><td height=\"37\" valign=\"top\"></td></tr>"
+ "<tr><td height=\"35\" valign=\"top\"><a href=\"http://192.168.1.122:8080/eyunshu\">�½�</a></td></tr>"
+ "<tr><td height=\"38\" valign=\"top\"></td></tr>"
+ "<tr><td height=\"163\"> </td></tr>"
+ "</table></body></html>";
sendMail("xiaohaibin@miaozhen.com","ceshi自动发邮件",mailbody,"/home/supertool/sql.sql","/home/supertool/sql.sql");
// test1();
}
/**
*
* Just do it as this
*
*/
public static void test1() {
String mailbody = "rrwerewr";
MailSend themail = new MailSend("mail.miaozhen.com");
themail.setNeedAuth(true);
if (themail.setSubject("") == false)
return;
if (themail.setBody(mailbody) == false)
return;
if (themail.setTo("jiangping@supertool.net.cn") == false)
return;
if (themail.setFrom("supertool@miaozhen.com") == false)
return;
themail.setNamePass("supertool@miaozhen.com", "123456");
if (themail.sendout() == false)
return;
}
public static boolean sendMail(String to, String title, String body,String... affixFileNames) {
ResourceBundle resource = null;
resource = ResourceBundle.getBundle("mailserver", Locale.getDefault());
String smtphost = resource.getString("mail.smtp.host");
String from = resource.getString("mail.smtp.from");
String usr = resource.getString("mail.smtp.name");
String password = resource.getString("mail.smtp.password");
boolean needAuth = Boolean.parseBoolean(resource.getString("mail.smtp.auth"));
MailSend themail = new MailSend(smtphost);
themail.setNeedAuth(needAuth);
if (themail.setSubject(title) == false)
return false;
if (themail.setBody(body) == false)
return false;
if (themail.setTo(to) == false)
return false;
boolean hasAffix = affixFileNames!=null&&affixFileNames.length>0?true:false;
if(hasAffix){
for(int i=0; i<affixFileNames.length; i++){
String filename = affixFileNames[i];
themail.addFileAffix(filename);
}
}
//themail.setBcc(bcc);
if (themail.setFrom(from) == false)
return false;
themail.setNamePass(usr, password);
if (themail.sendout(needAuth) == false)
return false;
themail = null;
return true;
}
}