Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Mail

 
pavan rck
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Okeke
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..............
 
Paul Clapham
Sheriff
Posts: 21291
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic