Java Mail send email

package com.eastpro.batch.biz;

//import javax.activation.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.*;
import javax.mail.*;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * Insert the type's description here. Creation date: (4/13/2001 4:09:33 PM)
 *
 * @author: Administrator
 */
public class JavaMail {
 public java.lang.String host = null;
 protected java.lang.String from;
 protected java.lang.String to;
 protected java.lang.String subject;
 protected String htmlText;
 protected String[] imgsPath = null;
 public javax.mail.Address invalidAddress[] = null;
 public javax.mail.Address validSentAddress[] = null;
 public javax.mail.Address validUnsentAddress[] = null;
 protected java.lang.String replyTo;
 protected java.lang.String cc = null;
 protected java.lang.String bcc;
 protected String contentType = null;
 protected String[] attachFile = null;
 protected String password;
 protected String username;

 public JavaMail() {
 }

 java.lang.String getBcc() {
  return bcc;
 }
 java.lang.String getCc() {
  return cc;
 }
 java.lang.String getReplyTo() {
  return replyTo;
 }
 public String getContentType() {
  return contentType;
 }
 public void setContentType(String contentType) {
  this.contentType = contentType;
 }

 public void setBcc(java.lang.String newBcc) {
  bcc = newBcc;
 }

 public void setCc(java.lang.String newCc) {
  cc = newCc;
 }
 public void setFrom(java.lang.String newFrom) {
  from = newFrom;
 }
 public void setHost(java.lang.String newHost) {
  host = newHost;
 }
 public void setHtmlText(String newHtmlText) {
  htmlText = newHtmlText;
 }
 public void setImgsPath(String[] newImgsPath) {
  imgsPath = newImgsPath;
 }
 public void setReplyTo(java.lang.String newReplyTo) {
  replyTo = newReplyTo;
 }
 public void setSubject(java.lang.String newSubject) {
  subject = newSubject;
 }
 public void setTo(java.lang.String newTo) {
  to = newTo;
 }
 public void setAttachFile(java.lang.String[] newAttachFile) {
  attachFile = newAttachFile;
 }
 protected PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(username, password);
 }

 public void setPassword(String newPassword) {

  this.password = newPassword;
 }

 public void setUsername(String newUsername) {
  this.username = newUsername;
 }
 public void replaceHtml() {
  int j = 0;
  if (imgsPath != null)
   while (j < imgsPath.length) {
    String newPattern = "cid:" + (new Integer(j)).toString()
      + "@eastpro";
    htmlText = stringReplace(htmlText, "<%= _CID %>", newPattern,
      false, true);
    j++;
   }
 }
 public static String stringReplace(String str, String oldPattern,
   String newPattern, boolean replaceAll, boolean ignoreCase) {
  String searchStr = str;
  String patt = oldPattern;

  if (ignoreCase) {
   searchStr = str.toUpperCase();
   patt = oldPattern.toUpperCase();
  }

  String newStr = str;
  String result = new String();
  int offset = 0;
  String tmp = "";

  while (searchStr.length() > 0) {
   offset = searchStr.indexOf(patt);
   if (offset == -1) {
    result = result + newStr;
    break;
   }
   try {
    tmp = newStr.substring(0, offset);
   } catch (Exception e) {
    tmp = "";
   }
   result = result + tmp + newPattern;
   newStr = newStr.substring(offset + oldPattern.length());
   if (!replaceAll) {
    result = result + newStr;
    break;
   }
   searchStr = searchStr.substring(offset + patt.length());
  }

  return result;
 }
 public boolean send() throws NoSuchProviderException, AddressException,
   MessagingException, SendFailedException {
  Properties props = new Properties();
  System.err.println("===from===" + from);
  System.err.println("===to===" + to);
  System.err.println("===cc===" + cc);
  System.err.println("===bcc===" + bcc);
  System.err.println("===replyTo===" + replyTo);
  System.err.println("===subject===" + subject);
  System.err.println("===contentType===" + contentType);
  System.err.println("===htmlText===" + htmlText);
  System.err.println("===username===" + username);
  System.err.println("===password===" + password);
  props.put("mail.smtp.host", host);
  if (username != null && !"".equalsIgnoreCase(username)
    && password != null && !"".equalsIgnoreCase(password)) {
   props.put("mail.smtp.auth", "true");
  } else {
   System.out.println("mail.smtp.auth===========>false");
   props.put("mail.smtp.auth", "false");
  }

  props.put("mail.debug", "true");

  Session session = Session.getDefaultInstance(props, null);
  session.setDebug(false);
  Transport transport = session.getTransport("smtp");
  System.out.println("HtmlMail host = " + host);
  Message msg = new MimeMessage(session);
  /*
   * msg.setFrom(new InternetAddress(from)); InternetAddress[] address =
   * {new InternetAddress(to)};
   * msg.setRecipients(Message.RecipientType.TO, address);
   */
  if (from != null)
   msg.setFrom(new InternetAddress(from));
  else
   msg.setFrom();

  if (replyTo != null && !"".equals(replyTo))
   msg.setReplyTo(InternetAddress.parse(replyTo));
  msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,
    false));
  if (cc != null)
   msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(
     cc, false));
  if (bcc != null)
   msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(
     bcc, false));

  try {
   msg.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B"));
  } catch (Exception e) {
   e.printStackTrace();
  }
  msg.setHeader("X-Mailer", "msgsend");
  MimeMultipart mp = new MimeMultipart();
  mp.setSubType("related");
  MimeBodyPart mbp1 = new MimeBodyPart();
  replaceHtml();
  if (contentType == null)
   mbp1.setContent(htmlText, "text/html");
  else
   mbp1.setContent(htmlText, contentType);
  mp.addBodyPart(mbp1);
  msg.setContent(mp);
  msg.setSentDate(new Date());

  if (imgsPath != null)
   for (int i = 0; i < imgsPath.length; i++) {

    MimeBodyPart mbp2 = new MimeBodyPart();
    FileDataSource fds = new FileDataSource(imgsPath[i]);
    try {
     mbp2.setFileName(MimeUtility.encodeText(fds.getName(),
       "UTF-8", "B"));
    } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
    }
    mbp2.setText("image");
    mbp2.setDataHandler(new DataHandler(fds));
    String cid = (new Integer(i)).toString() + "@eastpro";
    mbp2.setHeader("Content-ID", cid);
    mp.addBodyPart(mbp2);
   }
  if (username != null && !"".equalsIgnoreCase(username)
    && password != null && !"".equalsIgnoreCase(password)) {
   transport.connect(host, username, password);
  } else {
   transport.connect();
  }

  if (replyTo != null && !"".equals(replyTo)) {
   if (to != null && !to.trim().equals("")) {
    transport.sendMessage(msg, msg
      .getRecipients(Message.RecipientType.TO));
   }
  }

  if (cc != null && !cc.trim().equals("")) {
   /**
    * InternetAddress[] old_address = {new InternetAddress(cc)};
    * msg.setRecipients(Message.RecipientType.TO, old_address);
    * transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
    */
   InternetAddress[] old_address = InternetAddress.parse(cc);
   msg.setRecipients(Message.RecipientType.CC, old_address);
   transport.sendMessage(msg, msg
     .getRecipients(Message.RecipientType.CC));
  }
  if (bcc != null && !bcc.trim().equals("")) {
   InternetAddress[] old_address = InternetAddress.parse(bcc);
   msg.setRecipients(Message.RecipientType.BCC, old_address);
   transport.sendMessage(msg, msg
     .getRecipients(Message.RecipientType.BCC));
  }
  return true;
 }
 
 public static void main(String[] args) {
  JavaMail JavaMail=new JavaMail();
  try {
   JavaMail.send();
  } catch (MessagingException e) {
   e.printStackTrace();
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值