I want to send email from
Java. I keep searching on Javax in CodeRanch and also Googling. It keep seeing code such as the following.
After creating the
Maven project, I put in the dependencies. They did not produce an error.
After adding the import statements and running, it gives an error in the bottom right panel saying:
C:\Users\kevin\IdeaProjects\fixJavax\src\main\java\DoIt\OutlookSend.java:3:18
java: package javax.mail does not exist
I don't know how to debug this. Assistance please.
Credit to code and POM given to:
https://kodejava.org/how-do-i-send-an-email-message/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>fixJavax</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<!--
http://repo1.maven.org/maven2/javax/mail/javax.mail-api/1.5.6/javax.mail-api-1.5.6.jar -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.6</version>
</dependency>
<!--
http://repo1.maven.org/maven2/javax/mail/mail/1.4.7/mail-1.4.7.jar -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
package DoIt;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class OutlookSend {
public static void main(
String[] args) {
String from = "kodejava@gmail.com";
String to = "kodejava@gmail.com";
String subject = "Hi There...";
String text = "How are you?";
// A properties to store mail server smtp information such as the host
// name and the port number. With this properties we create a Session
// object from which we'll create the Message object.
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(properties, null);
try {
// Message is a mail msg to be send through the Transport object.
// In the Message object we set the sender address and the
// recipient address. Both of this address is a type of
// InternetAddress. For the recipient address we can also set the
// type of recipient, the value can be TO, CC or BCC. In the next
// two lines we set the email subject and the content text.
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(text);
// Send the msg to the recipient.
Transport.send(msg, "kodejava", "********");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
C:\Users\kevin\IdeaProjects\fixJavax\src\main\java\DoIt\OutlookSend.java:3:18
java: package javax.mail does not exist