As we send the email, we can send the html content also.
For better understanding of this example, learn the steps of sending email using JavaMail API first. |
For receiving or sending the email using JavaMail API, you need to load the two jar files:
download these jar files (or) go to the Oracle site to download the latest version.
Example of sending email with html content using JavaMail API
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.activation.*;
-
- class SendHtmlEmail
- {
- public static void main(String [] args)
- {
-
- String host="mail.javatpoint.com";
- String to="sonoojaiswal1987@gmail.com";
- final String user="sonoojaiswal@javatpoint.com";
- final String password="xxxxx";
-
- Properties properties = System.getProperties();
- properties.setProperty("mail.smtp.host", );
- properties.put("mail.smtp.auth", "true");
-
- Session session = Session.getDefaultInstance(properties,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user,password);
- }
- });
-
- try{
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(user));
- message.addRecipient(Message.RecipientType.TO,
- new InternetAddress(to));
-
- message.setSubject("HTML Message");
- message.setContent("<h1>sending html mail check</h1>","text/html" );
-
- Transport.send(message);
- System.out.println("message sent....");
- }catch (MessagingException ex) {ex.printStackTrace();}
- }
- }
Load the jar file | c:\> set classpath=mail.jar;activation.jar;.; |
compile the source file | c:\> javac SendHtmlEmail.java |
run by | c:\> java SendHtmlEmail |
|
No comments:
Post a Comment