The Program to Send E-Mail [J2EE] - Java Network

Java examples for Network:EMail

Description

The Program to Send E-Mail [J2EE]

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

public class SendApp {
    public static void send(String smtpHost, int smtpPort,
                            String from, String to,
                            String subject, String content)
            throws AddressException, MessagingException {

        java.util.Properties props = new java.util.Properties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", ""+smtpPort);
        Session session = Session.getDefaultInstance(props, null);

        // Construct the message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setSubject(subject);
        msg.setText(content);

        // Send the message
        Transport.send(msg);
    }

    public static void main(String[] args) throws Exception {
        send("hostname", 25, "joe@java2s.com", "sue@java2s.com","title", "How about at 7?");
    }
}

Related Tutorials