Enter your email address:

Delivered by FeedBurner


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Send Email in Java
12-12-2011, 10:27 PM (This post was last modified: 12-18-2011 11:21 PM by rajasri.)
Post: #1
Tongue Send Email in Java
Today we are going to learn how to send a Authenticated mail using java
Before proceed to code you need to have certain prerequirements ie jar files Download it from
1.Mail.jar(bundled in javamail1.4.2) In order to view links, you must have to reply to this thread.
2.Activation.jar(bundled in jaf(java activation framework) In order to view links, you must have to reply to this thread.
Now look at the code below

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

public class Main
{
String fromemail = "youremail@gmail.com",
password = "yourpassword",
host = "smtp.gmail.com",
port = "465",
toemail = "receiveremail@any.com",
subject = "Enter your subject Here",
yourtext = "Enter Your Message text Here";

public Main()
{
Properties p = new Properties();
p.put("mail.smtp.user", fromemail);
p.put("mail.smtp.host", host);
p.put("mail.smtp.port", port);
p.put("mail.smtp.starttls.enable","true");
p.put("mail.smtp.auth", "true");
p.put("mail.smtp.socketFactory.port", port);
p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
p.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(p, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(yourtext);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(fromemail));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toemail));
Transport.send(msg);
System.out.println("Message sent successfully");
}
catch (Exception mex)
{
mex.printStackTrace();
}
}

public static void main(String[] args)
{
Main call = new Main();
}

private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(fromemail, password);
}
}
}


Save this file as Main.java and set the classpath to the jar files like below

E:\projectfolder>set classpath="E:\projectfolder\activation.jar;E:\j af-1_1_1\jaf-1.1.1\mail.jar;E:\projectfolder\dsn.jar;E:\jaf-1_1_1\jaf-1.1. 1\imap.jar;E:\projectfolder\mailapi.jar;E:\projectfolder\pop3.jar;E: \projectfolder\smtp.jar;E:\projectfolder\servlet-api.jar;


Now compile the file "javac Main.java "and run it "java Main" now look your email client mail has been successfully sent,
Any doubt ask us.

Happy coding
Find all posts by this user
Add Thank You Quote this message in a reply
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Technorati Twitter Digg MySpace Delicious

Possibly Related Threads...
Thread: Author Replies Views: Last Post
Tongue Students management java project download rajasri 25 2,290 05-15-2012 12:45 PM
Last Post: itischinmay
  shopping cart in jsp&java admin 6 1,072 04-13-2012 08:51 PM
Last Post: sushantkop
Sad java.sql.SQLException: General error solution rajasri 3 1,090 02-07-2012 03:05 PM
Last Post: shahbaz07dbit
Sad create a file from form input in java,jsp rajasri 0 258 12-19-2011 06:32 PM
Last Post: rajasri
Wink Reading Html code of all the url using java rajasri 0 187 12-19-2011 06:17 PM
Last Post: rajasri
Star pagination in jsp,java rajasri 0 238 12-19-2011 05:32 PM
Last Post: rajasri
Music How to extract war file in java rajasri 0 2,887 12-14-2011 12:07 PM
Last Post: rajasri
Thumbs Up Solution for java.lang.NoClassDefFoundError: javax/transaction/Synchronization rajasri 0 1,013 12-14-2011 12:00 PM
Last Post: rajasri
Video How to use batch update in java and jsp rajasri 0 471 12-12-2011 11:38 PM
Last Post: rajasri
Bug Print current time using java in jsp page rajasri 0 620 12-12-2011 11:20 PM
Last Post: rajasri

Forum Jump:


User(s) browsing this thread: 1 Guest(s)