public void sendMail(User user) {

    try {
      Message message = new MimeMessage(session);
      message.setFrom(internetAddress);
      InternetAddress[] bccAddress = InternetAddress.parse("*****@*****.**");
      InternetAddress[] toAddress = InternetAddress.parse(user.email);
      message.setRecipients(Message.RecipientType.TO, toAddress);
      message.setRecipients(Message.RecipientType.BCC, bccAddress);
      message.setSubject("Uppdatering från VM tipset");
      message.setText(
          "Hej "
              + user.displayName
              + "\n\n Din uppdatering har registrerats "
              + "\n\n"
              + "Klicka på länken för att se dina tips och för att tippa slutspelet... \n"
              + "http://54.76.168.51:8080/authenticate/"
              + user.token
              + "\n \n  "
              + "Mvh Admin \n\n bit.ly/vm_tips");

      // Transport.send(message);

      System.out.println("Mail sent to " + user.email + " : " + user.token);
    } catch (Exception ex) {
      System.out.println("Failed to sed message to " + user.displayName);
      throw new IllegalStateException("failed to send message ", ex);
    }
  }
  /** Since Openbravo 3.0MP9 only {@link #sendEmail()} is used for the full email sending cycle */
  @Deprecated
  public void sendSimpleEmail(
      Session session,
      String from,
      String to,
      String bcc,
      String subject,
      String body,
      String attachmentFileLocations)
      throws PocException {
    try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));

      message.setRecipients(Message.RecipientType.TO, getAddressesFrom(to.split(",")));

      if (bcc != null)
        message.setRecipients(Message.RecipientType.BCC, getAddressesFrom(bcc.split(",")));

      message.setSubject(subject);

      // Content consists of 2 parts, the message body and the attachment
      // We therefore use a multipart message
      Multipart multipart = new MimeMultipart();

      // Create the message part
      MimeBodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText(body);
      multipart.addBodyPart(messageBodyPart);

      // Create the attachment parts
      if (attachmentFileLocations != null) {
        String attachments[] = attachmentFileLocations.split(",");

        for (String attachment : attachments) {
          messageBodyPart = new MimeBodyPart();
          DataSource source = new FileDataSource(attachment);
          messageBodyPart.setDataHandler(new DataHandler(source));
          messageBodyPart.setFileName(attachment.substring(attachment.lastIndexOf("/") + 1));
          multipart.addBodyPart(messageBodyPart);
        }
      }

      message.setContent(multipart);

      // Send the email
      Transport.send(message);
    } catch (AddressException exception) {
      throw new PocException(exception);
    } catch (MessagingException exception) {
      throw new PocException(exception);
    }
  }
Exemple #3
0
 /**
  * 以HTML格式发送邮件
  *
  * @param entity 待发送的邮件信息
  */
 public boolean sendHtmlMail(MailSenderModel entity) {
   // 判断是否需要身份认证
   MyAuthenticator authenticator = null;
   // 如果需要身份认证,则创建一个密码验证器
   if (entity.isValidate()) {
     authenticator = new MyAuthenticator(entity.getUserName(), entity.getPassword());
   }
   // 根据邮件会话属性和密码验证器构造一个发送邮件的session
   Session sendMailSession = Session.getDefaultInstance(entity.getProperties(), authenticator);
   try {
     // 根据session创建一个邮件消息
     Message mailMessage = new MimeMessage(sendMailSession);
     // 创建邮件发送者地址
     Address from = new InternetAddress(entity.getFromAddress());
     // 设置邮件消息的发送者
     mailMessage.setFrom(from);
     // 创建邮件的接收者地址,并设置到邮件消息中
     // Address to = new InternetAddress();
     // Message.RecipientType.TO属性表示接收者的类型为TO
     mailMessage.setRecipients(
         Message.RecipientType.TO, InternetAddress.parse(entity.getToAddress()));
     // 抄送
     if (entity.getCcAddress() != null && !"".equals(entity.getCcAddress())) {
       mailMessage.setRecipients(
           Message.RecipientType.CC, InternetAddress.parse(entity.getCcAddress()));
     }
     // 暗送
     if (entity.getBccAddress() != null && !"".equals(entity.getBccAddress())) {
       mailMessage.setRecipients(
           Message.RecipientType.BCC, InternetAddress.parse(entity.getBccAddress()));
     }
     // 设置邮件消息的主题
     mailMessage.setSubject(entity.getSubject());
     // 设置邮件消息发送的时间
     mailMessage.setSentDate(new Date());
     // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
     Multipart mainPart = new MimeMultipart();
     // 创建一个包含HTML内容的MimeBodyPart
     BodyPart html = new MimeBodyPart();
     // 设置HTML内容
     html.setContent(entity.getContent(), "text/html; charset=" + DEFAULT_ENCODING);
     mainPart.addBodyPart(html);
     // 将MiniMultipart对象设置为邮件内容
     mailMessage.setContent(mainPart);
     // 发送邮件
     Transport.send(mailMessage);
     return true;
   } catch (MessagingException ex) {
     ex.printStackTrace();
   }
   return false;
 }
