Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaMail and Server

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to try the JavaMail and need guidance. According to what I have read, I have to connect to a "server" to send mails/messages.
I want to use the JavaMail using my PC at home. Do I have to download a "server"? Which one should I download? Do I have to configure the "server"? Thanks.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you basically need an SMTP server in order to send emails. When I was playing with java mail, I used to use www.mycgiserver.com to host my jsp's and servlets. They also provide the SMTP server you need (you need an account in this site, of course).
Another solution is apache James, which is a java SMTP and POP3 Mail server. You will have to configure it though, but it shouldn't be difficult at all.
good luck
[ August 22, 2003: Message edited by: Andres Gonzalez ]
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your guidance. I have signed up at the http://www.mycgiserver.com and noticed that the site provides SMTP server. How do I connect to the SMTP server so that I can use JavaMail in my PC at home?
[ August 22, 2003: Message edited by: JiaPei Jen ]
 
Frank Chen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you set up your account with the smtp server provider, all you need to do is to put the host name in your code, something like this:
//set SMTP host
String mailHost = "your_smtp.server";

//Specify the desired SMTP server
Properties props = System.getProperties();
props.put("mail.smtp.host", mailHost);
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the very first exercis given by the Sun online training. The file was msgsend.java bundled in the javamail-1.3.1.zip. I followed the instrucitons given at http://developer.java.sun.com/developer/onlineTraining/JavaMail/exercises/MailSetup/help.html
1. downloaded the latest versions of javamail api; i.e. javamail-1_3.1.zip
2. downloaded the java activation framework jaf-1_0_2.zip
3. unzipped both the files and placed the activation.jar and mail.jar
in C:\j2sdk1.4.1_02\jre\lib\ext
4. then C:\javamail-1.3.1\demo>javac msgsend.java (compilation was
successful)
5. then tried running the program
C:\javamail-1.3.1\demo>java msgsend -o jiapei_jen@yahoo.com -M
SMTP.mail.yahoo.com abcxyz@yahoo.com
I got
Exception in thread "main" java.lang.NoClassDefFoundError:
javax/mail/Message
I do not know what went wrong. I thought that I was supposed to be asked to give a Subject.
AND
please tolerate my simple minded question:
I have registered at http://www.mycgiserver.com
that site has the SMTP server.
I am working using my PC at home. How does my machine access the SMTP server at the mycgiserver.com so that I can use JavaMail to send mails/messages? :roll:
[ August 22, 2003: Message edited by: JiaPei Jen ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working using my PC at home. How does my machine access the SMTP server at the mycgiserver.com so that I can use JavaMail to send mails/messages?
2 things:
1o) if you have your servlets/jsp at home and you want to use mycgiserver's SMTP, you can't. This is because of security issues. Can you imagine everyone using the SMTP server to send emails, that'd be a target for spam. In order to use this SMTP server you have to host your servlets/jsp in this site. AFAIK, they give you 5 M for free.
2o) if you host your files in mycgiserver.com, this is a dirty way to do it from a jsp:

<%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
%>
<%
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;

sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host", "mycgiserver.com");
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("anymail@anymail.com")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("anotherEmail@anotherEmail.com")));
newMessage.setSubject(request.getParameter("your subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("mail content"));
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>

If for any reasons you still want to host your pages in your pc at home, then I'd recommend again apache James (it's free).
Also, have a look at mailer taglib, you will see the difference of doing it using tag libs rather than coding it with the example I showed you.
good luck
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic