Question

Possible Duplicate:
How do you send email from a Java app using Gmail?

How do I send an SMTP Message from Java?

Was it helpful?

Solution

Here's an example for Gmail smtp:

import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;

import javax.mail.*;

import javax.mail.internet.*;

import com.sun.mail.smtp.*;


public class Distribution {

    public static void main(String args[]) throws Exception {
        Properties props = System.getProperties();
        props.put("mail.smtps.host","smtp.gmail.com");
        props.put("mail.smtps.auth","true");
        Session session = Session.getInstance(props, null);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("mail@tovare.com"));;
        msg.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse("tov.are.jacobsen@iss.no", false));
        msg.setSubject("Heisann "+System.currentTimeMillis());
        msg.setText("Med vennlig hilsennTov Are Jacobsen");
        msg.setHeader("X-Mailer", "Tov Are's program");
        msg.setSentDate(new Date());
        SMTPTransport t =
            (SMTPTransport)session.getTransport("smtps");
        t.connect("smtp.gmail.com", "admin@tovare.com", "<insert password here>");
        t.sendMessage(msg, msg.getAllRecipients());
        System.out.println("Response: " + t.getLastServerResponse());
        t.close();
    }
}

Now, do it this way only if you would like to keep your project dependencies to a minimum, otherwise i can warmly recommend using classes from apache

http://commons.apache.org/email/

Regards

Tov Are Jacobsen

OTHER TIPS

Another way is to use aspirin (https://github.com/masukomi/aspirin) like this:

MailQue.queMail(MimeMessage message)

..after having constructed your mimemessage as above.

Aspirin is an smtp 'server' so you don't have to configure it. But note that sending email to a broad set of recipients isnt as simple as it appears because of the many different spam filtering rules receiving mail servers and client applications apply.

Please see this post

How can I send an email by Java application using GMail, Yahoo, or Hotmail?

It is specific to gmail but you can substitute your smtp credentials.

See the JavaMail API and associated javadocs.

See the following tutorial at Java Practices.

http://www.javapractices.com/topic/TopicAction.do?Id=144

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

public void postMail(String recipients[], String subject,
    String message , String from) throws MessagingException {

    //Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.jcom.net");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(false);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
    for (int i = 0; i < recipients.length; i++) {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top