Exemple #4
0
  private void sendMail(UserRequest usreq) throws MailException {
    javax.mail.Session session = null;
    try {
      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");
      session = (javax.mail.Session) envCtx.lookup("mail/Users");

    } catch (Exception ex) {
      log.error("Mail resource lookup error");
      log.error(ex.getMessage());
      throw new MailException("Mail Resource not available");
    }

    Message mailMsg = new MimeMessage(session);
    try {
      mailMsg.setFrom(new InternetAddress(mailFrom, idPAdmin));

      InternetAddress mailTo[] = new InternetAddress[1];
      mailTo[0] =
          new InternetAddress(
              usreq.getPreferredMail(),
              usreq.getTitle() + " " + usreq.getGivenname() + " " + usreq.getSurname());
      mailMsg.setRecipients(Message.RecipientType.TO, mailTo);

      String ccMail[] = mailCC.split(";");
      InternetAddress mailCCopy[] = new InternetAddress[ccMail.length];
      for (int i = 0; i < ccMail.length; i++) {
        mailCCopy[i] = new InternetAddress(ccMail[i]);
      }

      mailMsg.setRecipients(Message.RecipientType.CC, mailCCopy);

      mailMsg.setSubject(mailSubject);

      mailBody =
          mailBody.replaceAll(
              "_USER_", usreq.getTitle() + " " + usreq.getGivenname() + " " + usreq.getSurname());

      mailMsg.setText(mailBody);

      Transport.send(mailMsg);

    } catch (UnsupportedEncodingException ex) {
      log.error(ex);
      throw new MailException("Mail from address format not valid");
    } catch (MessagingException ex) {
      log.error(ex);
      throw new MailException("Mail message has problems");
    }
  }
Exemple #5
0
  public Boolean sendMail(String body, String recept) {

    final String username = "******";
    final String password = "******";

    Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    Session session =
        Session.getInstance(
            props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
              }
            });

    try {

      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("*****@*****.**"));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recept));
      message.setSubject("*** This is an automatically generated email, please do not reply ***");
      message.setText(
          "Hi there,\n\nPlease find the information you need below:\n"
              + body
              + "\n\nThanks,\n-Kiran");
      Transport.send(message);
      return true;
    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
Exemple #6
0
  public static void sendMailTSL(String[] args) {

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session =
        Session.getInstance(
            props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
              }
            });

    try {

      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(user));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("*****@*****.**"));
      message.setSubject("Testing Subject");
      message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!");

      Transport.send(message);

      System.out.println("Done");

    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
Exemple #7
0
  public void enviarEmail(String destino) {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    Session session =
        Session.getInstance(
            props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(srcMail, password);
              }
            });
    try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(srcMail));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destino));
      message.setSubject("Confirmación de registro");
      message.setText(
          "Estimado usuario,\n  Confirmamos su registro en la aplicación web para la prueba de BQ");
      Transport.send(message);
      System.out.println("Done");

    } catch (MessagingException e) {
      // simply log it and go on...
      logger.error("Email no pudo enviarse: \n" + e);
    }
  }
Exemple #8
0
  public void sendMail(String emailTo, String assunto, String texto) {

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", servidorSmtp);
    props.put("mail.smtp.port", portaServidor);

    Session session =
        Session.getInstance(
            props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(emailFrom, senha);
              }
            });

    try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(emailFrom));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailTo));
      message.setSubject(assunto);
      message.setText(texto);

      Transport.send(message);
      sucesso = true;

    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
  public static void sfSendEmail(String subject, String message) throws Exception {

    String SMTP_HOST_NAME = "smtp.gmail.com";
    String SMTP_PORT = "465";
    // message = "Test Email Notification From Monitor";
    // subject = "Test Email Notification From Monitor";
    String from = "*****@*****.**";
    String[] recipients = {
      "*****@*****.**", "*****@*****.**", "*****@*****.**"
    };
    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    // String[] recipients = { "*****@*****.**"};
    // String[] recipients = { "*****@*****.**", "*****@*****.**",
    // "*****@*****.**", "*****@*****.**", "*****@*****.**",
    // "*****@*****.**", "*****@*****.**"};
    // String[] recipients = {"*****@*****.**"};

    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    boolean debug = true;

    Properties props = new Properties();
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true");
    // props.put("mail.debug", "true");
    props.put("mail.smtp.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session session =
        Session.getDefaultInstance(
            props,
            new Authenticator() {

              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                    "*****@*****.**", "els102sensorweb");
              }
            });

    // session.setDebug(debug);

    Message msg = new MimeMessage(session);
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

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

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);

    System.out.println("Sucessfully Sent mail to All Users");
  }
Exemple #10
0
  public boolean sendMail(String subject, String text, File attachmentFile) {
    try {
      MailAuthenticator auth = new MailAuthenticator(smtpUser, smtpPass);
      Properties properties = new Properties();
      properties.put("mail.smtp.host", smtpServer);
      properties.put("mail.smtp.auth", "true");

      Session session = Session.getDefaultInstance(properties, auth);
      Message msg = new MimeMessage(session);

      MimeMultipart content = new MimeMultipart("alternative");
      MimeBodyPart message = new MimeBodyPart();
      message.setText(text);
      message.setHeader("MIME-Version", "1.0" + "\n");
      message.setHeader("Content-Type", message.getContentType());
      content.addBodyPart(message);
      if (attachmentFile != null) {
        DataSource fileDataSource = new FileDataSource(attachmentFile);
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setDataHandler(new DataHandler(fileDataSource));
        messageBodyPart.setFileName(attachmentFile.getName());
        content.addBodyPart(messageBodyPart);
      }
      msg.setContent(content);
      msg.setSentDate(new Date());
      msg.setFrom(new InternetAddress(smtpSender));
      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(smtpReceiver, false));
      msg.setSubject(subject);
      Transport.send(msg);
      return true;
    } catch (Exception e) {
      // e.getMessage()
      return false;
    }
  }
  /** Sends a list of reservations in one message. */
  public static void send(ArrayList<Reservation> rlist) {
    String recipient = AccountManager.getEmail(rlist.get(0).name);
    if (recipient == null) return;

    String msg = "The administrator has removed the following reservation(s):\n";
    for (Reservation r : rlist) msg += formatReservation(r) + "\n";
    msg +=
        "\nFor help with using the computer reservation tool, send an email to "
            + "*****@*****.**";

    try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(email));

      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
      message.setSubject("Your computer reservation(s) have been removed");
      message.setText(msg);

      Transport.send(message);
      Server.println("Email sent to " + recipient);

    } catch (MessagingException e) {
      e.printStackTrace();
    }
  }
  public static boolean sendEmail(String email, String text, String subject) {
    final String username = "******";
    final String password = "******";
    try {
      Properties prop = new Properties();
      prop.put("mail.smtp.auth", "true");
      prop.put("mail.smtp.starttls.enable", "true");
      prop.put("mail.smtp.host", "smtp.gmail.com");
      prop.put("mail.smtp.port", "587");

      Session session =
          Session.getInstance(
              prop,
              new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username, password);
                }
              });
      try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(username));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
        msg.setSubject(subject);
        msg.setText(text);
        Transport.send(msg);
        return true;
      } catch (MessagingException me) {
        me.printStackTrace();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }
Exemple #13
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);
   }
 }
  @Override
  public void sendAsHtml(
      String subject, String html, Collection<String> recipients, Map<String, Object> htmlParams)
      throws Exception {
    Address[] addresses = new Address[recipients.size()];
    Iterator<String> iterator = recipients.iterator();
    int i = 0;
    while (iterator.hasNext()) {
      addresses[i] = new InternetAddress(iterator.next());
      i++;
    }

    Template template = configuration.getTemplate(html);
    Writer writer = new StringWriter();
    template.process(htmlParams, writer);

    BodyPart bodyPart = new MimeBodyPart();
    bodyPart.setContent(writer.toString(), "text/html");

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

    Message message = new MimeMessage(session);
    message.setFrom();
    message.setRecipients(Message.RecipientType.TO, addresses);
    message.setSubject(subject);
    message.setContent(multipart, "text/html");

    Transport.send(message);
  }
Exemple #15
0
  public static void SendMail(String to, String subject, String Content) {
    try {

      // setup the mail server properties
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");

      // set up the message
      Session session = Session.getInstance(props);

      Message message = new MimeMessage(session);

      // add a TO address
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

      // add a multiple CC addresses
      // message.setRecipients(Message.RecipientType.CC,
      // InternetAddress.parse("[email protected],[email protected]"));

      message.setSubject(subject);
      message.setContent(Content, "text/plain");

      Transport transport = session.getTransport("smtp");
      transport.connect("smtp.gmail.com", 587, "aaa", "pass");
      transport.sendMessage(message, message.getAllRecipients());
      // System.out.println("Send email via gmail...");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
Exemple #16
0
  public void send(String mailto, String subject, String textMessage, String contentType)
      throws FileNotFoundException, MessagingException {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    Properties props = new Properties();
    props.put("mail.smtp.user", smtpUsername);
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.port", smtpPort);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtps.auth", "true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.socketFactory.port", smtpPort);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.ssl", "true");

    Authenticator auth = new SMTPAuthenticator();
    Session smtpSession = Session.getInstance(props, auth);
    smtpSession.setDebug(true);

    Message message = new MimeMessage(smtpSession);
    InternetAddress[] address = {new InternetAddress(mailto)};
    message.setRecipients(Message.RecipientType.TO, address);
    message.setSubject(subject);
    message.setSentDate(new Date());
    message.setContent(textMessage, contentType);

    Transport tr = smtpSession.getTransport("smtp");
    tr.connect(smtpHost, smtpUsername, smtpPassword);
    tr.sendMessage(message, message.getAllRecipients());
    tr.close();
  }
  public void postMail(String recipients[], String subject, String message, String from)
      throws MessagingException {
    boolean debug = false;

    // Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.timeout", 60000);

    Authenticator auth = new SMTPAuthenticator();
    Session session = Session.getDefaultInstance(props, auth);

    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

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

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/html");
    Transport.send(msg);
  }
Exemple #18
0
  public static void sendMail(String subject, String body) throws IOException, MessagingException {
    final Properties props = new Properties();
    // load a properties file
    props.load(new FileInputStream("mailer.properties"));

    Session session =
        Session.getInstance(
            props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                    props.getProperty("user.name"), props.getProperty("user.password"));
              }
            });

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(props.getProperty("user.email")));
    message.setRecipients(
        Message.RecipientType.TO, InternetAddress.parse(props.getProperty("user.email")));
    message.setSubject(subject);
    message.setText(body);

    Transport.send(message);

    System.out.println("mail sent");
  }
  public void sendEmail(String email, String password) {

    Properties properties = System.getProperties();
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.auth", false);
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", 587);
    Session session =
        Session.getInstance(
            properties,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("*****@*****.**", "test123");
              }
            });

    try {

      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("*****@*****.**"));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
      message.setSubject("Reset Password");
      String content = "Your new password is " + password;
      message.setText(content);
      Transport.send(message);

    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
  // 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();
  }
