コード例 #1
0
  public void sendVerificationEmailLocally(String recipients, String verificationLink) {

    String body = String.format(bodyTemplate, verificationLink);

    log.debug("Sending email to recipients={}, subject={}, body={}", recipients, subject, body);

    // Gmail properties
    Properties smtpProperties = new Properties();

    smtpProperties.put("mail.smtp.host", smtpHost);
    smtpProperties.put("mail.smtp.socketFactory.port", smtpPort);
    smtpProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    smtpProperties.put("mail.smtp.auth", "true");
    smtpProperties.put("mail.smtp.port", "465");

    Session session =
        Session.getInstance(
            smtpProperties,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(smtpUsername, smtpPassword);
              }
            });

    try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(fromAddress));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
      message.setSubject(subject);

      message.setContent(body, "text/html; charset=utf-8");
      Transport.send(message);
      log.info("Sent email to " + recipients);
    } catch (MessagingException e) {
      String smtpInfo =
          "Error sending email. SMTP_HOST="
              + smtpHost
              + ", SMTP_PORT="
              + smtpPort
              + ", smtpUsername="******", subject="
              + subject;
      if (e.getCause() instanceof AuthenticationFailedException) {
        log.warn(
            "Failed to send mail due to missconfiguration? Reason {}", e.getCause().getMessage());
      }
      throw new RuntimeException(smtpInfo, e);
    }
  }
コード例 #2
0
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Properties prop = new Properties();
    Session session = Session.getDefaultInstance(prop, null);
    SendMail sm = new SendMail();
    String br = "\n";

    try {
      // 受信メールを取得
      MimeMessage recMsg = new MimeMessage(session, req.getInputStream());

      DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      String sentDate = df.format(recMsg.getSentDate());
      String from = recMsg.getFrom().toString();
      String subject = recMsg.getSubject();
      String content = this.getText(recMsg.getContent());

      String msgBody = "";
      msgBody += "sent at : " + sentDate + br;
      msgBody += "from : " + from + br;
      msgBody += "subject : " + subject + br;
      msgBody += "-----------content----------" + br + content;
      sm.send("*****@*****.**", "", "sutekitest転送", msgBody);
    } catch (MessagingException e) {
      sm.send("*****@*****.**", "", "sutekitestメール受信エラー", e.getMessage() + br + e.getCause());
    }
  }