Question

Write a Java program that uses a java mail API that sends an encrypted email from...

Write a Java program that uses a java mail API that sends an encrypted email from google mail using TLS and giving the MAIL FROM, MAIL TO, SUBJ, and message

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For this program, we need to keep the following things into our mind.

  1. We need to enable the authentification in the email process, so for that we need to provide the usename and password.
  2. We need to enable TLS and we neeed to set the port which is going to be used by the SMTP.
  3. Then we need to start a session in which first the authentication has to be done using the username and the password.
  4. Now all the parameters have been set, so we need to create the message, we need to setFrom, setRecepients for mail-to and setSubject for subject of the email, and lastly the text of the message by setText.
  5. Then we need to use the static method provided by the transport class.

For running the below program, you need to have javax-mail jar file set up properly and then you need to change the value of email and pass in the program and then you need to set the value of to_email to the email where you want to send this test message and last you just need to remove spcae present between "i" and "l" in the code below, I am highlighting it at that place.

You may need to change some settings in your google account, I took these from google website. Its just a single step.

  1. open google mail.
  2. click Settings on right side.
  3. open setting
  4. open the forwarding_POP/IMAP tab.
  5. enable IMAP in IMAP access.
  6. Click Save Changes.

Following is the program.

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

public class Email { 

   public static void main(String[] args) { 
      
      // Set your own google address here
      final String email = "your_email"; 
      // Set an email to which you want to send email to
      final String to_email = "to_email";
      // writing your password here
      final String pass = "password"; 
      

      // Getting the properties of system
      Properties props = new Properties();          
      
      // here we need to enable the authentification using email and password 
      props.put("mail.smtp.auth", "true");          
      
      // As we are using TLS, we need to turn it on.
      props.put("mail.smtp.starttls.enable", "true");     
      
      // we need to set up the properties object and remove the space between "i" and "l"
      props.put("mail.smtp.host", "smtp.gmai l.com");  
      
      // setting the port used by SMTP for sending email
      props.put("mail.smtp.port", "587");              

      // Creating a session
      Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
         
         //override the getPasswordAuthentication method 
         protected PasswordAuthentication 
                  getPasswordAuthentication() { 
                              
            return new PasswordAuthentication(email, 
                                    pass); 
         } 
      }); 

      try { 
         
         // making a new Message class object to fill in the details of message content
         Message message = new MimeMessage(session);     
         
         // setting the MAIL from in the content of email 
         message.setFrom(new InternetAddress(email)); 
         
         // now setting the receipients, basically means the MAIL TO:
         message.setRecipients(Message.RecipientType.TO, 
            InternetAddress.parse(to_email)); 
         
         // Adding the subject to the email
         message.setSubject("TEST EMAIL"); 
         // adding the text of the email
         message.setText("email has been sent"); 

         
         // this Transport static method send will send the message.
         Transport.send(message);       

         // printing the success message.
         System.out.println("Message has been sent"); 

      } catch (MessagingException e) { 
         // handling the exception.
         throw new RuntimeException(e); 
      } 
   } 
} 

l Problems Javadoc Declaration ConsoleProperties kterminated> Email |Java Application] CAProgram FilesJavaljrel8.0 171 bin\javaw.exe (Dec 2, 2018, 1:26:12 AM Message has been sent

If you have any problem regarding the solution to the problem, do let me know in the comment section below.

Don't forget to like the solution, it really means a lot.

Cheers!

Add a comment
Know the answer?
Add Answer to:
Write a Java program that uses a java mail API that sends an encrypted email from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT