Java Mail 深入浅出(2)

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  /**

  * SendMailService 构造子注解。

  */

  public SendMail() {

  super();

  }

  private void fillMail(Session session,MimeMessage msg) throws IOException, MessagingException{

  String fileName = null;

  Multipart mPart = new MimeMultipart();

  if (mailFrom != null) {

  msg.setFrom(new InternetAddress(mailFrom));

  System.out.println(“发送人Mail地址:”+mailFrom);

  } else {

  System.out.println(“没有指定发送人邮件地址!”);

  return;

  }

  if (mailTo != null) {

  InternetAddress[] address = InternetAddress.parse(mailTo);

  msg.setRecipients(Message.RecipientType.TO, address);

  System.out.println(“收件人Mail地址:”+mailTo);

  } else {

  System.out.println(“没有指定收件人邮件地址!”);

  return;

  }

  if (mailccTo != null) {

  InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);

  System.out.println(“CCMail地址:”+mailccTo);

  msg.setRecipients(Message.RecipientType.CC, ccaddress);

  }

  if (mailbccTo != null) {

  InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);

  System.out.println(“BCCMail地址:”+mailbccTo);

  msg.setRecipients(Message.RecipientType.BCC, bccaddress);

  }

  msg.setSubject(subject);

  InternetAddress[] replyAddress = { new InternetAddress(mailFrom)};

  msg.setReplyTo(replyAddress);

  // create and fill the first message part

  MimeBodyPart mBodyContent = new MimeBodyPart();

  if (msgContent != null)

  mBodyContent.setContent(msgContent, messageContentMimeType);

  else

  mBodyContent.setContent(“”, messageContentMimeType);

  mPart.addBodyPart(mBodyContent);

  // attach the file to the message

  if (attachedFileList != null) {

  for (Enumeration fileList = attachedFileList.elements(); fileList.hasMoreElements();) {

  fileName = (String) fileList.nextElement();

  MimeBodyPart mBodyPart = new MimeBodyPart();

  // attach the file to the message

  FileDataSource fds = new FileDataSource(messageBasePath + fileName);

  System.out.println(“Mail发送的附件为:”+messageBasePath + fileName);

  mBodyPart.setDataHandler(new DataHandler(fds));

  mBodyPart.setFileName(fileName);

  mPart.addBodyPart(mBodyPart);

  }

  }

  msg.setContent(mPart);

  msg.setSentDate(new Date());

  }

[1][2][3]

人情似纸张张薄,世事如棋局局新。

Java Mail 深入浅出(2)

相关文章:

你感兴趣的文章:

标签云: