|
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
|
|||
|
|||
|
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
|
|||
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
|
« Next Oldest | Next Newest »
|
![]() |
|||||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
User(s) browsing this thread: 1 Guest(s)






![[-]](images/royal_red/collapse.gif)






