• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Send Outlook Email with Outlook

 
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It took me a few minutes to figure out why I got the same error. The issue is in your dependencies - you don't have any.

You've added some dependencies inside the dependencyManagement element. That doesn't add any dependencies to your project itself. It's like a declaration of dependencies you can use to avoid copying the same stuff like versions. To add the dependencies to your project itself, add a dependencies element as a child element of the dependencyManagement element. Since you're already using dependencyManagement, you can use what's declared inside, there's no need to redeclare the version:

Note that I only added one of the dependencies. The two dependencies are different artifacts for the same library - JavaMail. There's been some refactoring in the groupIds and artifactIds between versions, but the first dependency is just a newer version of the first. There is one major difference though - the first one only includes the API, not an actual implementation. If you want to use version 1.5 or higher, you need to include another dependency for that: com.sun.mail:javax.mail.

Here's the final POM I created from your POM:

Note that I stuck with JavaMail 1.5.6 and removed the 1.4 version.
 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
I have used POM.XML by pasting in the dependency blocks.   I think that there is more to it than how I am using them.  Before it just worked when I needed it.  
Your explanation sounds great but I can tell I am missing a lot of background on the topic.

I had more dependencies in my



block.

I added these lines of code inside of the block above that comes from your answer.

Then after the </dependencyManagement>

I added

your code



I kept the
</project>

which closes the XML.

I do a right click on the XML code and then select Maven / Reload Project

I get this red error message:

Cannot resolve javax.mail:javax.mail-api:${javax.mail.version}



I don't know enough to know what is wrong.

Thank you for your assistance and I gave you a pie.

Thanks,

Kevin
 
Saloon Keeper
Posts: 7585
176
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're probably missing the "properties" section in your POM. That'S where javax.mail.version is defined.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:Rob,
I have used POM.XML by pasting in the dependency blocks.   I think that there is more to it than how I am using them.  Before it just worked when I needed it.  
Your explanation sounds great but I can tell I am missing a lot of background on the topic.


If it worked before, you probably didn't put the dependencies inside a dependencyManagement element. If you want to know more, I suggest you read https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html.

Thank you for your assistance and I gave you a pie.


Thanks

Tim Moores wrote:You're probably missing the "properties" section in your POM. That'S where javax.mail.version is defined.


Tim is right. Because the API and implementation should have compatible versions, I defined the version as a property so you only need to change it in one location if you want to use a newer version.
 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim and Rob,
Yes.  I was missing the line of code in the properties section.  
   <javax.mail.version>1.5.6</javax.mail.version>

Now it is rebuilding OK.

I took a look at the link for Maven.  I read some of it but its too much to absorb at one sitting.  

Now I can work on getting sending mail through Outlook to work.

Thanks,

Kevin
 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if I should have started a new thread but I'm still on the topic of javax and sending mail.

The code below gives the error:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;

Should I be contacting a network administrator for this or is there something wrong with the code?

I try to give credit when I use other people's code on the internet but I lost track of the author.  







 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that "localhost" means the computer that you're running that code on. So you're trying to connect to an SMTP server on that computer, and you aren't running one there.

But you mentioned network administrators. Yes, this would be a good time to get your network admins involved if you plan to send e-mail from your company's server. Even (or especially) if it's only for testing.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic