public static void main(String[] args) {
    // Get the Properties and Create a default session
    Properties prop = System.getProperties();
    prop.setProperty("mail.server.com", "127.0.0.1");
    Session session = Session.getDefaultInstance(prop);

    try {
      // Set the mail headers
      MimeMessage msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress("*****@*****.**"));
      msg.addRecipient(Message.RecipientType.TO, new InternetAddress("*****@*****.**"));
      msg.setSubject("First Mail");

      // Create the mime body and attachments
      MimeBodyPart msgBody = new MimeBodyPart();
      msgBody.setContent("Hello World", "text/html");

      MimeBodyPart attFile = new MimeBodyPart();
      attFile.attachFile("RecvMail.java");

      Multipart partMsg = new MimeMultipart();
      partMsg.addBodyPart(msgBody);
      partMsg.addBodyPart(attFile);

      msg.setContent(partMsg);

      Transport.send(msg);

      System.out.println("Message Successfully sent...");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
예제 #2
0
  protected void fillContent(Message email, Execution execution, JCRSessionWrapper session)
      throws MessagingException {
    String text = getTemplate().getText();
    String html = getTemplate().getHtml();
    List<AttachmentTemplate> attachmentTemplates = getTemplate().getAttachmentTemplates();

    try {
      if (html != null || !attachmentTemplates.isEmpty()) {
        // multipart
        MimeMultipart multipart = new MimeMultipart("related");

        BodyPart p = new MimeBodyPart();
        Multipart alternatives = new MimeMultipart("alternative");
        p.setContent(alternatives, "multipart/alternative");
        multipart.addBodyPart(p);

        // html
        if (html != null) {
          BodyPart htmlPart = new MimeBodyPart();
          html = evaluateExpression(execution, html, session);
          htmlPart.setContent(html, "text/html; charset=UTF-8");
          alternatives.addBodyPart(htmlPart);
        }

        // text
        if (text != null) {
          BodyPart textPart = new MimeBodyPart();
          text = evaluateExpression(execution, text, session);
          textPart.setContent(text, "text/plain; charset=UTF-8");
          alternatives.addBodyPart(textPart);
        }

        // attachments
        if (!attachmentTemplates.isEmpty()) {
          addAttachments(execution, multipart);
        }

        email.setContent(multipart);
      } else if (text != null) {
        // unipart
        text = evaluateExpression(execution, text, session);
        email.setText(text);
      }
    } catch (RepositoryException e) {
      logger.error(e.getMessage(), e);
    } catch (ScriptException e) {
      logger.error(e.getMessage(), e);
    }
  }
예제 #3
0
  public boolean sendMessage() {
    Properties properties = new Properties();
    properties.put("mail.smtp.host", mailHost);
    properties.put("mail.from", Source);
    Session session = Session.getInstance(properties, null);
    try {
      Message message = new MimeMessage(session);
      InternetAddress[] addressTO = null;
      if (this.Destination != null && this.Destination.length() != 0) {
        StringTokenizer TOList = getTokenizer(this.Destination);
        addressTO = new InternetAddress[TOList.countTokens()];
        for (int i = 0; i < addressTO.length; i++) {
          addressTO[i] = new InternetAddress(TOList.nextToken());
          message.addRecipient(Message.RecipientType.TO, addressTO[i]);
        }
      }

      if (this.MsgCC != null && this.MsgCC.length() != 0) {
        StringTokenizer CCList = getTokenizer(this.MsgCC);
        InternetAddress[] addressCC = new InternetAddress[CCList.countTokens()];
        for (int i = 0; i < addressCC.length; i++) {
          addressCC[i] = new InternetAddress(CCList.nextToken());
          message.addRecipient(Message.RecipientType.CC, addressCC[i]);
        }
      }
      message.setFrom(new InternetAddress(Source));
      message.setSubject(Subject);
      Content = getHtmlHeader() + Content + getHtmlFooter();
      Content = Content.replaceAll("\\{style\\}", MailStyle);

      BodyPart messageBodyPart = new MimeBodyPart();

      messageBodyPart.setText(Content);
      messageBodyPart.setContent(Content, "text/html");

      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);

      Iterator it = this.BinaryAttachments.iterator();
      while (it.hasNext()) {
        ByteArrayDataSource bads = (ByteArrayDataSource) it.next();

        messageBodyPart = new MimeBodyPart();
        // messageBodyPart.setDataHandler(new DataHandler(new FileDataSource("c:/test/tom.jpg")));
        messageBodyPart.setDataHandler(new DataHandler(bads));
        messageBodyPart.setFileName(bads.getName());

        multipart.addBodyPart(messageBodyPart);
      }
      message.setContent(multipart);

      Transport transport = session.getTransport(addressTO[0]);
      transport.addConnectionListener(new ConnectionHandler());
      transport.addTransportListener(new TransportHandler());
      transport.connect();
      transport.send(message);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
예제 #4
0
  public static boolean sendMail(
      String userName,
      String passWord,
      String host,
      String port,
      String starttls,
      String auth,
      boolean debug,
      String socketFactoryClass,
      String fallback,
      String[] to,
      String[] cc,
      String[] bcc,
      String subject,
      String text,
      String attachmentPath,
      String attachmentName) {

    // Object Instantiation of a properties file.
    Properties props = new Properties();

    props.put("mail.smtp.user", userName);

    props.put("mail.smtp.host", host);

    if (!"".equals(port)) {
      props.put("mail.smtp.port", port);
    }

    if (!"".equals(starttls)) {
      props.put("mail.smtp.starttls.enable", starttls);
      props.put("mail.smtp.auth", auth);
    }

    if (debug) {

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

    } else {

      props.put("mail.smtp.debug", "false");
    }

    if (!"".equals(port)) {
      props.put("mail.smtp.socketFactory.port", port);
    }
    if (!"".equals(socketFactoryClass)) {
      props.put("mail.smtp.socketFactory.class", socketFactoryClass);
    }
    if (!"".equals(fallback)) {
      props.put("mail.smtp.socketFactory.fallback", fallback);
    }

    try {

      Session session = Session.getDefaultInstance(props, null);

      session.setDebug(debug);

      MimeMessage msg = new MimeMessage(session);

      msg.setText(text);

      msg.setSubject(subject);

      Multipart multipart = new MimeMultipart();
      MimeBodyPart messageBodyPart = new MimeBodyPart();
      DataSource source = new FileDataSource(attachmentPath);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(attachmentName);
      multipart.addBodyPart(messageBodyPart);

      msg.setContent(multipart);
      msg.setFrom(new InternetAddress(userName));

      for (int i = 0; i < to.length; i++) {
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
      }

      for (int i = 0; i < cc.length; i++) {
        msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
      }

      for (int i = 0; i < bcc.length; i++) {
        msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
      }

      msg.saveChanges();

      Transport transport = session.getTransport("smtp");

      transport.connect(host, userName, passWord);

      transport.sendMessage(msg, msg.getAllRecipients());

      transport.close();

      return true;

    } catch (Exception mex) {
      mex.printStackTrace();
      return false;
    }
  }