maria salahuddin
maria salahuddin

Reputation: 125

How to send Link on html button click in java mail?

In my code I send reset password mail to the user , now I want to send some html content in the mail , I want a button on which user clicks and then reset password links opens in new tab , but I don't know how to embedded a link on html button , my link is saved in variable token here is my code

MailServiice.java

public void sendMail(String email,String token)
    {
        this.email=email;
        this.token=token;
     try {
               // Create a default MimeMessage object.
               Message message = new MimeMessage(session);

               // Set From: header field of the header.
               message.setFrom(new InternetAddress(from));

               // Set To: header field of the header.
               message.setRecipients(Message.RecipientType.TO,
                       InternetAddress.parse(to));

               // Set Subject: header field
               message.setSubject("change password request");



               // Now set the actual message 
              //  message.setText(messages);
                message.setContent(
                        "<h2>Reset password request </h2>" + 
                        "<h3> Plaese click on the button to reset password \n </h3> "+
                        " <a href=token>" +"<button>Reset your password</button></a>" + token,
                        "text/html");


               // Send message
               Transport.send(message);

               System.out.println("Sent message successfully....");

              } catch (MessagingException e) {
                 throw new RuntimeException(e);
              }

Upvotes: 1

Views: 2703

Answers (1)

sajid
sajid

Reputation: 113

I hope it will help. I have added an JsFidler for you.

     var varificetioncode = "code-to-send";
     var token_link = "http://example.org/questions/123/" + varificetioncode;
     message.setContent(
                        "<h2>Reset password request </h2>" + 
                        "<h3> Please click on the button to reset password </h3>"+
                        "<a target='_blank' href="+ token_link +">
                           <button>Reset your password</button>
                        </a>",
                       "text/html; charset=utf-8"
                      );

Upvotes: 2

Related Questions