package com.heartjack.db.batch.mail;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.heartjack.mvc.base.HjBaseException;
import com.heartjack.mvc.helper.HtmlReadHelper;
import com.heartjack.mvc.helper.ReadWriteFile;
public class SendInlineImagesInEmail {
public static void main(String[] args) throws IOException, InterruptedException {
SendInlineImagesInEmail client = new SendInlineImagesInEmail();
List<String> emails = getEmails();
List<String> fails = new ArrayList<String>();
int count = 0;
for (String to : emails) {
client.SendEmail(getParentPathFile(), to, fails);
count++;
Thread.sleep(1000);
}
try {
ReadWriteFile.writeTxtFile(getParentPathFile().toString()+ "/hjMVCBatch/mail", "fail", fails.toString());
} catch (HjBaseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static File getParentPathFile() {
File f = new File(".");
File parentPathFile = f.getAbsoluteFile().getParentFile();
return parentPathFile;
}
public static List<String> getEmails() {
List<String> emails = HtmlReadHelper.readMailList(
new File( getParentPathFile(), "hjMVCBatch/mail"), "to");
return emails;
}
public void SendEmail(File parentPathFile, String to, List<String> fails) {
// Recipient's email ID needs to be mentioned.
// String to = "xxx";
// Sender's email ID needs to be mentioned
String from = "xxx";
final String username = "xxx";//change accordingly
final String password = "xxx";//change accordingly
// Assuming you are sending email through smtp.pigai.me
String host = "smtp.shenqing.me";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("完全免费的申请神器?高效又使用的写作帮手?你没有看错,这都不是梦!");
// This mail has 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
//从文件读取编辑内容
String info = HtmlReadHelper.readhtml(new File(parentPathFile, "hjMVCBatch/html") ,"mail2");
String htmlText = info + "<img src=\"cid:college\">"+ "<br/>" + "<img src=\"cid:writing\">";
messageBodyPart.setContent(htmlText, "text/html;charset=UTF-8");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource college = new FileDataSource("hjMVCBatch/images/college.png");
messageBodyPart.setDataHandler(new DataHandler(college));
messageBodyPart.setHeader("Content-ID", "<college>");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource writing = new FileDataSource("hjMVCBatch/images/writing.jpg");
messageBodyPart.setDataHandler(new DataHandler(writing));
messageBodyPart.setHeader("Content-ID", "<writing>");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
// Send message
message.saveChanges();
Transport.send(message);
session = null;
message = null;
college = null;
writing = null;
System.out.println(to + " Sent");
} catch (MessagingException e) {
byte[] bytes = e.getMessage().getBytes();
try {
String msg = new String(bytes,"ISO8859-1");
System.out.println(to + " (" + msg + ")");
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
fails.add(to + " (" + e.getMessage() + ")\r\n");
// throw new RuntimeException(e);
}
}
}
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.heartjack.mvc.base.HjBaseException;
import com.heartjack.mvc.helper.HtmlReadHelper;
import com.heartjack.mvc.helper.ReadWriteFile;
public class SendInlineImagesInEmail {
public static void main(String[] args) throws IOException, InterruptedException {
SendInlineImagesInEmail client = new SendInlineImagesInEmail();
List<String> emails = getEmails();
List<String> fails = new ArrayList<String>();
int count = 0;
for (String to : emails) {
client.SendEmail(getParentPathFile(), to, fails);
count++;
Thread.sleep(1000);
}
try {
ReadWriteFile.writeTxtFile(getParentPathFile().toString()+ "/hjMVCBatch/mail", "fail", fails.toString());
} catch (HjBaseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static File getParentPathFile() {
File f = new File(".");
File parentPathFile = f.getAbsoluteFile().getParentFile();
return parentPathFile;
}
public static List<String> getEmails() {
List<String> emails = HtmlReadHelper.readMailList(
new File( getParentPathFile(), "hjMVCBatch/mail"), "to");
return emails;
}
public void SendEmail(File parentPathFile, String to, List<String> fails) {
// Recipient's email ID needs to be mentioned.
// String to = "xxx";
// Sender's email ID needs to be mentioned
String from = "xxx";
final String username = "xxx";//change accordingly
final String password = "xxx";//change accordingly
// Assuming you are sending email through smtp.pigai.me
String host = "smtp.shenqing.me";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("完全免费的申请神器?高效又使用的写作帮手?你没有看错,这都不是梦!");
// This mail has 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
//从文件读取编辑内容
String info = HtmlReadHelper.readhtml(new File(parentPathFile, "hjMVCBatch/html") ,"mail2");
String htmlText = info + "<img src=\"cid:college\">"+ "<br/>" + "<img src=\"cid:writing\">";
messageBodyPart.setContent(htmlText, "text/html;charset=UTF-8");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource college = new FileDataSource("hjMVCBatch/images/college.png");
messageBodyPart.setDataHandler(new DataHandler(college));
messageBodyPart.setHeader("Content-ID", "<college>");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource writing = new FileDataSource("hjMVCBatch/images/writing.jpg");
messageBodyPart.setDataHandler(new DataHandler(writing));
messageBodyPart.setHeader("Content-ID", "<writing>");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
// Send message
message.saveChanges();
Transport.send(message);
session = null;
message = null;
college = null;
writing = null;
System.out.println(to + " Sent");
} catch (MessagingException e) {
byte[] bytes = e.getMessage().getBytes();
try {
String msg = new String(bytes,"ISO8859-1");
System.out.println(to + " (" + msg + ")");
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
fails.add(to + " (" + e.getMessage() + ")\r\n");
// throw new RuntimeException(e);
}
}
}