AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-mail: Java program to send an email

Please write a java program to send an email.

java x 211
mail x 2
Posted On : 2016-04-12 22:21:18.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188232063
up-rate
3
down-rate

Answers


First of all to send mail by using Java Mail API, you need to add 2 jar files(mail.jar, activation.jar) in you project.

Click Here to download these jar files

Notify.java:
----------------------------------------------
package in.anyforum;

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

public class Notify{

public static void notify(String subject,String msg,List<String> list){


String host="mail.anyforum.in";//change accordingly

final String user="support@anyforum.in";//change accordingly
final String epassword="****";//change accordingly

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");

Session msession = Session.getDefaultInstance(properties,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,epassword);
}
});


try{
MimeMessage message = new MimeMessage(msession);
message.setFrom(new InternetAddress(user));



StringBuffer sb=new StringBuffer();

for(int i=0;i<list.size()-1;i++){
sb=sb.append(list.get(i)+",");
}
sb=sb.append(list.get(list.size()-1));
message.addRecipients(Message.RecipientType.BCC,sb.toString());
message.setSubject(subject);
message.setContent("<h1>Updates at anyforum.in</h1><br/>"+msg+"<br/><h3>Thanks & Regards!</h3><br/>anyforum.in","text/html" );

Transport.send(message);

}catch (Exception ex) {ex.printStackTrace();}

}
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("rishi.prince91@gmail.com");
notify("Good luck","Hi<br/>",list);
}

}

Posted On : 2016-04-12 22:49:18
Satisfied : 1 Yes  0 No
profile Saksham Kumar - anyforum.in Saksham Kumar
73433925852
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in