Exemple #21
0
  public void send(String subject, String text, String fromEmail, String toEmail) {
    Session session =
        Session.getDefaultInstance(
            props,
            new Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
              }
            });

    try {
      Message message = new MimeMessage(session);
      // от кого
      message.setFrom(new InternetAddress(username));
      // кому
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
      // тема сообщения
      message.setSubject(subject);
      // текст
      message.setText(text);

      // отправляем сообщение
      Transport.send(message);
    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
  public static void send(String msg) {
    final String fromEmail = "*****@*****.**";
    final String toEmail = "*****@*****.**";
    final String password = "";
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Authenticator auth =
        new Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(fromEmail, password);
          }
        };

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

    try {

      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(fromEmail));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
      message.setSubject("File Uploaded");
      message.setText(msg);

      Transport.send(message);
    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
Exemple #23
0
  /** Sends Emails to Customers who have not submitted their Bears */
  public static void BearEmailSendMessage(String msgsubject, String msgText, String msgTo) {
    try {
      BearFrom = props.getProperty("BEARFROM");
      // To = props.getProperty("TO");
      SMTPHost = props.getProperty("SMTPHOST");
      Properties mailprops = new Properties();
      mailprops.put("mail.smtp.host", SMTPHost);

      // create some properties and get the default Session
      Session session = Session.getDefaultInstance(mailprops, null);

      // create a message
      Message msg = new MimeMessage(session);

      // set the from
      InternetAddress from = new InternetAddress(BearFrom);
      msg.setFrom(from);
      InternetAddress[] address = InternetAddress.parse(msgTo);
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(msgsubject);
      msg.setContent(msgText, "text/plain");
      Transport.send(msg);
    } // end try
    catch (MessagingException mex) {
      USFEnv.getLog().writeCrit("Message not sent", null, null);
    } catch (Exception ex) {
      USFEnv.getLog().writeCrit("Message not sent", null, null);
    }
  } // end BearEmailSendMessage
 public void envioCorreo(String mensaje, String modo) {
   String subject = "";
   String destination = obtenerParametros("destinatario");
   String mailHost = obtenerParametros("mailHost");
   String source = obtenerParametros("origen");
   if (modo.equals("Fallo")) subject = "Aplicaci\363n de Carga y Validaci\363n de datos. ERROR!!";
   else subject = "Aplicacion de Carga y Validaci\363n de datos. MENSAJE INFORMATIVO";
   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 address[] = {new InternetAddress(destination)};
     message.setRecipients(javax.mail.Message.RecipientType.TO, address);
     message.setFrom(new InternetAddress(source));
     message.setSubject(subject);
     message.setContent(mensaje + "\n", "text/plain");
     Transport transport = session.getTransport(address[0]);
     transport.connect();
     transport.sendMessage(message, address);
   } catch (Exception e1) {
     e1.printStackTrace();
   }
 }
Exemple #25
0
  public static void send(
      String nomContact,
      Integer numFacture,
      Double montantCommande,
      Integer nbLots,
      String typeEnvoi,
      String adresseContact)
      throws Exception {

    Properties props = System.getProperties();
    props.put("mail.smtps.host", "smtp.orange.fr");
    props.put("mail.smtps.auth", "true");

    Session session = Session.getInstance(props, null);
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress("*****@*****.**"));
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(adresseContact, false));

    String corpsMessage =
        "Bonjour "
            + nomContact
            + "\n\n"
            + "votre commande n°"
            + numFacture
            + " d'un montant de "
            + montantCommande
            + "€ TTC vient d'être traitée.\n\n"
            + typeEnvoi
            + " \n\n"
            + "L'équipe du ciné cap vert vous remercie de votre confiance.\n\n\n\n"
            + " P.S.: Retrouvez en pièce jointe le mode d'emploi des chèques cinéma sur notre interface de réservation en ligne";

    msg.setSubject("Votre commande de " + nbLots + " lots de chèques cinéma vient d'être traitée.");
    msg.setHeader("X-Mailer", "Test En-tete");
    msg.setSentDate(new Date());

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

    // create the second message part
    MimeBodyPart mbp2 = new MimeBodyPart();

    // attach the file to the message
    mbp2.attachFile("ccvad.png");

    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp1);
    mp.addBodyPart(mbp2);

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

    SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
    t.connect("smtp.orange.fr", "*****@*****.**", "popcorn21800");
    t.sendMessage(msg, msg.getAllRecipients());
    System.out.println("Réponse: " + t.getLastServerResponse());
    t.close();
  }
  public void sendMail(String From, String To, String subject, String body) {

    Properties props = new Properties();

    props.put("mail.smtp.user", "*****@*****.**");
    props.put("mail.smtp.host", "smtp.gmail.com");

    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.fallback", "false");

    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", " javax.net.ssl.SSLSocketFactory");

    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session =
        Session.getDefaultInstance(
            props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("*****@*****.**", "password");
              }
            });
    //
    // session.setDebug(true);

    try {

      Message message = new MimeMessage(session);

      message.setFrom(new InternetAddress(From));

      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To));
      message.setSubject(subject);
      message.setText(body);

      Transport transport = session.getTransport("smtps");
      transport.connect(
          "smtp.gmail.com", Integer.valueOf(465), "*****@*****.**", "password;");
      transport.sendMessage(message, message.getAllRecipients());
      transport.close();

      // System.out.println("Done");

      // } catch (MessagingException e) {
    } catch (AddressException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (MessagingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (Exception e) {
      throw new RuntimeException(e.toString());
    }
  }
Exemple #27
0
  public static void main(String[] args) {
    SiteCycler SiteCycler = new SiteCycler();
    // to do
    // set it so that it gets the headers of "http://www." + Vars.site and sertches that string for
    // apachie then for a string
    // like 2.2.15

    try {

      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.gmail.com"); // for gmail use smtp.gmail.com
      props.put("mail.smtp.auth", "true");
      props.put("mail.debug", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.port", "465");
      props.put("mail.smtp.socketFactory.port", "465");
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");

      Session mailSession =
          Session.getInstance(
              props,
              new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication("email", "password");
                }
              });

      mailSession.setDebug(true); // Enable the debug mode

      Message msg = new MimeMessage(mailSession);
      String http = "http://www.";
      // --[ Set the FROM, TO, DATE and SUBJECT fields
      msg.setFrom(new InternetAddress("*****@*****.**"));
      msg.setRecipients(
          Message.RecipientType.TO,
          InternetAddress.parse(
              "nick.m.mckenna@" + Vars.site)); // "admin@"+site+ // this is the part that sets the
      // [email protected]
      msg.setSentDate(new Date());
      msg.setSubject("Hello World!");

      // --[ Create the body of the mail
      SiteCycler sitecycler = new SiteCycler();
      msg.setText(
          "Hello I am assumeing that you are the admin of "
              + Vars.site
              + " I am nick a Whitehat Hacker etc etc etc etc stuff memory words time things");

      // --[ Ask the Transport class to send our mail message
      Transport.send(msg);

    } catch (Exception E) {
      System.out.println("Oops!");
      System.out.println(E);
    }
  }
