JAVA發送郵件的程序

activation-1.1.jar

 

mail-1.4.jar

 

 

import java.util.Properties; //import common.util.Email_Autherticatorbean;
import javax.mail.Authenticator;
import javax.mail.internet.InternetAddress; //import org.apache.commons.lang.StringUtils;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.activation.FileDataSource;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataHandler;
import javax.mail.internet.MimeUtility;
import java.util.Date;

/**
 * 利用java.mail的郵件發送程序
 */

public class SendMailTest {
 public static void main(String[] args) {
  String title = "titleTest";// 所發送郵件的標題
  String from = "[email protected]";// 從那裏發送
  String sendTo[] = { "[email protected]", "[email protected]" };// 發送到那裏
  // 郵件的文本內容,可以包含html標記則顯示爲html頁面
  String content = "mail test!!!!!!<br><a href=#>aaa</a>";
  // 所包含的附件,及附件的重新命名
  String fileNames[] = { "D:/webapp.log,webapp.log",
    "D:/webdevelopment.log,webdevelopment.log" };
  try {
   // MailSender mailsender = new MailSender();
   sendmail(title, from, sendTo, content, fileNames,
     "text/html;charset=gb2312");
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 public static void sendmail(String subject, String from, String[] to,
   String text, String[] filenames, String mimeType) throws Exception {
  // ResourceBundle mailProps = ResourceBundle.getBundle("mail");
  // 可以從配置文件讀取相應的參數
  Properties props = new Properties();

  String smtp = "smtp.163.com"; // 設置發送郵件所用到的smtp
  String servername = "abc";
  String serverpaswd = "abc";

  javax.mail.Session mailSession; // 郵件會話對象
  javax.mail.internet.MimeMessage mimeMsg; // MIME郵件對象

  props = java.lang.System.getProperties(); // 獲得系統屬性對象
  props.put("mail.smtp.host", smtp); // 設置SMTP主機
  props.put("mail.smtp.auth", "false"); // 是否到服務器用戶名和密碼驗證
  // 到服務器驗證發送的用戶名和密碼是否正確
  Email_Autherticatorbean myEmailAuther = new Email_Autherticatorbean(
    servername, serverpaswd);
  // 設置郵件會話
  mailSession = javax.mail.Session.getInstance(props,
    (Authenticator) myEmailAuther);
  // 設置傳輸協議
  javax.mail.Transport transport = mailSession.getTransport("smtp");
  // 設置from、to等信息
  mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
  if (from.length() > 0) { //must modify
   InternetAddress sentFrom = new InternetAddress(from);
   mimeMsg.setFrom(sentFrom); // 設置發送人地址
  }

  InternetAddress[] sendTo = new InternetAddress[to.length];
  for (int i = 0; i < to.length; i++) {
   System.out.println("發送到:" + to[i]);
   sendTo[i] = new InternetAddress(to[i]);
  }

  mimeMsg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO,
    sendTo);
  mimeMsg.setSubject(subject, "gb2312");

  MimeBodyPart messageBodyPart1 = new MimeBodyPart();
  // messageBodyPart.setText(UnicodeToChinese(text));
  messageBodyPart1.setContent(text, mimeType);

  Multipart multipart = new MimeMultipart();// 附件傳輸格式
  multipart.addBodyPart(messageBodyPart1);

  for (int i = 0; i < filenames.length; i++) {
   MimeBodyPart messageBodyPart2 = new MimeBodyPart();
   // 選擇出每一個附件名
   String filename = filenames[i].split(",")[0];
   System.out.println("附件名:" + filename);
   String displayname = filenames[i].split(",")[1];
   // 得到數據源
   FileDataSource fds = new FileDataSource(filename);
   // 得到附件本身並至入BodyPart
   messageBodyPart2.setDataHandler(new DataHandler(fds));
   // 得到文件名同樣至入BodyPart
   // messageBodyPart2.setFileName(displayname);
   // messageBodyPart2.setFileName(fds.getName());
   messageBodyPart2.setFileName(MimeUtility.encodeText(displayname));
   multipart.addBodyPart(messageBodyPart2);
  }
  mimeMsg.setContent(multipart);
  // 設置信件頭的發送日期
  mimeMsg.setSentDate(new Date());
  mimeMsg.saveChanges();
  // 發送郵件
  transport.send(mimeMsg);
  transport.close();
 }

}

// 詳細出處參考:http://www.jb51.net/article/15323.htm

 


import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class Email_Autherticatorbean extends Authenticator {
 private String m_username = null;
 private String m_userpass = null;

 public void setUsername(String username) {
  m_username = username;
 }

 public void setUserpass(String userpass) {
  m_userpass = userpass;
 }

 public Email_Autherticatorbean(String username, String userpass) {
  super();
  setUsername(username);
  setUserpass(userpass);

 }

 public PasswordAuthentication getPasswordAuthentication() {

  return new PasswordAuthentication(m_username, m_userpass);
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章