This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes easiest way to send an email from a java program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "easiest way to send an email from a java program?" Watch "easiest way to send an email from a java program?" New topic
Author

easiest way to send an email from a java program?

Ben David
Ranch Hand

Joined: Oct 01, 2010
Posts: 67
what is the easiest way to send an email from a java program?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 01, 2010
Posts: 67
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.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1

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


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Ben David
Ranch Hand

Joined: Oct 01, 2010
Posts: 67
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 01, 2010
Posts: 67
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

Joined: Oct 01, 2010
Posts: 67
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

Joined: Oct 01, 2010
Posts: 67
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 01, 2010
Posts: 67
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 01, 2010
Posts: 67
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

Joined: Oct 01, 2010
Posts: 67
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 01, 2010
Posts: 67
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Prabu Dhanapal
Greenhorn

Joined: Aug 12, 2011
Posts: 21
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.
Aatif Imran
Greenhorn

Joined: Aug 15, 2013
Posts: 1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: easiest way to send an email from a java program?
 
Similar Threads
send email attachment
A couple questions
how to send email from servlet program
ClassLoading?
Need to Invoke a Java Program from a trigger