It's not a secret anymore!
The moose likes Other JSE/JEE APIs and the fly likes Java Mail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail" Watch "Java Mail" New topic

Java Mail

pavan rck

Joined: Mar 10, 2009
Posts: 2
public synchronized static boolean sendMail(String userName,String passWord,String host,String port,String starttls,String auth,boolean debug,String socketFactoryClass,String fallback,String[] to,String[] cc,String[] bcc,String subject,String text){

Properties props = new Properties();
//Properties props=System.getProperties();
props.put("mail.smtp.user", userName);
props.put("", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", auth);
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.debug", "false");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.fallback", fallback);
Session session = Session.getDefaultInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(""));
for(int i=0;i<to.length;i++){
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));

/*for(int i=0;i<cc.length;i++){
msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));

for(int i=0;i<bcc.length;i++){
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent("<h1>Check attachment</h1>", "html/text");

MimeBodyPart attachFilePart = new MimeBodyPart();
FileDataSource fds =
new FileDataSource("C:\\ITextTest.pdf");
attachFilePart.setDataHandler(new DataHandler(fds));

Multipart mp = new MimeMultipart();
// mp.addBodyPart(textPart);

Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
return true;
catch (Exception mex)
return false;

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

You don't say what, if any, the problem is but 14 parameters to a static method is just silly. This needs to be OO implemented as a class with a constructor accepting the first 4 parameters and then methods for everything else.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Paul Okeke
Ranch Hand

Joined: May 16, 2009
Posts: 58
Please Pavan I have actually gotten some books on Java Mail but the problem remains that I have not be able
to get and configure the mail service which i will use to test what I read. I downloaded Apache James for this but dose
not know how to configure this for use.

Please can you or any other person help me to start Up? Thanks..............

OCJP 1.6
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Paul Okeke, you are on the wrong track. (Not to mention that you should have started your own thread to ask your question instead of hijacking this one.) JAMES is an SMTP server. It has nothing to do with JavaMail, which is an SMTP client. You're better off trying to connect JavaMail with an existing SMTP server, one which a competent professional has already set up. It isn't hard to use Gmail's server, for example. But don't waste your time trying to set up your own server when you know almost nothing about how e-mail works.
pavan rck

Joined: Mar 10, 2009
Posts: 2
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15100

pavan, welcome to JavaRanch.

Please UseCodeTags when you post source code, so that the code is easier to read.

Also, please explain what your question or problem is.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Java Mail
It's not a secret anymore!