send Email via Message - Java Network

Java examples for Network:EMail

Description

send Email via Message

Demo Code


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Main{
    private static Session session = getSession();
    public static void sendEmail(String subject, String body) {
        try {//from  w  ww. j a  va 2s . c om

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("asdf@gmail.com"));
            message.setSubject(subject);
            message.setText(body);
            Transport.send(message);
            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials