Installing JavaMail

A plea for help

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 8892 Views - Last Post: 12 January 2009 - 09:22 AM Rate Topic: -----

#1 jurence  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Installing JavaMail

Post icon  Posted 04 December 2008 - 05:51 PM

Hey guys, I'm so glad I found this. I have my final project in intermediate programming coming up, and its going great! So far, I have it so I can create usernames and passwords and store them in a database, log in and all of that jazz. In the end, it is going to be a user-friendly hub that allows file writing, deleting, web browsing and email sending.

Long story short, I got up to reading about how to send emails in java and tried to put in:
import javax.mail.*;

And, I got a great error telling me that it didn't exist.

Being on a public school's computer, I couldn't download the javamail package there, so I had to do it at home. After that, I realized id have to download java sdk6 onto my flash drive, and configure it all to run from my flash drive.

No problem.

But here is where I run into a problem: installing the JavaMail package.

Here are the instructions presented:

 Windows
  -------

  1. Unzip the javamail-1_4_1.zip archive. (you may have already done this)

  2. Set your CLASSPATH to include the "mail.jar" file obtained from 
	 the download, as well as the current directory. 

	 Assuming you unzipped javamail-1_4_1.zip in c:\download the
	 following would work:
	 
	  set CLASSPATH=%CLASSPATH%;c:\download\javamail-1.4.1\mail.jar;.

	Also include the "activation.jar" file that you obtained from
	downloading the JavaBeans Activation Framework, in your CLASSPATH.

	  set CLASSPATH=%CLASSPATH%;c:\download\activation\activation.jar
	
  3. Go to the demo directory

  4. Compile any demo using your java compiler. For example:

	  javac msgshow.java

  5. Run the demo. The '-' option lists the required and optional
	 command-line options to successfully run any demo. For example:

	  java msgshow -

	lists the available options. And

	  java msgshow -T imap -H <mailserver> -U <username> -P <passwd> -f INBOX 5
	
	uses the IMAP protocol to display message number 5 from your INBOX.



I have unzipped the JAF (Which I thought was suppose to come standard in SDK6 now days anyways) but am not a little ... mm... confused as to what to do.

Assuming you unzipped javamail-1_4_1.zip in c:\download the
	 following would work:
	 
	  set CLASSPATH=%CLASSPATH%;c:\download\javamail-1.4.1\mail.jar;.


I have unzipped javamail-1_4_1.zip into g:\ and have all of the contents of the folders, but have no idea as to what it means to set the classpath.

When installing java on my flashdrive, i was instructed to create a .bat file that contains the following to allow java to run off of it:

set Path=\java\bin;%Path%


So, my question basically comes down to: How the hell can I install JavaMail without tearing my hair out? I am good at java programming, but have no idea what these instructions are telling me.

I realize you all want a sample of my code to see how well I am doing so far, but it is unfortunately at school and is not relative to what I need help with.

Anyways, its like- 600 lines right now (Which i know is short by your guys's standards heh)

Any help would be appreciated, thanks in advanced!


,Jurence B)

Is This A Good Question/Topic? 0
  • +

Replies To: Installing JavaMail

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2002
  • View blog
  • Posts: 4,167
  • Joined: 11-December 07

Re: Installing JavaMail

Posted 04 December 2008 - 06:20 PM

OK, the classpath is where the virtual machine looks for the class and jar files. Here's a great tutorial for putting java on a thumb drive:
http://www.dreaminco...wtopic42544.htm

You'll also need to put mail.jar and activation.jar somewhere on your thumb drive.

At the bottom of the tutorial it shows you how to set up the classpath to look for java on the thumb drive. You'll need to add the 2 jar files you've added. This should do the trick (obviously including the full paths from the root directory, but missing out the drive letters):

set Path=\jdk6\bin;mail.jar;actication.jar;%Path%


But since you're using the flash drive to showcase your program why not just go the whole hog and do this:

set Path=\jdk6\bin;mail.jar;actication.jar;%Path%
java MyApp


or use the classpath option on the java command:

java -classpath \jdk6\bin;mail.jar;actication.jar;%Path% MyApp


For a bit of added professionalism, make an autorun.ini to launch your app when the drive is inserted. ;)

Hope that helps!
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Installing JavaMail

Posted 04 December 2008 - 06:24 PM

Quote

(Which I thought was suppose to come standard in SDK6 now days anyways)


You're right, so you don't need to worry about activation.jar. You just need to set the classpath to contain the full path to mail.jar (somewhere in G: ?) and the current directory
Was This Post Helpful? 0
  • +
  • -

#4 jurence  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Re: Installing JavaMail

Posted 04 December 2008 - 06:31 PM

Thanks for that clarification!

How do i set the classpath? What is the classpath?

I have to switch between two computers: mine and at school. I know i ahve to set the JDK every time is witch computers because here it is G:\ and there it is U:\ but I will be able to figure it out if i get it set up on my computer.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2002
  • View blog
  • Posts: 4,167
  • Joined: 11-December 07

Re: Installing JavaMail

Posted 04 December 2008 - 06:35 PM

