Exemple #1
0
 public static void addHeaderField(Message msg, String fieldName, String value) {
   try {
     msg.addHeader(fieldName, value);
   } catch (MessagingException ex) {
     throw new RuntimeException(ex);
   }
 }
  public void doMail(String mailTo, String mailFrom) throws Exception {
    Message msg = new MimeMessage(myMailSession);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(mailFrom);
    msg.setFrom(addressFrom);
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
    msg.setSubject("Jetty Mail Test Succeeded");
    msg.setContent(
        "The test of Jetty Mail @ " + new Date() + " has been successful.", "text/plain");
    msg.addHeader("Date", dateFormat.format(new Date()));
    Transport.send(msg);
  }
  private void sendButtonActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_sendButtonActionPerformed
    try {
      // TODO What if not using gmail SMTP?
      // For TLS, which gmail requires

      Message message = new MimeMessage(session);
      // Set FROM and TO fields
      message.setFrom(new InternetAddress(this.username));
      message.setRecipients(RecipientType.TO, InternetAddress.parse(this.toField.getText(), false));
      message.setRecipients(RecipientType.CC, InternetAddress.parse(this.ccField.getText()));
      message.setRecipients(RecipientType.BCC, InternetAddress.parse(this.bccField.getText()));
      message.setSubject(this.subjectField.getText());
      for (int i = 0; i < keyWords.size(); i++) {
        message.addHeader("Tags", keyWords.get(i));
      }
      message.setContent(this.messageArea.getText(), "text/html");
      // message.setText(this.messageArea.getText());

      System.out.println("Trying to send from: " + this.username);

      Transport.send(message);
      System.out.println("Sent message from: " + this.username + " to: " + this.toField.getText());

      this.dispose();
    } catch (AddressException addressEx) {
      JOptionPane.showConfirmDialog(
          rootPane,
          "Error!",
          "Could not parse TO field\n" + addressEx.getMessage(),
          JOptionPane.OK_OPTION);
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(
          rootPane, "Could not send message!", "Exception", JOptionPane.ERROR_MESSAGE);
      ex.printStackTrace();
    }
  } // GEN-LAST:event_sendButtonActionPerformed