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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.net.*;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* Envoyer un email
*/
public class EnvoiMail {
private Session session = null;
private Transport transport = null;
/**
* Fixer les propriétés
*/
public void connect(String host, String user, String password) throws NoSuchProviderException, MessagingException {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", host);
//properties.setProperty("mail.smtp.port", "25");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.user", user);
//properties.setProperty("mail.smtp.starttls.enable", "true");
this.session = Session.getDefaultInstance(properties, null);
this.transport = this.session.getTransport("smtp");
this.transport.connect(host, user, password);
}
public void send(String from, String to, String subject, String body) throws MessagingException {
MimeMessage message = new MimeMessage(this.session);
message.setSubject(subject);
message.setContent(body, "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setFrom(new InternetAddress(from));
this.transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
this.transport.close();
}
public static void main(String args[]) {
System.out.println("Taper mail valide :");
try {
mail=mail_to.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Taper sujet :");
try {
sujet=buffer_sujet.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Taper un message :");
try {
mes=buffer_corp.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
EnvoiMail email = new EnvoiMail();
email.connect("localhost", "user", "mdp");
email.send("user@mondomaine.com",mail, sujet, mes);
} catch (MessagingException ex) {
Logger.getLogger(EnvoiMail.class.getName()).log(Level.SEVERE, null, ex);
}
}
//static BufferedReader host = new BufferedReader(new InputStreamReader(System.in));
//static BufferedReader buffernom = new BufferedReader(new InputStreamReader(System.in));
static BufferedReader buffer_corp = new BufferedReader(new InputStreamReader(System.in));
static BufferedReader mail_from = new BufferedReader(new InputStreamReader(System.in));
static BufferedReader mail_to = new BufferedReader(new InputStreamReader(System.in));
static BufferedReader buffer_sujet = new BufferedReader(new InputStreamReader(System.in));
static String mes;
static String mail;
static String sujet;
} |
Partager