How to send mail by mutiple People using JAVA

Requirement: 
1) You need to have JAVA  installed in your PC
2) You need to configure JAVA in your PC, to test if JAVA in configured in your PC. Open Command Prompt in type: JAVAC
if you have a response means JAVA it's installed
3) You need a JAVA MAIL library you can download it here:https://www.oracle.com/technetwork/java/javamail/index-138643.html
4) You need to add this code in your IDE. can be (BLUE J, NETBEANS or ECLIPSE)
5)You need an INTERNET connection
6) COPY and PASTE this code in your IDE:


import java.util.Properties;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress ; 
import javax.mail.internet.MimeMessage;

import java.util.Properties;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress ; 
import javax.mail.internet.MimeMessage;

public class EmailSender{
 static String[] to={"jbej90@gmail.com","alexisdodo20@gmail.com","antoinewood92@gmail.com","harlyauguste@gmail.com"};

static String from="YourEmailAdress";
static  String password="YourPassword";
static String message="*Rien ne sert de courir il faut partir à point* Telecharge l'application mobile,clic ici: http://bit.ly/proverbehaitien";

public EmailSender(){

String host="smtp.gmail.com";
Properties props=System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true");
Session  session=Session.getDefaultInstance(props);
MimeMessage mimeMessage = new MimeMessage(session);
try{
mimeMessage.setFrom(new InternetAddress(from));
// now get the adress of reciepents
InternetAddress[] toAddress = new InternetAddress[to.length];
for(int i=0;i<to.length;i++){
  toAddress[i] =new InternetAddress(to[i]);

}
 for(int i=0;i<toAddress.length;i++){
       mimeMessage.addRecipient(RecipientType.TO, toAddress[i]);
}
//add subject
mimeMessage.setSubject("Telecharge [Proverbe Haitien] sur PLAYSTORE");
//set message to mimeMessage
mimeMessage.setText(message);
Transport transport = session.getTransport("smtp");
transport.connect(host,from,password);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients() );
transport.close();


}

catch(MessagingException me)  {
me.printStackTrace();

}



}

}

Now Compile and RUN this code in your IDE. 

Explication: This code uses GMAIL smtp server , it uses your GMAIL account, user and password to send mail to different email address.

WIll not WORK:
1) If you don't have INTERNET connection
2) If your password it's not valid
3) If you don't put email correctly
4) If you didn't put the JAVA MAIL library correctly

N.B: you need to place JAVA MAIL  API in the same place where your project are located

The JAVA MAIl API can be foud here: https://www.oracle.com/technetwork/java/javamail/index-138643.html

Having difficulties to complete this tutorial? Feel free to download this mobile app and chat with me in Message button. I am always available:



Commentaires

Posts les plus consultés de ce blog

Men kijan pou voye MINUT NATCOM-NATCOM pou ZANMIW an 3 etap

Comment partager des minutes sur natcom

Men kijan pou prete kob, minutes, lajan nan men NATCOM Ayiti