1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
   |  
package snec.models.audit.auditModel.treatment;
 
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.event.ConnectionListener;
import javax.mail.event.TransportListener;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class SendMail {
    /*------------------------------------------------
     * 		ATTRIBUTES
     --------------------------------------------------*/
    private String host;
    private String userName;
    private String userPassword;
    private Address fromAddr;
    private Address replyAddr;
    private String subject;
    private String content;
 
     /*------------------------------------------------
     * 		CONSTRUCTORS
     --------------------------------------------------*/
    public SendMail() throws MessagingException {
 
        this.host = "smtp.serveur.com";
        this.userName = "login";
        this.userPassword = "password";
 
        this.fromAddr = new InternetAddress("bap@bap.com");
        Address[] toAddr = {new InternetAddress("bap1@bap.com"), 
                            new InternetAddress("bap2@bap.com")};
 
        this.replyAddr = new InternetAddress("bap@bap.com");
 
        this.subject = "Alert";
        this.content = "First email throw JavaMail";
 
        Properties props = new Properties();
        Session session = Session.getInstance(props, null);
 
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(this.fromAddr);
        msg.setRecipients(Message.RecipientType.TO, toAddr);
        msg.setSubject(this.subject);
        msg.setContent(this.content, "text/plain");
 
        Transport transport = session.getTransport(toAddr[0]);
 
        //transport.addConnectionListener(this);
        //transport.addTransportListener(this);
        transport.connect(this.host, this.userName, this.userPassword);
        transport.sendMessage(msg, toAddr);
 
        transport.close();
    }
} | 
Partager