|
import java.io.*;
|
|
import java.util.Properties;
|
|
import javax.activation.DataHandler;
|
|
import javax.activation.DataSource;
|
|
import javax.activation.FileDataSource;
|
|
import javax.mail.*;
|
|
import javax.mail.internet.*;
|
|
import jxl.Cell;
|
|
import jxl.Sheet;
|
|
import jxl.Workbook;
|
|
import jxl.WorkbookSettings;
|
|
import jxl.read.biff.BiffException;
|
|
|
|
public class Mail {
|
|
|
|
/*
|
|
* Mail server connection variables
|
|
*/
|
|
private static final String SMTP_HOST_NAME = "my_smtp_hostname";
|
|
private static final String SMTP_AUTH_USER = "smtp_user";
|
|
private static final String SMTP_AUTH_PWD = "smtp_user_password";
|
|
|
|
/*
|
|
* Main
|
|
*/
|
|
public static void main(String[] args) throws Exception {
|
|
new Mail().test();
|
|
}
|
|
|
|
/*
|
|
* test() creation
|
|
*/
|
|
public void test() throws Exception {
|
|
|
|
Properties props = new Properties();
|
|
props.put("mail.transport.protocol", "smtp");
|
|
props.put("mail.smtp.host", SMTP_HOST_NAME);
|
|
props.put("mail.smtp.auth", "true");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
* Opening the excel sheet
|
|
*/
|
|
|
|
Workbook wb = null;
|
|
Cell wbCell[][] = new Cell[15][6965];
|
|
String wbString[][] = new String[15][6965];
|
|
|
|
|
|
/*
|
|
* Initializing reading loop variables
|
|
*/
|
|
|
|
int i, j, k;
|
|
i = 1;
|
|
j = 1;
|
|
k = 1;
|
|
|
|
/*
|
|
* Initializing supervising loop variables
|
|
*/
|
|
int total, success, failure;
|
|
total = 0;
|
|
success = 0;
|
|
failure = 0;
|
|
|
|
|
|
/*
|
|
* Starting loop
|
|
*/
|
|
|
|
try {
|
|
/*
|
|
* Reading Excel file
|
|
*/
|
|
WorkbookSettings ws = new WorkbookSettings();
|
|
ws.setEncoding("ISO-8859-1");
|
|
wb = Workbook.getWorkbook(new File("path/to/file.xls"), ws);
|
|
|
|
/*
|
|
* Accessing the sheet I want
|
|
*/
|
|
|
|
Sheet wbSheet = wb.getSheet(0);
|
|
|
|
/*
|
|
* Creating loop
|
|
*/
|
|
|
|
/*
|
|
* for (j = 1; j <= 5300; j++)
|
|
* j = 1 if you have column titles, 0 if not until last line number
|
|
*/
|
|
|
|
for (j = 1; j <= 5300; j++) {
|
|
|
|
/*
|
|
* I save my data in a table call wbString
|
|
*/
|
|
|
|
for (i = 0; i <= 14; i++) {
|
|
wbCell[i][j] = wbSheet.getCell(i, j);
|
|
wbString[i][j] = wbCell[i][j].getContents();
|
|
}
|
|
|
|
|
|
total = total + 1;
|
|
for (k = 1; k <= 16; k++) {
|
|
CHEMINCell[k][j] = CHEMINSheet.getCell(k, j);
|
|
CHEMINString[k][j] = CHEMINCell[k][j].getContents();
|
|
}
|
|
|
|
try {
|
|
/*
|
|
* Creating mail associated to the line I just read
|
|
*/
|
|
|
|
|
|
/*
|
|
* Create the body of the mail with the Cell you read
|
|
* Lot is custom field
|
|
*/
|
|
|
|
String body = "description\n " + wbString[1][j] + "\nproject: " + wbString[2][j] + ... + "\nLot: " + wbString[3][j]";
|
|
boolean debug = true;
|
|
boolean result = false;
|
|
try {
|
|
Authenticator auth = new SMTPAuthenticator();
|
|
Session mailSession = Session.getDefaultInstance(props, auth);
|
|
// uncomment for debugging infos to stdout
|
|
// mailSession.setDebug(true);
|
|
Transport transport = mailSession.getTransport();
|
|
transport.connect();
|
|
|
|
|
|
MimeMessage message = new MimeMessage(mailSession);
|
|
|
|
message.setFrom(new InternetAddress("e-mail@domain.com"));
|
|
message.setSubject(wbString[0][j]);
|
|
message.addRecipient(Message.RecipientType.TO,
|
|
new InternetAddress("e-mail@domain.com"));
|
|
mailSession.setDebug(debug);
|
|
|
|
MimeBodyPart mbpText = new MimeBodyPart();
|
|
mbpText.setText(body);
|
|
|
|
MimeMultipart mp = new MimeMultipart();
|
|
mp.addBodyPart(mbpText);
|
|
|
|
|
|
/*
|
|
* Create first attachment
|
|
*/
|
|
if (new File(wbString[4][j]).exists() == true) {
|
|
MimeBodyPart mbpAttachment1 = new MimeBodyPart();
|
|
DataSource source1 = new FileDataSource(SIGMAString[13][j]);
|
|
mbpAttachment1.setDataHandler(new DataHandler(source1));
|
|
mbpAttachment1.setFileName(SIGMAString[13][j]);
|
|
mp.addBodyPart(mbpAttachment1);
|
|
}
|
|
|
|
/*
|
|
*Create second attachment
|
|
*/
|
|
if (new File(wbString[5][j]).exists() == true) {
|
|
MimeBodyPart mbpAttachment2 = new MimeBodyPart();
|
|
DataSource source2 = new FileDataSource(CHEMINString[1][j]);
|
|
mbpAttachment2.setDataHandler(new DataHandler(source2));
|
|
mbpAttachment2.setFileName(CHEMINString[1][j]);
|
|
mp.addBodyPart(mbpAttachment2);
|
|
}
|
|
|
|
...
|
|
|
|
|
|
message.setContent(mp);
|
|
|
|
|
|
transport.sendMessage(message,
|
|
message.getRecipients(Message.RecipientType.TO));
|
|
transport.close();
|
|
result = true;
|
|
} catch (AddressException e) {
|
|
e.printStackTrace();
|
|
} catch (MessagingException e) {
|
|
e.printStackTrace();
|
|
}
|
|
if (result == true) {
|
|
success = success + 1;
|
|
} else {
|
|
failure = failure + 1;
|
|
}
|
|
}
|
|
} catch (BiffException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (wb != null) {
|
|
wb.close();
|
|
}
|
|
}
|
|
|
|
System.out.println(total + " mails with " + success + " success and " + failure + " failure");
|
|
}
|
|
|
|
private class SMTPAuthenticator extends javax.mail.Authenticator {
|
|
|
|
public PasswordAuthentication getPasswordAuthentication() {
|
|
String username = SMTP_AUTH_USER;
|
|
String password = SMTP_AUTH_PWD;
|
|
return new PasswordAuthentication(username, password);
|
|
}
|
|
}
|
|
}
|