Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I need help with a code where I need to send emails using java. There are thr

ID: 3832745 • Letter: H

Question

Hi I need help with a code where I need to send emails using java. There are three options to how you can send email 1) send an email to everyone in the text file by reading that textfile 2)send an email to multiple users by typing up the email adresses and 3) send an email to one ser by typing up the email address. Here's my code. import java.util.Scanner;

import java.util.*;

import java.io.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

public class ExtraCredit1

{

public static void main(String[]args)throws Exception

{

Scanner keyboard= new Scanner(System.in);

System.out.println("You have 3 options");

System.out.println("-------------------");

System.out.println("Option 1: Send an email to everyone");

System.out.println("Option 2: Send an email to multiple users by typing in the email");

System.out.println("Option 3: Send an email to one user");

System.out.println("Please type in an option");

int optionEmail=keyboard.nextInt();

switch(optionEmail)

{

case 1:

emailAll();

break;

case 2:

emailFew();

break;

case 3:

emailOne();

break;

default:

System.out.println("Error...");

break;

}

}

public static void emailAll()throws IOException

{

File file=new File("email.txt");

Scanner inputFile=new Scanner(file);

while(inputFile.hasNext())

{

String str=inputFile.nextLine();

}

}

public static void emailFew() throws IOException

{

Scanner keyboard=new Scanner(System.in);

//String selectedEmail= keyboard.nextLine();

String to=keyboard.nextLine();

String from="";

String host= "localhost";

Properties properties=System.getProperties();

properties.setProperty("mail.smtp.host",host);

Session session=Session.getDefaultInstance(properties);

try

{

MimeMessage message=new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

message.setSubject("This is the Subject Line!");

message.setText("This is actual message");

Transport.send(message);

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

}catch (MessagingException mex)

{

mex.printStackTrace();

}

} public static void emailOne() throws IOException

{

Scanner keyboard=new Scanner(System.in);

String to=keyboard.nextLine();

String from="";

String host= "localhost";

Properties properties=System.getProperties();

properties.setProperty("mail.smtp.host",host);

Session session=Session.getDefaultInstance(properties);

try

{

MimeMessage message=new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

message.setSubject("This is the Subject Line!"); message.setText("This is actual message");

Transport.send(message);

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

}catch (MessagingException mex)

{

mex.printStackTrace();

}

}

}

Explanation / Answer

import java.util.Scanner;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class ExtraCredit1
{
   public static void main(String[]args)throws Exception
   {
       Scanner keyboard= new Scanner(System.in);
       System.out.println("You have 3 options");
       System.out.println("-------------------");
       System.out.println("Option 1: Send an email to everyone");
       System.out.println("Option 2: Send an email to multiple users by typing in the email");
       System.out.println("Option 3: Send an email to one user");
       System.out.println("Please type in an option");
       int optionEmail=keyboard.nextInt();
       switch(optionEmail)
       {
           case 1:
           emailAll();
           break;
           case 2:
           emailFew();
           break;
           case 3:
               Scanner sc=new Scanner(System.in);
               String to=sc.nextLine();
               emailOne(to);
           break;
           default:
           System.out.println("Error...");
           break;
       }
   }
   public static void emailAll()throws IOException
   {
       File file=new File("email.txt");
       Scanner inputFile=new Scanner(file);
       while(inputFile.hasNext()){
           String str=inputFile.nextLine();
           System.out.println("Sending email to "+str);
           emailOne(str);
       }
   }
   public static void emailFew() throws IOException
   {
       Scanner keyboard=new Scanner(System.in);
       System.out.print("Enter Email(type end to stop): ");
       String to=keyboard.nextLine();
       while(!to.equalsIgnoreCase("end")){
           emailOne(to);
           System.out.print("Enter Email(type end to stop): ");
           to = keyboard.nextLine();
       }
      
   }
   public static void emailOne(String to) throws IOException
   {
       String from="xxxx_xx@xxx.com";
       String host= "localhost";
       Properties properties=System.getProperties();
       properties.setProperty("mail.smtp.host",host);
       Session session=Session.getDefaultInstance(properties);
       try
       {
           MimeMessage message=new MimeMessage(session);
           message.setFrom(new InternetAddress(from));
           message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
           message.setSubject("This is the Subject Line!"); message.setText("This is actual message");
           Transport.send(message);
           System.out.println("Sent message successfully...");
       }catch (MessagingException mex)
       {
           mex.printStackTrace();
       }
   }
}