0

How do I sent an email in Java so that when Outlook displays it as a notification it will automatically show a "Copy" button like when logging into ChatGPT?

enter image description here

Here is what I have tried. It sends the email nicely, but the notification does not include the "Copy"

// HTML Template 
    private static final String OTP_EMAIL_TEMPLATE_HTML = 
        "<html>"
        + "<head>"
        + "<style>"
        + "  body { font-family: Arial, sans-serif; line-height: 1.6; color: #374151; }"
        + "  .container { max-width: 600px; margin: 20px auto; padding: 30px; border: 1px solid #d1d5db; border-radius: 12px; background-color: #ffffff; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); }"
        + "  .header { font-size: 24px; font-weight: bold; color: #1f2937; margin-bottom: 25px; border-bottom: 1px solid #e5e7eb; padding-bottom: 15px; }"
        + "  .otp-box { text-align: center; margin: 30px 0; }"
        + "  .otp-code { "
        + "    font-size: 40px; "
        + "    font-weight: 900; "
        + "    color: #059669; /* Bright Green */ "
        + "    letter-spacing: 5px; "
        + "    display: inline-block; "
        + "    padding: 18px 30px; "
        + "    border: 3px dashed #a7f3d0; "
        + "    border-radius: 10px; "
        + "    background-color: #f0fdf4; "
        + "    white-space: nowrap; "
        + "  }"
        + "  .warning { color: #b91c1c; font-weight: bold; }"
        + "  .detail { font-size: 14px; color: #6b7280; margin-top: 15px; border-top: 1px dashed #e5e7eb; padding-top: 15px; }"
        + "</style>"
        + "</head>"
        + "<body>"
        + "<div class='container'>"
        + "  <div class='header'>One-Time Passcode for %s</div>" // %1$s: sysName
        + "  <p>Dear %s,</p>" // %2$s: firstName
        + "  <p>You recently requested a login to your %s account. Please use the following **Verification Code** to proceed:</p>" // %3$s: sysName
        + "  <div class='otp-box'>"
        + "    <span class='otp-code'>%s</span>" // %4$s: code (The actual digits)
        + "  </div>"
        + "  <p class='warning'>This code expires in 5 minutes and should NEVER be shared with anyone.</p>"
        + "  <div class='detail'>"
        + "    <p>Login Attempt Details:</p>"
        + "    <p>%s</p>" // %5$s: formattedUserAgent
        + "    <p>If this was NOT you, please contact security immediately at: <a href='mailto:%s'>%s</a></p>" // %6$s, %7$s: securityEmail
        + "  </div>"
        + "</div>"
        + "</body>"
        + "</html>";

    //Plain Text Template 
   
    private static final String PLAIN_TEXT_TEMPLATE =
        "Dear %s,\n\n" + // %1$s: firstName
        "You recently requested a login to your %s account. Please use the following One-Time Password (OTP) to proceed:\n\n" + // %2$s: sysName
        
        "********************\n" +
        "  VERIFICATION CODE: %s \n" + // %3$s: code
        "********************\n\n" +
        "This code expires in 5 minutes and should NEVER be shared with anyone.\n\n" +
        "Login Attempt Details:\n%s\n\n" + // %4$s: formattedUserAgent
        "If this was NOT you, please contact security immediately at %s\n\n" + // %5$s: securityEmail
        "Thank you,\n" +
        "The [Your Service Name] Team";


   
    public synchronized int sendSecurityCode(
        String dsName,
        HttpServletRequest request,
        SecretClient secretClient) {

        // 1. Generate Code and Get System Properties
        int code = generate6DigitNum();
        String sysName = Monitor.getPropertyValue("SYSTEM_NAME");
        String securityEmail = Monitor.getPropertyValue("SECURITY_EMAIL");

        // 2. Update Security Code in Database
        int result = updateSecCode(dsName, code);

        // 3. Send Email only if DB update was successful (result == 1)
        if (result == 1) {
            UserAgentFormatter ua = new UserAgentFormatter();
            String formattedUserAgent = ua.formatUserAgent(request);
            String codeString = String.valueOf(code);

            // 4. Generate the HTML Content
            String htmlMessage = String.format(
                OTP_EMAIL_TEMPLATE_HTML,
                sysName, // %1$s
                firstName, // %2$s
                sysName, // %3$s
                codeString, // %4$s: The actual 6-digit code
                formattedUserAgent, // %5$s
                securityEmail, // %6$s
                securityEmail // %7$s
            );

            // 5. Generate the Plain Text Content
            String plainTextMessage = String.format(
                PLAIN_TEXT_TEMPLATE,
                firstName, // %1$s
                sysName, // %2$s
                codeString, // %3$s: The actual 6-digit code
                formattedUserAgent, // %4$s
                securityEmail // %5$s
            );

            // 6. Subject Line
            String subject = String.format("Your %s Verification Code: %d", sysName, code);

            // 7. Send the Email 
            
            DMLUtils.sendEmailHTMLandPlain(
                email,
                
                htmlMessage,
                plainTextMessage, 
                subject,
                secretClient
            );
           
            
           
           
        }

        return result;
    }
3
  • 4
    I don't think it is Actionable Messages / Adaptive Cards, but rather Outlook itself detecting an OTP message. Try to send exactly the same message, maybe just replacing ChatGPT with your company/app name. Does Outlook recognize it? Commented Nov 25 at 19:40
  • Have you viewed the source of the message that does have the copy button and compared it to the formatting of your email message? Commented Nov 26 at 17:20
  • Sorry for delay in replying. I saved the the ChatGPT message as a .htm file using Outlook and read that into my htmlMessage variable using htmlMessage = Files.readString(Path.of(path), Charset.forName("Windows-1252"));. And I saved the text of the ChatGPT message in plainTextMessage but it didn't recognize it! Commented Dec 4 at 17:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.