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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
package jMail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
/**
*
* @author Agraj
*/
public class Mail {
private String to;
private String from;
private String message;
private String subject;
private String smtpServ;
private String joindre;
public Mail(String to, String from, String message, String subject, String smtpServ, String joindre) {
this.to = to;
this.from = from;
this.message = message;
this.subject = subject;
this.smtpServ = smtpServ;
this.joindre = joindre;
}
public String getJoindre() {
return joindre;
}
public void setJoindre(String joindre) {
this.joindre = joindre;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getSmtpServ() {
return smtpServ;
}
public void setSmtpServ(String smtpServ) {
this.smtpServ = smtpServ;
}
public int sendMail(){
try
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host",smtpServ);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props,auth);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse("s.sayahi@hotmail.com", false));
msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("sayahisalem@live.fr", false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(message);
// -- Set some other header information --
msg.setHeader("MyMail", "Java Mail Test");
msg.setSentDate(new Date());
msg.setFrom(new InternetAddress(from));
InternetAddress ADRESSE[]={new InternetAddress("s.sayahi@hotmail.com"),new InternetAddress("sayahisalem@live.fr")};
msg.addFrom(ADRESSE);
// msg.setFileName(to)
Multipart multipart = new MimeMultipart();
// creation partie principale du message
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
multipart.addBodyPart(messageBodyPart);
// creation et ajout de la piece jointe
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("C:\\CientMail.java");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("C:\\CientMail.java");
// BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart.setText(message);
multipart.addBodyPart(messageBodyPart);
// ajout des ?l?ments au mail
msg.setContent(multipart);
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent to"+to+" OK.");
return 0;
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Exception "+ex);
return -1;
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "user@mail.com";
String password = "*******";
return new PasswordAuthentication(username, password);
}
}
} |
Partager