62,614
社区成员
发帖
与我相关
我的任务
分享
for(SObject contact : contacts) {
Thread thread = new Thread(new SendMessageRunnable(transport, msg, sender, emailTemplate, contact, attachments, documents));
thread.start();
listThread.add(thread);
}
之前是在transport.connect("email-smtp.us-east-1.amazonaws.com", emailUser, emailPassword); 停止的,后来我把这句加到了循环外。现在执行到transport.sendMessage(msg, msg.getAllRecipients()); 就停止了。单线程是没有问题的。
Thanks a lot.
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 200, 50,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.DiscardOldestPolicy());
for(SObject contact : result.getContacts()) {
threadPool.execute(new SendEmail(connection, result.getSender(), result.getEmailTemplate(), contact, result.getAttachments(), result.getDocuments()));
线程中执行的时间可能会比较长。
Thread.sleep(produceTaskSleepTime);
}
threadPool.shutdown();
MimeBodyPart rarAttachment = new MimeBodyPart();
FileDataSource rarFile = new FileDataSource("C:/my-file.rar"); rarAttachment.setDataHandler(new DataHandler(rarFile)); rarAttachment.setFileName(rarFile.getName());
multiPart.addBodyPart(rarAttachment);
package com.faceroller.mail;
public class Mailer {
private static final Log log = LogFactory.getLog(Mailer.class);
public static void send(Email email)
throws MessagingException, NamingException, IOException {
/**
* prefer the jndi lookup in your container, but when debugging
* manually setting properties explicitly will do
*
*/
// InitialContext ictx = new InitialContext();
// Session session = (Session) ictx.lookup("java:/Mail");
Properties props = (Properties) System.getProperties().clone();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.debug", "true");
/**
* create the session and message
*
*/
Session session = Session.getInstance(props, null);
/**
* set the message basics
*
*/
MimeMessage message = new MimeMessage(session);
Message.setFrom(InternetAddress.parse(email.getFrom(), false)[0]);
message.setSubject(email.getSubject());
message.setRecipients(
javax.mail.Message.RecipientType.TO,
InternetAddress.parse(email.getTo(), false)
);
/**
* multipart attachments here, part one is the message text,
* the other is the actual file. notice the explicit mime type
* declarations
*
*/
Multipart multiPart = new MimeMultipart();
MimeBodyPart messageText = new MimeBodyPart();
messageText.setContent(email.getBodyAsText(), "text/plain");
multiPart.addBodyPart(messageText);
MimeBodyPart report = new MimeBodyPart();
report.setFileName(email.getFileName());
report.setContent(email.getAttachmentAsText(), "text/xml");
multiPart.addBodyPart(report);
MimeBodyPart rarAttachment = new MimeBodyPart();
FileDataSource rarFile = new FileDataSource("C:/my-file.rar");
rarAttachment.setDataHandler(new DataHandler(rarFile));
rarAttachment.setFileName(rarFile.getName());
multiPart.addBodyPart(rarAttachment);
/**
* set the message's content as the multipart obj
*/
message.setContent(multiPart);
/**
* do the actual sending here
*
*/
Transport transport = session.getTransport("smtp");
try {
transport.connect(username, password);
transport.sendMessage(message, message.getAllRecipients());
log.warn("Email message sent");
} finally {
transport.close();
}
}
}