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();
    }
  }
Ejemplo n.º 2
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;
    }
  }