Adding html attribute in mail injava

In Java, you can add HTML attributes to an email using the HtmlEmail class from the javax.mail package. Here's an example:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class SendHtmlEmail {
    public static void main(String[] args) {
        // Set up email properties
        Properties props = new Properties();
        props.put("mail.smtp.host", "your-smtp-host");
        props.put("mail.smtp.port", "your-smtp-port");
        props.put("mail.smtp.auth", "true");

        // Create a session
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("your-email-username", "your-email-password");
            }
        });

        // Create a message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("your-email-username"));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email-username"));
        msg.setSubject("HTML Email Example");

        // Create a HTML body
        BodyPart htmlBodyPart = new MimeBodyPart();
        htmlBodyPart.setContent("<html><body><h1>Hello World!</h1></body></html>", "text/html");

        // Create a Multipart email
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(htmlBodyPart);

        // Set the email body
        msg.setContent(multipart);

        // Send the email
        Transport.send(msg);
    }
}

In this example, we create a MimeMessage object and set its subject, from, and to addresses. We then create a MimeBodyPart object and set its content to an HTML string using the setContent() method. We then create a Multipart object and add the HTML body part to it. Finally, we set the email body to the Multipart object using the setContent() method and send the email using the Transport.send() method.

You can also use the HtmlEmail class from the org.apache.commons.mail package, which provides a simpler way to send HTML emails:

import org.apache.commons.mail.HtmlEmail;

public class SendHtmlEmail {
    public static void main(String[] args) {
        HtmlEmail email = new HtmlEmail();
        email.setFrom("your-email-username");
        email.setTo("recipient-email-username");
        email.setSubject("HTML Email Example");
        email.setHtmlMsg("<html><body><h1>Hello World!</h1></body></html>");
        email.send();
    }
}

This code creates an HtmlEmail object and sets its from, to, and subject addresses. It then sets the email body to an HTML string using the setHtmlMsg() method and sends the email using the send() method.