Friday, May 6, 2011

Sending email in Java?

Can anyone suggest a library for sending emails in Java?

From stackoverflow
  • JavaMail API

  • Spring has a mail wrapper layer as well:

  • Try Commons Mail. This builds on the Java Mail API but makes it much more simple to use.

    : hey will common mail with work with gmail smtp server?
    Valentin Rocher : It works with any smtp server, just like any mail client.
  • You may also want to take a look at the Apache Commons Email library. It is featureful and easy to use.

    You could do something along the lines of:

    import org.apache.commons.mail.SimpleEmail;
    String[] recipients = {"", ""};
    SimpleEmail email = new SimpleEmail();
    for (int i = 0; i < recipients.length; i++)
    email.setFrom("", "Me");
    email.setSubject("Test message");
    email.setMsg("This is a simple test of commons-email");

    The sample code is taken from the Commons Email example page, modified to show adding multiple recipients. Hope that helps.

    : hey will this with work with gmail smtp server?
    Thorbjørn Ravn Andersen : you need to authenticate properly, but when that is done, you can use it with gmail


Post a Comment