Did you read my post?
Did you read the tutorial I linked to?
Was This Post Helpful? 0
  • +
  • -

#6 jurence  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Re: Installing JavaMail

Posted 04 December 2008 - 06:43 PM

thanks cfoley! I hit refresh and your post wasn't there, just g00se's. So i will make another .bat with

java -classpath g:\Java\jdk6\bin;mail.jar;activation.jar;%Path% MyApp
in it? That sounds easy enough!

I need to put the mail.jar and activation.jar in the bin folder of my jdk if I'm correct?

And yeah, That's the post i used to install it on my thumb drive :)

Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2002
  • View blog
  • Posts: 4,167
  • Joined: 11-December 07

Re: Installing JavaMail

Posted 04 December 2008 - 07:47 PM

No worries.

You need to put in the path to mail.jar (not just the filename), and you can take out activation.jar if it's in the JRE. You also need to change MyApp to whatever your class name is (and include its directory in the class path).

It's also a good idea to leave off the drive letter in the paths in case the next computer assigns a different letter. Start paths from the root directory (the first back slash)
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Installing JavaMail

Posted 04 December 2008 - 08:57 PM

View Postjurence, on 4 Dec, 2008 - 05:31 PM, said:

Thanks for that clarification!

How do i set the classpath? What is the classpath?

I have to switch between two computers: mine and at school. I know i ahve to set the JDK every time is witch computers because here it is G:\ and there it is U:\ but I will be able to figure it out if i get it set up on my computer.

Talking about professionalism... did you read the previous posts ?
Was This Post Helpful? 0
  • +
  • -

#9 jurence  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Re: Installing JavaMail

Posted 05 December 2008 - 07:34 AM

View Postpbl, on 4 Dec, 2008 - 07:57 PM, said:

View Postjurence, on 4 Dec, 2008 - 05:31 PM, said:

Thanks for that clarification!

How do i set the classpath? What is the classpath?

I have to switch between two computers: mine and at school. I know i ahve to set the JDK every time is witch computers because here it is G:\ and there it is U:\ but I will be able to figure it out if i get it set up on my computer.

Talking about professionalism... did you read the previous posts ?


Quote

thanks cfoley! I hit refresh and your post wasn't there, just g00se's. So i will make another .bat with


No need to be rude.
Was This Post Helpful? 0
  • +
  • -

#10 jurence  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Re: Installing JavaMail

Posted 05 December 2008 - 07:55 AM

Thanks everyone, I got it to work. I kept following the instructions but it wasn't finding it. In JCreator i found an "Add Archive" option, once when i added the mail.jar file it worked fine.

Thanks to all!
Was This Post Helpful? 0
  • +
  • -

#11 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2002
  • View blog
  • Posts: 4,167
  • Joined: 11-December 07

Re: Installing JavaMail

Posted 05 December 2008 - 09:05 AM

Glad you got it working!
Was This Post Helpful? 0
  • +
  • -

#12 jurence  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Re: Installing JavaMail

Posted 05 December 2008 - 10:07 AM

View Postcfoley, on 5 Dec, 2008 - 08:05 AM, said:

Glad you got it working!


Thanks for all of your help! It compiles, but doesn't run.

The gmail servers seem to be down at our school; i got the same error this morning though when they were up.

gmail.com mail.google.com and https://gmail.com all have a page not found

here is my error when i try to execute my code:

--------------------Configuration: StoreFront - Java <Default> - <Default>--------------------
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
  nested exception is:
	java.net.ConnectException: Connection timed out: connect
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
	at javax.mail.Service.connect(Service.java:310)
	at javax.mail.Service.connect(Service.java:169)
	at javax.mail.Service.connect(Service.java:118)
	at javax.mail.Transport.send0(Transport.java:188)
	at javax.mail.Transport.send(Transport.java:118)
	at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:97)
	at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:60)
Caused by: java.net.ConnectException: Connection timed out: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
	at java.net.Socket.connect(Socket.java:519)
	at java.net.Socket.connect(Socket.java:469)
	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
	... 8 more

Process completed.



and here is my code:

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



public class SendMailUsingAuthentication
{

  private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  private static final String SMTP_AUTH_USER = "javaproject.amail@gmail.com";
  private static final String SMTP_AUTH_PWD  = "PASSWORDREMOVED";

  private static final String emailMsgTxt	  = "If you are reading this, you are full of win";
  private static final String emailSubjectTxt  = "Full of win";
  private static final String emailFromAddress = "javaproject.amail@gmail.com";

  // Add List of Email address to who email needs to be sent to
  private static final String[] emailList = {"yngbullwnohorns@gmail.com"};

  public static void main(String args[]) throws Exception
  {
	SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
	smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
	System.out.println("Sucessfully Sent mail to All Users");
  }

  public void postMail( String recipients[ ], String subject,
							String message , String from) throws MessagingException
  {
	boolean debug = false;

	 //Set the host smtp address
	 Properties props = new Properties();
	 props.put("mail.smtp.host", SMTP_HOST_NAME);
	 props.put("mail.smtp.auth","true");

	Authenticator auth = new SMTPAuthenticator();
	Session session = Session.getDefaultInstance(props, auth);

	session.setDebug(debug);

	// create a message
	Message msg = new MimeMessage(session);

	// set the from and to address
	InternetAddress addressFrom = new InternetAddress(from);
	msg.setFrom(addressFrom);

	InternetAddress[] addressTo = new InternetAddress[recipients.length];
	for (int i = 0; i < recipients.length; i++)
	{
		addressTo[i] = new InternetAddress(recipients[i]);
	}
	msg.setRecipients(Message.RecipientType.TO, addressTo);


	// Setting the Subject and Content Type
	msg.setSubject(subject);
	msg.setContent(message, "text/plain");
	Transport.send(msg);
 }


/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{

	public PasswordAuthentication getPasswordAuthentication()
	{
		String username = SMTP_AUTH_USER;
		String password = SMTP_AUTH_PWD;
		return new PasswordAuthentication(username, password);
	}
}

}





Help would be appreciated! Thanks in advanced. Thanks again for your help cfoley :D

This post has been edited by jurence: 05 December 2008 - 10:08 AM

Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2002
  • View blog
  • Posts: 4,167
  • Joined: 11-December 07

Re: Installing JavaMail

Posted 05 December 2008 - 11:22 AM

I'd love to help further but I'm afraid I've never used Java mail. Sorry I can't be of assistance.
Was This Post Helpful? 0
  • +
  • -

#14 jurence  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Re: Installing JavaMail

Posted 05 December 2008 - 03:10 PM

Oh, and I changed the port to 465 in which Gmail works off of and it sitll doenst work :(
Was This Post Helpful? 0
  • +
  • -

#15 raaji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-December 08

Re: Installing JavaMail

Posted 30 December 2008 - 01:37 AM

Hi,


Instead of giving this:SMTP_HOST_NAME
Please give the mail exchange server name which is used to send a mail.



View Postjurence, on 5 Dec, 2008 - 09:07 AM, said:

View Postcfoley, on 5 Dec, 2008 - 08:05 AM, said:

Glad you got it working!


Thanks for all of your help! It compiles, but doesn't run.

The gmail servers seem to be down at our school; i got the same error this morning though when they were up.

gmail.com mail.google.com and https://gmail.com all have a page not found

here is my error when i try to execute my code:

--------------------Configuration: StoreFront - Java <Default> - <Default>--------------------
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
  nested exception is:
	java.net.ConnectException: Connection timed out: connect
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
	at javax.mail.Service.connect(Service.java:310)
	at javax.mail.Service.connect(Service.java:169)
	at javax.mail.Service.connect(Service.java:118)
	at javax.mail.Transport.send0(Transport.java:188)
	at javax.mail.Transport.send(Transport.java:118)
	at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:97)
	at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:60)
Caused by: java.net.ConnectException: Connection timed out: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
	at java.net.Socket.connect(Socket.java:519)
	at java.net.Socket.connect(Socket.java:469)
	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
	... 8 more

Process completed.



and here is my code:

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



public class SendMailUsingAuthentication
{

  private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  private static final String SMTP_AUTH_USER = "javaproject.amail@gmail.com";
  private static final String SMTP_AUTH_PWD  = "PASSWORDREMOVED";

  private static final String emailMsgTxt	  = "If you are reading this, you are full of win";
  private static final String emailSubjectTxt  = "Full of win";
  private static final String emailFromAddress = "javaproject.amail@gmail.com";

  // Add List of Email address to who email needs to be sent to
  private static final String[] emailList = {"yngbullwnohorns@gmail.com"};

  public static void main(String args[]) throws Exception
  {
	SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
	smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
	System.out.println("Sucessfully Sent mail to All Users");
  }

  public void postMail( String recipients[ ], String subject,
							String message , String from) throws MessagingException
  {
	boolean debug = false;

	 //Set the host smtp address
	 Properties props = new Properties();
	 props.put("mail.smtp.host", SMTP_HOST_NAME);
	 props.put("mail.smtp.auth","true");

	Authenticator auth = new SMTPAuthenticator();
	Session session = Session.getDefaultInstance(props, auth);

	session.setDebug(debug);

	// create a message
	Message msg = new MimeMessage(session);

	// set the from and to address
	InternetAddress addressFrom = new InternetAddress(from);
	msg.setFrom(addressFrom);

	InternetAddress[] addressTo = new InternetAddress[recipients.length];
	for (int i = 0; i < recipients.length; i++)
	{
		addressTo[i] = new InternetAddress(recipients[i]);
	}
	msg.setRecipients(Message.RecipientType.TO, addressTo);


	// Setting the Subject and Content Type
	msg.setSubject(subject);
	msg.setContent(message, "text/plain");
	Transport.send(msg);
 }


/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{

	public PasswordAuthentication getPasswordAuthentication()
	{
		String username = SMTP_AUTH_USER;
		String password = SMTP_AUTH_PWD;
		return new PasswordAuthentication(username, password);
	}
}

}





Help would be appreciated! Thanks in advanced. Thanks again for your help cfoley :D

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2