BenSkeleton BenSkeleton - 10 months ago 80
Java Question

java sending email with activation link

I think I have a quite simple question. I'm working on a web app with java and glassfish server where users can register and after they do, I want to send them an email with an activation link.

Is it possible to send mails with the java mail API without an external smtp server?

Since it is not necessary for users to answer to that mail. It seems I'm lacking basic knowledge of how sending emails works. I just want to invent some sender adress like "". Its obvious to me, that I would need a mail server for that domain so that one could send a message TO that adress. But if I'm just sending a mail FROM that adress, why can't I just invent the adress?

I don't want to use an external service like google or yahoo. If it's not possible can you suggest me an open source mail server that goes along with glassfish? I mean, is it possible to use glassfish as an email server? If not, what else could I use?

Thank you!

Answer Source

Yes you can do that. Simply use the javax mail library.

If you use Maven, you'd do something like this


Then you could do something like this

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail
   public static void main(String [] args)
      // Recipient's email ID needs to be mentioned.
      String to = "";
      String from = "";

      Properties properties = System.getProperties();
      properties.setProperty("", "localhost");
      Session session = Session.getDefaultInstance(properties);

         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(from));
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

         message.setSubject("Registration from me :)");
         message.setText("You got yourself an account. congrats");

         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {