aspose file tools*
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
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail" Watch "Java Mail" New topic
Author

Java Mail

pavan rck
Greenhorn

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("mail.smtp.host", host);
if(!"".equals(port))
props.put("mail.smtp.port", port);
if(!"".equals(starttls))
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
if(debug){
props.put("mail.smtp.debug", "true");
}else{
props.put("mail.smtp.debug", "false");
}
if(!"".equals(port))
props.put("mail.smtp.socketFactory.port", port);
if(!"".equals(socketFactoryClass))
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
if(!"".equals(fallback))
props.put("mail.smtp.socketFactory.fallback", fallback);
try
{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("pavanrck@gmail.com"));
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]));
}*/
msg.saveChanges();
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));
attachFilePart.setFileName(fds.getName());

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

msg.setContent(mp);
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
}
catch (Exception mex)
{
mex.printStackTrace();
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
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

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
Greenhorn

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

Joined: Aug 16, 2005
Posts: 14435
    
  23

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
 
 
subject: Java Mail