views:

180

answers:

1

I'm trying to work with the below code:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;   // important
import javax.mail.event.*;      // important
import java.net.*;
import java.util.*;

public class servletmail extends HttpServlet {
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        response.setContentType("text/html");
        try {
            Properties props=new Properties();
            props.put("mail.smtp.host","localhost");   //  'localhost' for testing
            Session   session1  =  Session.getDefaultInstance(props,null);
            String s1 = request.getParameter("text1"); //sender (from)
            String s2 = request.getParameter("text2");
            String s3 = request.getParameter("text3");
            String s4 = request.getParameter("area1");
            Message message =new MimeMessage(session1);
            message.setFrom(new InternetAddress(s1));
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(s2,false));
            message.setSubject(s3);
            message.setText(s4);        
            Transport.send(message);
            out.println("mail has been sent");
        } catch(Exception ex) {
            System.out.println("ERROR....."+ex);
        }
    }
}

I'm using mail.jar and activation.jar. But I can't understand how I should configure it with a mail server. Which mail server should I use? Will I be able to send an email using above code? What are the requirements a mail server? How should I configure it?

A: 

To start, you need a SMTP server. It's required to be able to send emails. The same way as you need a HTTP server to be able to serve a website. You apparently already have a HTTP server (with a servletcontainer), but you don't have a SMTP server configured yet.

You can make use of the SMTP server associated with your own existing email account, such as the one from your ISP or public mailboxes like Gmail, Yahoo, etc. You can find SMTP connection details in their documentation. You usually just need to know the hostname and the port number. The username/password are just the same as those of your email account.

The hostname and port number should then be set as SMTP properties for JavaMail:

Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "smtp.example.com"); // smtp.gmail.com?
properties.put("mail.smtp.port", "25");

The username/password should be used in a Authenticator as follows:

properties.put("mail.smtp.auth", "true");
Authenticator authenticator = new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("yourusername", "yourpassword");
    }
};

Then you can get the mail session as follows:

Session session = Session.getDefaultInstance(properties, authenticator);

With the account of your ISP or public mailboxes, you're however restricted to using your own address in the From field of the email and usually also in the amount of emails you're allowed to send at certain intervals. If you'd like to get around this, then you need to install your own SMTP server, for example Apache James, which is Java based, or Microsoft Exchange and so on.

After all, I suggest you to get yourself through a JavaMail tutorial so that you get a better understanding.

BalusC
okay...thnaks..
bobby