本文整理匯總了Java中javax.mail.Multipart.addBodyPart方法的典型用法代碼示例。如果您正苦於以下問題:Java Multipart.addBodyPart方法的具體用法?Java Multipart.addBodyPart怎麽用?Java Multipart.addBodyPart使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.mail.Multipart
的用法示例。
在下文中一共展示了Multipart.addBodyPart方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getMessagePart
import javax.mail.Multipart; //導入方法依賴的package包/類
private static Multipart getMessagePart() throws MessagingException, IOException {
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(getVal("msg.Body"));
multipart.addBodyPart(messageBodyPart);
if (getBoolVal("attach.reports")) {
LOG.info("Attaching Reports as zip");
multipart.addBodyPart(getReportsBodyPart());
} else {
if (getBoolVal("attach.standaloneHtml")) {
multipart.addBodyPart(getStandaloneHtmlBodyPart());
}
if (getBoolVal("attach.console")) {
multipart.addBodyPart(getConsoleBodyPart());
}
if (getBoolVal("attach.screenshots")) {
multipart.addBodyPart(getScreenShotsBodyPart());
}
}
messageBodyPart.setContent(getVal("msg.Body")
.concat("\n\n\n")
.concat(MailComponent.getHTMLBody()), "text/html");
return multipart;
}
示例2: buildMessage
import javax.mail.Multipart; //導入方法依賴的package包/類
private static Message buildMessage(Session session, String from, String recipients, String subject, String text, String filename) throws MessagingException, AddressException
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
message.setSubject(subject);
BodyPart messageTextPart = new MimeBodyPart();
messageTextPart.setText(text);
BodyPart messageAttachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(new File(filename));
messageAttachmentPart.setDataHandler(new DataHandler(source));
messageAttachmentPart.setFileName(filename);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageTextPart);
multipart.addBodyPart(messageAttachmentPart);
message.setContent(multipart);
return message;
}
示例3: submitFaxJobValidTest
import javax.mail.Multipart; //導入方法依賴的package包/類
/**
* Test
*
* @throws Exception
* Any exception
*/
@Test
public void submitFaxJobValidTest() throws Exception
{
Message message=new MimeMessage((Session)null);
message.setSubject("fax:123456789");
message.setFrom(new InternetAddress("[email protected]"));
File file=File.createTempFile("temp_",".txt");
file.deleteOnExit();
IOHelper.writeTextFile("abc",file);
DataSource source=new FileDataSource(file);
BodyPart messageFileAttachmentBodyPart=new MimeBodyPart();
messageFileAttachmentBodyPart.setDataHandler(new DataHandler(source));
messageFileAttachmentBodyPart.setFileName(file.getName());
Multipart multipart=new MimeMultipart();
multipart.addBodyPart(messageFileAttachmentBodyPart);
message.setContent(multipart);
FaxJob faxJob=this.faxBridge.submitFaxJob(message);
Assert.assertNotNull(faxJob);
Assert.assertNotNull(faxJob.getFile());
file.delete();
}
示例4: sendAttachMail
import javax.mail.Multipart; //導入方法依賴的package包/類
public boolean sendAttachMail(MailSenderInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
try {
Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
Multipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
multi.addBodyPart(textBodyPart);
for (String path : mailInfo.getAttachFileNames()) {
DataSource fds = new FileDataSource(path);
BodyPart fileBodyPart = new MimeBodyPart();
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(path.substring(path.lastIndexOf("/") + 1));
multi.addBodyPart(fileBodyPart);
}
mailMessage.setContent(multi);
mailMessage.saveChanges();
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
return false;
}
}
示例5: buildEmailMessage
import javax.mail.Multipart; //導入方法依賴的package包/類
private Message buildEmailMessage(EmailInfo emailInfo)
throws AddressException, MessagingException, UnsupportedEncodingException {
MimeMessage message = new MimeMessage(this.session);
message.setFrom(new InternetAddress(emailInfo.getFrom(), "網頁更新訂閱係統", "UTF-8"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailInfo.getTo()));
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(emailInfo.getContent(), "text/html;charset=UTF-8");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.setSubject(emailInfo.getTitle());
message.saveChanges();
return message;
}
示例6: run
import javax.mail.Multipart; //導入方法依賴的package包/類
@Override
public void run() {
try {
Multipart mainPart = new MimeMultipart("mixed");
mainPart.addBodyPart(bodyDestination.get().getBodyPart());
for (Future<EmailPartDocumentDestination> ad : attachmentDests) mainPart.addBodyPart(ad.get().getBodyPart());
Message msg = tx.email.newMimeMessage();
msg.setFrom(new InternetAddress(replacePlainTextParameters(fromPattern, params)));
msg.addRecipient(RecipientType.TO, new InternetAddress(replacePlainTextParameters(toPattern, params)));
msg.setSubject(replacePlainTextParameters(subjectPattern, params));
msg.setContent(mainPart);
msg.setSentDate(new Date());
tx.email.send(msg);
}
catch (ExecutionException | InterruptedException | MessagingException e) { throw new RuntimeException(e); }
}
示例7: prepareMessage
import javax.mail.Multipart; //導入方法依賴的package包/類
/**
* @param attachmentPaths
* Paths to attachment files.
* @throws Exception
* Exception.
* @return Message with attachments.
*/
private Message prepareMessage(String[] attachmentPaths) throws Exception {
Message message = new MimeMessage(Session.getDefaultInstance(System.getProperties()));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Test.");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
for (String file : attachmentPaths) {
BodyPart attachmentPart = new MimeBodyPart();
File attachment = new File(SRC_TEST_RESOURCES_MAILING_TEST + "/" + file);
DataSource source = new FileDataSource(attachment);
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName(attachment.getName());
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.removeHeader("Content-Type");
message.setHeader("Content-Type", "multipart/mixed");
Assert.assertTrue(message.isMimeType("multipart/*"));
return message;
}
示例8: createMimeMessageWithHtml
import javax.mail.Multipart; //導入方法依賴的package包/類
public static MimeMessage createMimeMessageWithHtml(Session session) throws MessagingException, AddressException {
MimeMessage message = createMimeMessage(session);
Multipart multiPart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("text");
multiPart.addBodyPart(textPart);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<b>html</b>", MailContentType.TEXT_HTML.getType());
multiPart.addBodyPart(htmlPart);
message.setContent(multiPart);
return message;
}
示例9: createMimeMessageWithAttachment
import javax.mail.Multipart; //導入方法依賴的package包/類
public static MimeMessage createMimeMessageWithAttachment(Session session, File attachment) throws MessagingException, AddressException, IOException {
MimeMessage message = createMimeMessage(session);
Multipart multiPart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("text");
multiPart.addBodyPart(textPart);
MimeBodyPart filePart = new MimeBodyPart();
filePart.attachFile(attachment);
multiPart.addBodyPart(filePart);
message.setContent(multiPart);
return message;
}
示例10: addAttachFile
import javax.mail.Multipart; //導入方法依賴的package包/類
/**
*
* 發送郵件時添加附件列表
* @param mp
* @param attachFiles
* @return
* @throws MailException
* @throws IOException
* @throws MessagingException
*/
private Multipart addAttachFile(Multipart mp, Set<String> attachFiles) throws MailException, IOException, MessagingException{
if(mp == null){
throw new MailException("bean Multipart is null .");
}
//沒有附件時直接返回
if(attachFiles == null || (attachFiles.size())== 0){
return mp;
}
Iterator<String> iterator = attachFiles.iterator();
while (iterator.hasNext()) {
String fileName = iterator.next();
MimeBodyPart mbp_file = new MimeBodyPart();
mbp_file.attachFile(fileName);
mp.addBodyPart(mbp_file);
//防止亂碼
String encode = MimeUtility.encodeText(mbp_file.getFileName());
mbp_file.setFileName(encode);
}
return mp;
}
示例11: sendHtmlMail
import javax.mail.Multipart; //導入方法依賴的package包/類
/**
* 以HTML格式發送郵件.
*
* @param mailSender 待發送的郵件信息
* @return Boolean
*/
protected boolean sendHtmlMail(final AbstractMailSender mailSender) {
final Properties pro = mailSender.getProperties();
MailAuthenticator authenticator = null;
if (mailSender.isValidate()) {
authenticator = new MailAuthenticator(mailSender.getUserName(), mailSender.getPassword());
}
final Session sendMailSession;
if(singletonSessionInstance) {
sendMailSession = Session.getDefaultInstance(pro, authenticator);
} else {
sendMailSession = Session.getInstance(pro, authenticator);
}
sendMailSession.setDebug(debugEnabled);
try {
final Message mailMessage = new MimeMessage(sendMailSession);
final Address from = new InternetAddress(mailSender.getFromAddress());
mailMessage.setFrom(from);
mailMessage.setRecipients(Message.RecipientType.TO, toAddresses(mailSender.getToAddress()));
mailMessage.setSubject(mailSender.getSubject());
mailMessage.setSentDate(new Date());
final Multipart mainPart = new MimeMultipart();
final BodyPart html = new MimeBodyPart();
html.setContent(mailSender.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
return true;
} catch (final MessagingException ex) {
LOGGER.error(ex.getMessage(), ex);
}
return false;
}
示例12: sendMail
import javax.mail.Multipart; //導入方法依賴的package包/類
public void sendMail(String from, String fromName, String toMail, String subject, String msgBody) {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from, fromName));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
// msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(from));
msg.setSubject(subject);
final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(msgBody, "text/html");
final Multipart mp = new MimeMultipart();
mp.addBodyPart(htmlPart);
msg.setContent(mp);
Transport.send(msg);
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
示例13: setAttachments
import javax.mail.Multipart; //導入方法依賴的package包/類
private void setAttachments(Multipart multipart, Collection<MailAttachment> attachments) throws MessagingException {
if (attachments == null || attachments.isEmpty()) {
return;
}
for(final MailAttachment attachment : attachments){
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(attachment.getLocation()) {
@Override
public String getContentType() {
return attachment.getContentTypeText();
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(attachment.getFileName());
multipart.addBodyPart(attachmentPart);
}
}
示例14: attachCSV
import javax.mail.Multipart; //導入方法依賴的package包/類
private void attachCSV(Multipart multipart, QrHolder[] attachmentData) throws Exception {
StringBuilder sb = new StringBuilder();
for (QrHolder qrHolder : attachmentData) {
sb.append(qrHolder.token)
.append(",")
.append(qrHolder.deviceId)
.append(",")
.append(qrHolder.dashId)
.append("\n");
}
MimeBodyPart attachmentsPart = new MimeBodyPart();
ByteArrayDataSource source = new ByteArrayDataSource(sb.toString(), "text/csv");
attachmentsPart.setDataHandler(new DataHandler(source));
attachmentsPart.setFileName("tokens.csv");
multipart.addBodyPart(attachmentsPart);
}
示例15: send
import javax.mail.Multipart; //導入方法依賴的package包/類
public Result<String> send(String subject, String text, String... sendTo) {
if (sendTo == null || sendTo.length == 0) {
return Result.success("no-to");
}
try {
// From
message.setFrom(this.from);
// 宛先(TO)
List<InternetAddress> tos = new ArrayList<>();
for (String address : sendTo) {
tos.add(new InternetAddress(address));
}
message.setRecipients(MimeMessage.RecipientType.TO, tos.toArray(new InternetAddress[0]));
// 件名および本文
message.setSubject(subject, "ISO-2022-JP");
Multipart multiPart = new MimeMultipart();
BodyPart html = new MimeBodyPart();
html.setContent(text, "text/html; charset=utf-8");
multiPart.addBodyPart(html);
message.setContent(multiPart);
// message.setText(text, "ISO-2022-JP");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
return Result.success("ok");
}