Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 466 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
How to send email using gmail smtp

Hi,
I want to send email using gmail smtp. I downloded a program from internet but its producing lots of errors. The code is given below along with errors:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
10
* Created by anirudh on 28/10/14.
11
*/
public class EmailExample {
public static void main(String args[]) {
final String username = "[email protected]";
final String password = "yourpassword";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Test JCG Example");
message.setText("Hi," +
"This is a Test mail for JCG Example!");
Transport.send(message);
System.out.println("Mail sent succesfully!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
I am getting following errors:
import javax.mail.*;
^
import javax.mail.internet.MimeMessage;
^
EmailExample.java:48: error: cannot find symbol
Session session = Session.getInstance(props,
^
symbol: class Session
location: class EmailExample
EmailExample.java:50: error: package javax.mail does not exist
new javax.mail.Authenticator() {
^
EmailExample.java:48: error: cannot find symbol
Session session = Session.getInstance(props,
^
symbol: variable Session
location: class EmailExample
EmailExample.java:66: error: cannot find symbol
Message message = new MimeMessage(session);
^
symbol: class Message
location: class EmailExample
EmailExample.java:66: error: cannot find symbol
Message message = new MimeMessage(session);
^
symbol: class MimeMessage
location: class EmailExample
EmailExample.java:68: error: cannot find symbol
message.setFrom(new InternetAddress("[email protected]"));
^
symbol: class InternetAddress
location: class EmailExample
EmailExample.java:70: error: package Message does not exist
message.setRecipients(Message.RecipientType.TO,
^
EmailExample.java:72: error: cannot find symbol
InternetAddress.parse("[email protected]"));
^
symbol: variable InternetAddress
location: class EmailExample
EmailExample.java:81: error: cannot find symbol
Transport.send(message);
^
symbol: variable Transport
location: class EmailExample
EmailExample.java:89: error: cannot find symbol
} catch (MessagingException e) {
^
symbol: class MessagingException
location: class EmailExample
13 errors
l>
Some body please guide me about a better email program.
Zulfi.
Answers
-
please look here for your answer:
http://stackoverflow.com/questions/2096283/including-jars-in-classpath-on-commandline-javac-or-apt
bye
TPD
-
Or particularly for this package: http://stackoverflow.com/questions/6606529/package-javax-mail-and-javax-mail-internet-do-not-exist
-
Some body please guide me about a better email program.
The problem is NOT the 'email program'.
The problem in this thread, as with your other thread, is that you are trying to use Java when you haven't learned HOW to use it.
I suggest you STOP trying to write code before you know the language and how it works.
The Java Tutorials has trails on how to use ALL of the basic functionality.
https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Adding Classes to the JAR File's Classpath
You may need to reference classes in other JAR files from within a JAR file.
If you want to reference a class in your code the class MUST BE on the classpath or the jar that contains the class must be in the classpath.
That is one of the FIRST THINGS you learn when you take any basic Java class.
You will continue to have problems if you don't learn the language BEFORE you try to write code.
There are NO SHORTCUTS.
-
I would suggest first write simple programs, and learn the basics of java, for example how to add the classpath etc..
One more thing don't just try to run the downloaded programs, without understanding what that does, and reading the readMe file (pre-requisite to run the program) to compile and execute it .
-
Have you added required jar files in project class path ?
Check this code-
- import java.util.Properties;
- import javax.mail.*;
- import javax.mail.internet.*;
- public class SendMailSSL {
- public static void main(String[] args) {
- String to="[email protected]";//change accordingly
- //Get the session object
- Properties props = new Properties();
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.socketFactory.port", "465");
- props.put("mail.smtp.socketFactory.class",
- "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.port", "465");
- Session session = Session.getDefaultInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("[email protected]","password");//change accordingly
- }
- });
- //compose message
- try {
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress("[email protected]"));//change accordingly
- message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
- message.setSubject("Hello");
- message.setText("Testing.......");
- //send message
- Transport.send(message);
- System.out.println("message sent successfully");
- } catch (MessagingException e) {throw new RuntimeException(e);}
- }
- }
and go through this - http://www.javatpoint.com/example-of-sending-email-using-java-mail-api-through-gmail-server
Ashish