Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure a mail server for use with JavaMail?

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?

like image 808
simplyblue Avatar asked Jun 02 '10 11:06

simplyblue


People also ask

How can I get SMTP server response using JavaMail?

SMTPTransport t = (SMTPTransport)session. getTransport("smtps"); t. send(message); String response = t. getLastServerResponse(); boolean s = t.

How do I send an email to multiple recipients using JavaMail?

addRecipient(Message.RecipientType.CC, "[email protected],[email protected],[email protected]"); Or message. addRecipient(Message.RecipientType.CC, "[email protected];[email protected];[email protected]"); I can send a message using alternate methods too, but I want to know the purpose of the above method.

Which protocol is used to receive the messages in JavaMail?

SMTP– Simple Mail Transfer Protocol is used for sending e-mails. POP3– Post Office Protocol is used to receive e-mails. It provides one to one mapping for users and mail boxes, which is one mail box for one user. IMAP– Internet Message Access Protocol is also used to receive e-mails.


1 Answers

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.

like image 123
BalusC Avatar answered Sep 23 '22 07:09

BalusC