Ejemplo n.º 1
0
 public static void email(
     CallingContext context, String templateName, Map<String, ? extends Object> templateValues) {
   final SMTPConfig config = getSMTPConfig();
   Session session = getSession(config);
   EmailTemplate template = getEmailTemplate(context, templateName);
   try {
     // Instantiate a new MimeMessage and fill it with the required information.
     Message msg = new MimeMessage(session);
     msg.setFrom(new InternetAddress(config.getFrom()));
     String to = renderTemplate(template.getEmailTo(), templateValues);
     if (StringUtils.isBlank(to)) {
       throw RaptureExceptionFactory.create("No emailTo field");
     }
     InternetAddress[] address = {new InternetAddress(to)};
     msg.setRecipients(Message.RecipientType.TO, address);
     msg.setSubject(renderTemplate(template.getSubject(), templateValues));
     msg.setSentDate(new Date());
     msg.setContent(
         renderTemplate(template.getMsgBody(), templateValues), "text/html; charset=utf-8");
     // Hand the message to the default transport service for delivery.
     Transport.send(msg);
   } catch (MessagingException e) {
     log.error("Failed to send email", e);
   }
 }
Ejemplo n.º 2
0
  // Send the specified message.
  public void sendMessage(int type, Message message) throws Exception {
    // Display message dialog to get message values.
    MessageDialog dialog;
    dialog = new MessageDialog(this, null, type, message);
    if (!dialog.display()) {
      // Return if dialog was cancelled.
      return;
    }

    // Create a new message with values from dialog.
    Message newMessage = new MimeMessage(session);
    newMessage.setFrom(new InternetAddress(dialog.getFrom()));

    newMessage.setSubject(dialog.getSubject());
    newMessage.setSentDate(new Date());
    newMessage.setText(dialog.getContent());

    final Address[] recipientAddresses = InternetAddress.parse(dialog.getTo());
    newMessage.setRecipients(Message.RecipientType.TO, recipientAddresses);

    Transport transport = session.getTransport("smtps");
    transport.connect(getSmtpServer(), GMAIL_SMTP_PORT, getUsername(), getPassword());
    transport.sendMessage(newMessage, recipientAddresses);
    transport.close();
  }
Ejemplo n.º 3
0
  // Prefered way of how it should be used.
  public MailAgent(
      String to,
      String cc,
      String bcc,
      String from,
      String subject,
      String content,
      String smtpHost)
      throws FrameworkExecutionException {
    try {
      this.to = to;
      this.cc = cc;
      this.bcc = bcc;
      this.from = from;
      this.subject = subject;
      this.content = content;
      this.smtpHost = smtpHost;

      message = createMessage();
      message.setFrom(new InternetAddress(from));
      setToCcBccRecipients();

      message.setSentDate(new Date());
      message.setSubject(subject);
      message.setText(content);
    } catch (AddressException ex) {
      throw new FrameworkExecutionException(ex);
    } catch (MessagingException ex) {
      throw new FrameworkExecutionException(ex);
    } finally {

    }
  }
Ejemplo n.º 4
0
 protected Message getFullyLoad(Message message, Folder folder, String context)
     throws MessagingException {
   try {
     ((GoogleVoiceFolder) folder).loadFully((GoogleVoiceMessage) message);
   } catch (Exception e) {
     throw new MessagingException("Exception getting googlevoice message body " + e.getMessage());
   }
   Message wrappedMessage = new MimeMessage((MimeMessage) message);
   wrappedMessage.setSentDate(message.getSentDate());
   return wrappedMessage;
 }
  protected void sendNotif(ICNotification notif, Object target) throws Exception {
    // Creating message
    Message s_message = new MimeMessage(session);

    // Set message properties
    s_message.setFrom(new InternetAddress(sender));
    s_message.setSubject(notif.getAttrib(NOTIF_SUBJECT));
    s_message.setText(notif.getAttrib(NOTIF_CONTENT));
    s_message.setSentDate(new Date(notif.getTimeMsec()));

    for (Iterator it = ((Set) target).iterator(); it.hasNext(); ) {
      s_message.addRecipient(Message.RecipientType.TO, new InternetAddress((String) it.next()));
    }
    // Sending message
    transport.sendMessage(s_message, s_message.getAllRecipients());
  }
Ejemplo n.º 6
0
  public static int sendMail(String toAddr, String ccAddr, String mailTitle, String mailConcept) {
    Session s = Session.getInstance(SendMail.props, null);
    s.setDebug(false);

    Message message = new MimeMessage(s);
    try {
      Address from = new InternetAddress(SendMail.SenderEmailAddr);
      message.setFrom(from);
      Address to = new InternetAddress(toAddr);
      message.setRecipient(Message.RecipientType.TO, to);
      if (ccAddr != null && ccAddr != "") {
        Address cc = new InternetAddress(ccAddr);
        message.setRecipient(Message.RecipientType.CC, cc);
      }

      message.setSubject(mailTitle);
      Multipart mainPart = new MimeMultipart();
      BodyPart html = new MimeBodyPart();
      html.setContent(mailConcept, "text/html;charset=ISO-8859-1");
      mainPart.addBodyPart(html);
      message.setContent(mainPart);
      message.setSentDate(new Date());
      message.saveChanges();

      Transport transport = s.getTransport(SendMail.TransprotType);
      transport.connect(SendMail.SMTPServerName, SendMail.SMTPUserName, SendMail.SMTPPassword);
      transport.sendMessage(message, message.getAllRecipients());
      transport.close();

      //			System.out.println("发送邮件,邮件地址:"+toAddr);
      //			System.out.println("发送邮件,邮件地址:"+ccAddr);
      //			System.out.println("标题:"+mailTitle);
      //			System.out.println("内容:"+mailConcept);
      System.out.println("Email Send Success!");
      return 1;
    } catch (Exception e) {
      System.out.println(e.getMessage());
      return -1;
    }
  }
  public void sendEmail(
      String host,
      String port,
      String userName,
      String password,
      String toAddress,
      String subject,
      String message)
      throws MessagingException {
    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");

    // creates a new session with an authenticator
    Authenticator auth =
        new Authenticator() {
          public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
          }
        };

    Session session = Session.getInstance(properties, auth);

    // creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = {new InternetAddress(toAddress)};
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(message);

    // sends the e-mail
    Transport.send(msg);
  }
Ejemplo n.º 8
0
  private void doit(String[] argv) {
    String to = null,
        subject = null,
        from = null,
        replyTo = null,
        cc = null,
        bcc = null,
        url = null;
    String mailhost = null;
    String mailer = "MsgSend";
    String protocol = null, host = null, user = null, password = null, record = null;
    String filename = null, msg_text = null, inline_filename = null;
    boolean debug = false;
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int optind;

    for (optind = 0; optind < argv.length; optind++) {
      if (argv[optind].equals("-T")) {
        protocol = argv[++optind];
      } else if (argv[optind].equals("-X")) {
        msg_text = argv[++optind];
      } else if (argv[optind].equals("-H")) {
        host = argv[++optind];
      } else if (argv[optind].equals("-U")) {
        user = argv[++optind];
      } else if (argv[optind].equals("-P")) {
        password = argv[++optind];
      } else if (argv[optind].equals("-M")) {
        mailhost = argv[++optind];
      } else if (argv[optind].equals("-f")) {
        filename = argv[++optind];
      } else if (argv[optind].equals("-i")) {
        inline_filename = argv[++optind];
      } else if (argv[optind].equals("-s")) {
        subject = argv[++optind];
      } else if (argv[optind].equals("-o")) { // originator (from)
        from = argv[++optind];
      } else if (argv[optind].equals("-r")) { // reply-to
        replyTo = argv[++optind];
      } else if (argv[optind].equals("-c")) {
        cc = argv[++optind];
      } else if (argv[optind].equals("-b")) {
        bcc = argv[++optind];
      } else if (argv[optind].equals("-L")) {
        url = argv[++optind];
      } else if (argv[optind].equals("-d")) {
        debug = true;
      } else if (argv[optind].equals("--")) {
        optind++;
        break;
      } else if (argv[optind].startsWith("-")) {
        System.err.println(USAGE_TEXT);
        System.exit(1);
      } else {
        break;
      }
    }

    try {
      if (optind < argv.length) {
        // XXX - concatenate all remaining arguments
        to = argv[optind];
        System.out.println("To: " + to);
      } else {
        System.out.print("To: ");
        System.out.flush();
        to = in.readLine();
      }
      if (subject == null) {
        System.out.print("Subject: ");
        System.out.flush();
        subject = in.readLine();
      } else {
        System.out.println("Subject: " + subject);
      }

      Properties props = System.getProperties();
      // XXX - could use Session.getTransport() and Transport.connect()
      // XXX - assume we're using SMTP
      if (mailhost != null) props.put("mail.smtp.host", mailhost);

      // Get a Session object
      Session session = Session.getInstance(props, null);
      if (debug) session.setDebug(true);

      // construct the message
      Message msg = new MimeMessage(session);

      if (from != null) msg.setFrom(new InternetAddress(from));
      else msg.setFrom();

      if (reply_to_list == null && replyTo != null) {
        reply_to_list = new InternetAddress[1];
        reply_to_list[0] = new InternetAddress(replyTo);
        msg.setReplyTo(reply_to_list);
      } else msg.setReplyTo(reply_to_list);

      if (dis_list == null) {
        dis_list = new InternetAddress[1];
        dis_list[0] = new InternetAddress(to);
      }

      msg.setRecipients(Message.RecipientType.TO, dis_list);
      if (cc != null) msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
      if (bcc != null)
        msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));

      // in-line file contents if specified
      if (inline_filename != null) {
        msg_text = readFile(inline_filename);
      }

      // create and fill the first message part
      MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setText(msg_text);

      // create the Multipart and add the text part
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(mbp1);

      // create additional message part(s)

      // attach the file or files to the message
      if (filename != null) {
        MimeBodyPart mbp = new MimeBodyPart();
        FileDataSource fds = new FileDataSource(filename);
        mbp.setDataHandler(new DataHandler(fds));
        mbp.setFileName(fds.getName());
        mp.addBodyPart(mbp);
        mbp1.setText(msg_text + "\n\nAttachment: " + filename);
        System.out.println("Added attachment: " + filename);
      }

      if (attachments != null) {
        Iterator i = attachments.iterator();
        StringBuffer list = null;
        while (i.hasNext()) {
          String name = (String) i.next();
          MimeBodyPart mbp = new MimeBodyPart();
          FileDataSource fds = new FileDataSource(name);
          mbp.setDataHandler(new DataHandler(fds));
          mbp.setFileName(fds.getName());
          mp.addBodyPart(mbp);
          if (list == null) {
            list = new StringBuffer(name);
          } else {
            list.append(", " + name);
          }
          System.out.println("Added attachment: " + name);
          mbp1.setText(msg_text + "\nAttachment(s): " + list);
        }
      }

      // add the Multipart to the message
      msg.setContent(mp);

      msg.setSubject(subject);

      // jgfrun collect(in, msg);

      msg.setHeader("X-Mailer", mailer);
      msg.setSentDate(new Date());

      // send the thing off
      Transport.send(msg);

      System.out.println("Mail was sent successfully.");

      // Keep a copy, if requested.
      if (record != null) {

        // Get a Store object
        Store store = null;
        if (url != null) {
          URLName urln = new URLName(url);
          store = session.getStore(urln);
          store.connect();
        } else {
          if (protocol != null) store = session.getStore(protocol);
          else store = session.getStore();

          // Connect
          if (host != null || user != null || password != null) store.connect(host, user, password);
          else store.connect();
        }

        // Get record Folder. Create if it does not exist.
        Folder folder = store.getFolder(record);
        if (folder == null) {
          System.err.println("Can't get record folder.");
          System.exit(1);
        }
        if (!folder.exists()) folder.create(Folder.HOLDS_MESSAGES);

        Message[] msgs = new Message[1];
        msgs[0] = msg;
        folder.appendMessages(msgs);

        System.out.println("Mail was recorded successfully.");
      }

    } catch (Exception e) {
      System.err.println("Could not MsgSend.doit");
      e.printStackTrace();
    }
  } // doit