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

javax.mail servet/client issue

 
liliya woland
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My email.java file works only on the server and not on the client side. The script gives no errors either; and doesn't work at all when try using "cc" mailing option, but this could be a separate issue.
Can anyone tell me what's happening?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without seeing a bit of relevant code, it's hard to tell what might be going on. Are you sure there isn't an exception somewhere that is inadvertently caught and not logged?
 
liliya woland
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use Throwable, so if there is an error, it would be caught.
I'm including the code producing the message... Please let me know if you see something strange there.

try {


//Get system properties
Properties props = System.getProperties();

//Specify the desired SMTP server
props.put("mail.smtp.host", mailHost);

// create a new Session object
Session session = Session.getInstance(props);

Message message = new MimeMessage(session);
Multipart Container = new MimeMultipart();

BodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(new FileDataSource(new File(filepath))));
attachment.setFileName(filename);
String header = determineHeader(filename);
attachment.addHeader("Content-Type",header);
attachment.setDisposition("attachment");
Container.addBodyPart(attachment);


/**** text body of email ****/
BodyPart htmlText = new MimeBodyPart();
htmlText.setContent(body, "text/plain");
htmlText.setDisposition("inline");
Container.addBodyPart(htmlText);

message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, new InternetAddress[] {
new InternetAddress(to) });

if (!cc.equals("")){
InternetAddress[] addresses = {new InternetAddress(cc) };
message.setRecipients(Message.RecipientType.CC, addresses);
}
message.setSubject(subject);
message.setContent(Container);
Transport.send(message);

// it worked!
writer.println("<b>message to " + to + " was successfully sent.</b>");

} catch (Throwable t) {
writer.println("<b>Unable to send message: <br><pre>");
t.printStackTrace(writer);
writer.println("</pre></b>");
}
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what happens when you run this? It just prints "message was sent" even if the message wasn't sent? Or does it send the message, but not to the CC, and then print the message?
 
Sarath Mohan
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get 'it is not working in client side'

May be it could not be able to locate mail servers.

Apache James is good starting point for dealing with Mailing applications. It provides a free SMTP, POP server and you can test stand alone mailing apps.
 
liliya woland
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the code, no error happens. I test it in Firefox on the server and option "to" works fine alone, but if I try "cc" (with "to"), none of them work.
And if I test the code in IE - on a client machine, I can see the code pulling the data out (as long as I don't send an attachement - then I get an IO exception), says the message is sent - no errors, but option "to" doesn't work, and "cc" doesn't work...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic