My application is need to send email to the selected persons. my application is totally java based. and i even i dont know how to use my gmail account as SMTP mail.
Asked
Active
Viewed 577 times
1
-
1http://stackoverflow.com/questions/3649014/send-email-using-java – Joddy Dec 26 '12 at 11:57
-
Take a look at http://stackoverflow.com/a/483218/49251 – DWright Dec 26 '12 at 11:58
-
1http://tutorial4java.blogspot.in/2012/07/java-mail-api-tutorial.html Check This Blog too... Provides good information... – NamingException Dec 26 '12 at 12:20
-
possible duplicate of [How do you send email from a Java app using Gmail?](http://stackoverflow.com/questions/46663/how-do-you-send-email-from-a-java-app-using-gmail) – int3 Dec 26 '12 at 16:43
3 Answers
1
Here is a sample, change from
, pass
and to
based on your requirement
String host = "smtp.gmail.com";
String from = "username";
String pass = "password";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String[] to = {"to@gmail.com"}; // added this line
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

Jayamohan
- 12,734
- 2
- 27
- 41
-
-
This is for GMAIL SMTP only. Please refer the smtp host name and port. String host = "smtp.gmail.com". Its pointing to Gmail only. Just add your user name in _from_ and password in _pass_. The code will work. – Jayamohan Dec 26 '12 at 12:05
0
if you are in windows you can use Fake Sendmail with wamp server its works great her is a tutorial to how install it and configure it :

haffane hatim
- 474
- 5
- 30