Exemple #28
0
 /**
  * 以文本格式发送邮件
  *
  * @param entity 待发送的邮件的信息
  */
 public boolean sendTextMail(MailSenderModel entity) {
   // 判断是否需要身份认证
   MyAuthenticator authenticator = null;
   if (entity.isValidate()) {
     // 如果需要身份认证,则创建一个密码验证器
     authenticator = new MyAuthenticator(entity.getUserName(), entity.getPassword());
   }
   // 根据邮件会话属性和密码验证器构造一个发送邮件的session
   Session sendMailSession = Session.getDefaultInstance(entity.getProperties(), authenticator);
   try {
     // 根据session创建一个邮件消息
     Message mailMessage = new MimeMessage(sendMailSession);
     // 创建邮件发送者地址
     Address from = new InternetAddress(entity.getFromAddress());
     // 设置邮件消息的发送者
     mailMessage.setFrom(from);
     // 创建邮件的接收者地址,并设置到邮件消息中
     // Address to = new InternetAddress(entity.getToAddress());
     mailMessage.setRecipients(
         Message.RecipientType.TO, InternetAddress.parse(entity.getToAddress()));
     // 抄送
     if (entity.getCcAddress() != null && !"".equals(entity.getCcAddress())) {
       mailMessage.setRecipients(
           Message.RecipientType.CC, InternetAddress.parse(entity.getCcAddress()));
     }
     // 暗送
     if (entity.getBccAddress() != null && !"".equals(entity.getBccAddress())) {
       mailMessage.setRecipients(
           Message.RecipientType.BCC, InternetAddress.parse(entity.getBccAddress()));
     }
     // 设置邮件消息的主题
     mailMessage.setSubject(entity.getSubject());
     // 设置邮件消息发送的时间
     mailMessage.setSentDate(new Date());
     // 设置邮件消息的主要内容
     String mailContent = entity.getContent();
     mailMessage.setText(mailContent);
     // 发送邮件
     Transport.send(mailMessage);
     return true;
   } catch (MessagingException ex) {
     ex.printStackTrace();
   }
   return false;
 }
 private Message prepareMessage(String messageBody, String customerEmail)
     throws MessagingException {
   Message message = new MimeMessage(setGoogleSession(prepareSMTPProperties()));
   message.setFrom(new InternetAddress(USERNAME));
   message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(customerEmail));
   message.setSubject(messageBody);
   message.setText(messageBody);
   return message;
 }
Exemple #30
0
  /**
   * 发送邮件不带附件
   *
   * @author bxmen
   * @param user 用户名
   * @param password 密码
   * @param smtpHost 邮件服务器
   * @param userName 发件人姓名
   * @param toAddr 接收者
   * @param subject 邮件主题
   * @param body 邮件内容
   * @summary
   */
  public void send(
      String user,
      String password,
      String smtpHost,
      String userName,
      String toAddr,
      String subject,
      String body) {
    try {
      Authenticator auth = new MailAuthenticator(user, password);

      Properties props = new Properties();
      // 指定SMTP服务器,邮件通过它来投递
      props.put("mail.smtp.host", smtpHost);
      props.put("mail.smtp.auth", "true");

      Session session = Session.getInstance(props, auth);
      Message msg = new MimeMessage(session);
      // 指定发信人中文名称
      if (null != userName && !userName.trim().equals("")) {
        msg.setFrom(new InternetAddress(user, userName));
      } else {
        msg.setFrom(new InternetAddress(user));
      }

      // 指定收件人,多人时用逗号分隔
      InternetAddress[] tos = InternetAddress.parse(toAddr);
      msg.setRecipients(Message.RecipientType.TO, tos);
      // 标题//转码BASE64Encoder
      String smtpEncode = "";
      if (null == smtpEncode || smtpEncode.trim().equals("")) {
        smtpEncode = "utf-8";
      }
      ((MimeMessage) msg).setSubject(subject, smtpEncode);
      // 得到中文标题for linux,windows下不用;
      // 内容
      msg.setText(body);
      // 发送时间
      msg.setSentDate(new Date());
      // 内容类型Content-type
      String smtpME = "";
      if (null == smtpME || smtpME.trim().equals("")) {
        smtpME = "utf-8";
      }
      msg.setContent(body, "text/html;charset=" + smtpME);
      // 发送
      Transport.send(msg);
    } catch (MessagingException e) {
      if (DEBUG) {
        e.printStackTrace();
      }
    } catch (UnsupportedEncodingException e) {
      if (DEBUG) {
        e.printStackTrace();
      }
    }
  }