• 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

JavaMail Code Suggestions

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same topic has been posted on StackOverFlow. Need inputs from fellow Code Ranchers too.
http://stackoverflow.com/questions/25545226/javamail-code-suggestions

I am writing a Bean which can send same message to multiple user or can send different message to same user. My application is REST based and an email needs to be triggered when a service is hit.Application can send any number of emails in a day. I have written below class for the same.Any improvements that I can do in below code. I fetch values for properties like from ,host,etc from my Db.I am querying the Db only once and populating javax.mail. Session object with the same.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kinds of suggestions are you looking for? What is not working as you would like it to?
 
Ankur Luthra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to know if I can implement the above code in a more elegant way.
Any re-factoring that can be done.

Should I be making the class a singleton?
Most of the variables in this class will remain constant except TO-Address ,Subject and Body.
Any other way by which I can make sure that all variables except (TO-Address ,Subject and Body) gets instantiated only once.

Regards,
Ankur
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How you intend to use this class determines its design to a good deal - we can't really comment on that.

I guess my question would be why you're not using a library like http://commons.apache.org/proper/commons-email/ for this?
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
agree with Ulf, why to reinvent the wheel.

for code re factoring, one quick thing is to refactor sendEmailToUserList and sendEmailToUser should be refactored since they have 90% similar code. same goes with createEmailMessage and createBulkEmailMessage
 
Ankur Luthra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am facing some issue while using commons-mail API.

Getting following Exception .
Caused by: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
at javax.mail.Session.getProvider(Session.java:237)
at javax.mail.Session.getTransport(Session.java:346)
at javax.mail.Session.getTransport(Session.java:376)
at javax.mail.Transport.send(Transport.java:67)
at javax.mail.Transport.send(Transport.java:48)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
... 3 more


Below is snippet of Code that I am using


 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Commons Email is built on top of JavaMail, so it needs all its libraries in the classpath - it seems that's not the case.
 
Ankur Luthra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have added mail-1.4.jar in my class path and it works.
Is there any issue if i keep geronimo-javamail_1.4_spec-1.7.1.jar and geronimo-activation_1.1_spec-1.1.jar also in my classpath?

Regards,
Ankur

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what those are. Check what classes they contain, and make sure not to have overlaps with other jar files.
 
Ankur Luthra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is geronimo*activation*.jar contains the package javax.activation and I see javax.activation as a dependency for commons-email.
hence the question.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JAF (a.k.a Java Activation Framework) can be downloaded from Oracle if you want/need the original jar file.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic