• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

easiest way to send an email from a java program?

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the easiest way to send an email from a java program?
 
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's only one way. You write code to connect to an SMTP server which allows you to send e-mail.

Or were you interested in something more detailed than that? Your question was rather unfocused and I'm having trouble figuring out what you are after.
 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was hoping to be pointed to what people think are the most helpful, easy to use classes that facilitate the process of connecting to an smtp server to send email.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Mail API, I've done it via sockets to an SMTP server as well but ...

http://www.javaworld.com/javaworld/jw-10-2001/jw-1026-javamail.html
 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I am working with Java SE, but the links are broken for the downloads it suggests if you are working with SE. Any chance you could point me to the correct current download links for JavaMail, and JavaBeans Activation Framework?
 
Paul Clapham
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, JavaMail is your friend in this case. And your google keyword, too. Since Oracle took over from Sun they have massively changed the download sites and most old links will be broken. So just google it and you should find the download before long.

Also if you're using Java 6 or later then you don't need to download the Activation Framework any more, since it's built into the standard API now.

The other thing you're going to need, besides software, is access to an SMTP server. If you don't have that then there isn't any point in writing e-mail-sending code.
 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found and got the mail.jar set up and in my classpath. I am a bit confused with the config file.

This is the documentation I am looking at
http://javamail.kenai.com/nonav/javadocs/
I dont see where I can specify the password that goes with the user I specify.

Also -- how do I tell it if it needs to use SSL or not? (I will be trying to connect to a gmail smtp server
 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it working. For anyone stumbiling on this thread here is the class and config file I used to sent through gmail. I hard coded the password in the class. Obviouslyi this is probably really bad form. But for a newb just trying to make a script to send a warning email, it gets the job done. I just used a "junk" gmail account that is not really used for anything.




 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
spoke too soon. This works in eclipse, but I get an error when I try to run on another PC from command line

I tried putting the mail.jar in the folder with the class files incase it needed that to run. Still get the exception below.

Can someone point me in the right direction?



 
Paul Clapham
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben David wrote:I tried putting the mail.jar in the folder with the class files incase it needed that to run.



Clearly it does need that jar, since it's looking for one of the classes in it. But it looks in the classpath. You already knew about the classpath, I see from one of your earlier posts; set it up correctly so that mail.jar is in the classpath.
 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I ask newb questions... I am aware of classpath. I was under the impression that the current directory was always part of the class path. (how else does java find the other two of my own class files in the same folder when I run Java on the one with the static Main method) So when I put the jar in the current folder I thought I was putting it in the classpath.

Is that not correct? The current directory is not always in the class path? and if not, just so I can have a clearer understanding, can someone explain how Java finds my other two class files in the current folder, but fails to find the jar?

 
Paul Clapham
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben David wrote:I was under the impression that the current directory was always part of the class path.



No, that's wrong. The current directory IS the classpath if you don't specify one, otherwise the classpath is what you say it is.

And it's also irrelevant: the class which caused the problem wasn't in your current directory anyway. It was in a jar file.

So you may be "aware" of the classpath but the JVM doesn't care about that. It only cares about what you actually set the classpath to be. So you should spend a few minutes learning how to set your classpath properly.
 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarifying! I see now I can run it like the following...

java -classpath ".;.\mail.jar" DSSRecordingCheck


But now I get a new error. I dont get this when I run it in eclipse. any ideas?

Cannot send email. com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. e2sm8774885ibe.0

 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm..,.. I can run it from command line on the same PC i have eclipse on (a windows 7 PC)

but I can not run it on another PC (win XP PC)

I would not think the OS should make a difference. but it is the only difference I can think of in how I am doing it.

Any ideas?

(I get the following on the other PC)
Cannot send email. com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. e2sm8774885ibe.0
 
Paul Clapham
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure that's one of the entries in the JavaMail FAQ. Let me just google that... yes, it is. Here's the link: JAVAMAIL API FAQ.
 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course -- the thing I dont check the FAQ for is in there. (But not most of the things I did check it for) Dang Murphy.

Sorry and thanks!!

Strange that it worked on my PC just fine though. Curious why that is. Oh well -- just glad its working 100% now.

Thanks again for all your help!


 
Paul Clapham
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I thought that sounded strange too. But that FAQ is an excellent document and it's worth reading if you're working with e-mail. Anyway it sounds like you did something which fixed the problem, so that's good.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this website http://www.compiletimeerror.com/2013/03/java-mail-api-send-email-from-java-code.html for clear solution. Its good.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Obviously your best mate for sending email is java mail. Configure the helow properties and you'll be good.



//System properties
Properties props = new Properties();

// Setup your mail server
props.put("mail.smtp.host", SMTP_HOST);

props.put("mail.smtp.user",FROM_NAME);

props.put("mail.smtp.ssl.enable", "true");

props.put("mail.smtp.port", "25");

props.put("mail.debug", "true");

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable","true");

props.put("mail.smtp.EnableSSL.enable","true");

props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.setProperty("mail.smtp.socketFactory.fallback", "false");

props.setProperty("mail.smtp.port", "465");

props.setProperty("mail.smtp.socketFactory.port", "465");


Infact, I have a complete working example on my blog.See here
reply
    Bookmark Topic Watch Topic
  • New Topic