發送郵件

package com.sunway.quartz;


import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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 javax.mail.internet.MimeUtility;

/**
 * @author Administrator
 *郵件的基本配置文件
 */
public class MailConfig
{
 public MailConfig()
 {
  Properties pros = new Properties();
  Connection conn =null;
  PreparedStatement pst =null;
  ResultSet rs =null;
  try
  {
   conn =getConnection();
   String querySql ="SELECT smtpserver,smtpport AS smtpport," +
     " mailtype,smtpusername,smtppassword,email" +
     " FROM /"tblbasicconfig/"";
   pst =conn.prepareStatement(querySql);
   rs =pst.executeQuery();
   if(!rs.next())
   {
    return;
   }
   else
   {
    this.host =rs.getString("smtpserver");
    this.port =rs.getInt("smtpport");
    this.userName =rs.getString("smtpusername");
    this.userPw =rs.getString("smtppassword");
    setFrom(this.userName);
   }
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }

 public void sendMail() throws Exception
 {

  Properties props = new Properties();
  props.put("mail.smtp.host", host);
  if (this.userName.length() > 0)
  {
   props.put("mail.smtp.auth", "true");
  }
  Session session = Session.getDefaultInstance(props, null);
  session.setDebug(mailDebug);

  try
  {
   // create a message
   MimeMessage msg = new MimeMessage(session);
   msg.setFrom(new InternetAddress(from));
   InternetAddress[] address = new InternetAddress[tos.length];
   for (int i = 0; i < tos.length; i++)
   {
    address[i] = new InternetAddress(tos[i]);
   }
   msg.setRecipients(Message.RecipientType.TO, address);
   msg.setSubject(subject);
   Multipart mp = new MimeMultipart();

   MimeBodyPart mbp0 = new MimeBodyPart();

   // mbp0.setText(content);
   mbp0.setContent(content, "text/plain; charset=UTF-8");
   mp.addBodyPart(mbp0);
   if (attchedFileNames != null)
   {
    for (int i = 0; i < attchedFileNames.length; i++)
    {
     MimeBodyPart mbp1 = new MimeBodyPart();
     FileDataSource fds = new FileDataSource(attchedFileNames[i]);
     mbp1.setDataHandler(new DataHandler(fds));
     mbp1.setFileName(fds.getName());
     mp.addBodyPart(mbp1);
    }
   }
   msg.setContent(mp);

   msg.setSentDate(new Date());

   Transport transport = session.getTransport("smtp");
   transport.connect(host, port, userName, userPw);
   transport.sendMessage(msg, msg.getAllRecipients());
   transport.close();
   // System.out.println("Mail " + this.subject + " has been sent");
  } catch (MessagingException mex)
  {
   mex.printStackTrace();
   Exception ex = null;
   if ((ex = mex.getNextException()) != null)
   {
    ex.printStackTrace();
   }
  }
 }

 public String[] getAttchedFileNames()
 {
  return attchedFileNames;
 }

 public void setAttchedFileNames(String[] attchedFileNames)
 {
  this.attchedFileNames = attchedFileNames;
 }

 public String getContent()
 {
  return content;
 }

 public void setContent(String content)
 {
  this.content = content;
 }

 public String getFrom()
 {
  return from;
 }

 public void setFrom(String from)
 {
  this.from = from;
 }

 public String getSubject()
 {
  return subject;
 }

 public void setSubject(String subject)
 {
  try
  {
   this.subject = MimeUtility.encodeText(subject, "GB2312", "B");
  } catch (UnsupportedEncodingException e)
  {

   this.subject = subject;
  }
 }

 public String[] getTos()
 {
  return tos;
 }

 public void setTos(String[] tos)
 {
  this.tos = tos;
 }

 private Connection getConnection()
 {
  try
  {
   Class.forName(conf.getProperty("driver"));
   Connection conn = DriverManager.getConnection(conf.getProperty("uri"), conf.getProperty("user"), conf.getProperty("password"));
   System.out.println(conf.getProperty("uri"));
   return conn;
  } catch (Exception ex)
  {
   ex.printStackTrace();
  }
  return null;
 }
 
 public static void main(String[] args)
 {
  MailConfig sender = new MailConfig();
  sender.setSubject("TEST_中文_88");
  sender.setContent("Test content 還有中文 chinese!");
//  sender.setFrom("[email protected]");
  sender.setTos(new String[] { "[email protected]" });
  sender.setAttchedFileNames(new String[] { "E://swing//tmp//BackUp.txt" });

  try
  {
   sender.sendMail();
  } catch (Exception e)
  {
   e.printStackTrace();
  }
 }
 
 private static Config conf = Config.getInstance();
 
 private String host = "localhost";

 private int port = 25;

 private String userName = "";

 private String userPw = "";

 private boolean mailDebug = true;

 private String[] tos;

 private String from;

 private String content;

 private String subject;

 private String[] attchedFileNames;
}
 

發佈了37 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章