• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Sending Email with Java

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When trying to send an email message with javamail, I keep getting an error

unable to find valid certification path to requested target



I think it has something to do with trusted certificates. I am trying to send a message using gmail. Using the newer openjdk 17, is it possible to use javamail to send an email?
 
Saloon Keeper
Posts: 24295
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can send email by sending a series of text commands and data to the mailserver. All JavaMail is doing there is providing convenient methods to package things up and transmit them properly*. So yes, any recent Java implementation of JavaMail can send email to mail.google.com.

On the other hand, gmail's authentication and authorization protocols have been quirky lately, so I can't provide a cookbook solution off my head. Last time I had to deal with it I was setting up an email client on a tablet device and there was much swearing.

* Well, that and provide an API to run the process in reverse allowing for things like mailboxes.
 
Saloon Keeper
Posts: 7097
165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That one in discussed in the JavaMail FAQ: https://javaee.github.io/javamail/FAQ#installcert
 
Rancher
Posts: 157
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I've seen so many bad or even worse examples ... and some just plain wrong ... I can only imagine the lines that led to this Exception.
Recent Java versions have the required root certificates in their certificate store. On most Linux systems its a symlink to the mozilla-ca package instead of using its own root ca store. And as I don'T think someone at Google screwed up it's most likely the (probably bad) code.

Just post it ... I guess it suffers from using SMTP with TCP/465 - which is SMTPS.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




you can try in this way:
check smtp ports and packages

sample java program to send mail:
 
Sheriff
Posts: 26773
82
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
For what it's worth, I added code tags around your code so that it's more readable. However: there are some typing errors which will prevent it from compiling, and you have misspelled the names of the properties. Besides that it would be helpful if you explained how that code deals with the problem which the OP described.
 
Matthew Bendford
Rancher
Posts: 157
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paila Keerthana wrote:




you can try in this way:
check smtp ports and packages

sample java program to send mail:


I've requested a fix for your missing code-tags, but there's quite a few lines "not the way it should be done" ... and a few others are just wrong.

1) Aside from the way you authenticate, which can be done better, it wouldn't work without first setting up an application password for access your gmail account this way. By default, google only accept OAuth2 tokens, if one want to use regular passwords it has to be specified upfront in gmail settings.
2) You have missed the line where you add the actual message. This would result in gmail reject this message as spam. Yes, simple text/plain messages are ok, but only if they have meaningful subject and message body.
3) Your properites would fail as they're Uppercase - Java is case sensitive. Mail.something != mail.something.
4) I don'T really know why this lib was designed this way, but the static Transport.send() method is rather bad style. Some lines how to use this lib in a better way:
 
Paul Clapham
Sheriff
Posts: 26773
82
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
Also I notice that the code you posted ignores the "msg" parameter, i.e. it doesn't include it in the mail message that would be sent. And another thing, after correcting the spelling of the "Authenticator" class name:

The JavaMail FAQ wrote:There's really nothing wrong with using an Authenticator, it's just unnecessarily complex. A more straightforward approach is to call the connect method that takes a username and password when connecting to a Store. When sending a message, use the static Transport.send method that takes a username and password.



We appreciate all submissions here, but it would be helpful if you corrected the problems with this one. After all, copying and pasting code from the web and finding it doesn't work can be very annoying.
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic