本文整理匯總了Java中javax.mail.internet.MimeBodyPart.setDataHandler方法的典型用法代碼示例。如果您正苦於以下問題:Java MimeBodyPart.setDataHandler方法的具體用法?Java MimeBodyPart.setDataHandler怎麽用?Java MimeBodyPart.setDataHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.mail.internet.MimeBodyPart
的用法示例。
在下文中一共展示了MimeBodyPart.setDataHandler方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addAttachment
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
public static String addAttachment(MimeMultipart mm, String path)
{
if(count == Integer.MAX_VALUE)
{
count = 0;
}
int cid = count++;
try
{
java.io.File file = new java.io.File(path);
MimeBodyPart mbp = new MimeBodyPart();
mbp.setDisposition(MimeBodyPart.INLINE);
mbp.setContent(new MimeMultipart("mixed"));
mbp.setHeader("Content-ID", "<" + cid + ">");
mbp.setDataHandler(new DataHandler(new FileDataSource(file)));
mbp.setFileName(new String(file.getName().getBytes("GBK"), "ISO-8859-1"));
mm.addBodyPart(mbp);
return String.valueOf(cid);
}
catch(Exception e)
{
e.printStackTrace();
}
return "";
}
示例2: addAttachment
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
/**
* Add the specified file as an attachment
*
* @param fileName
* name of the file
* @throws PackageException
* on error
*/
@PublicAtsApi
public void addAttachment(
String fileName ) throws PackageException {
try {
// add attachment to multipart content
MimeBodyPart attPart = new MimeBodyPart();
FileDataSource ds = new FileDataSource(fileName);
attPart.setDataHandler(new DataHandler(ds));
attPart.setDisposition(MimeBodyPart.ATTACHMENT);
attPart.setFileName(ds.getName());
addPart(attPart, PART_POSITION_LAST);
} catch (MessagingException me) {
throw new PackageException(me);
}
}
示例3: addBodyPart
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
/**
* Method addBodyPart
*
* @param mp
* @param dh
*/
private static void addBodyPart(MimeMultipart mp, DataHandler dh) {
MimeBodyPart messageBodyPart = new MimeBodyPart();
try {
messageBodyPart.setDataHandler(dh);
String contentType = dh.getContentType();
if ((contentType == null) || (contentType.trim().length() == 0)) {
contentType = "application/octet-stream";
}
System.out.println("Content type: " + contentType);
messageBodyPart.setHeader(HEADER_CONTENT_TYPE, contentType);
messageBodyPart.setHeader(
HEADER_CONTENT_TRANSFER_ENCODING,
"binary"); // Safe and fastest for anything other than mail
mp.addBodyPart(messageBodyPart);
} catch (javax.mail.MessagingException e) {
}
}
示例4: addToMimeMultipart
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
public void addToMimeMultipart (@Nonnull final MimeMultipart aMimeMultipart) throws MessagingException
{
ValueEnforcer.notNull (aMimeMultipart, "MimeMultipart");
final MimeBodyPart aMimeBodyPart = new MimeBodyPart ();
aMimeBodyPart.setHeader (CHttpHeader.CONTENT_ID, getId ());
// !IMPORTANT! DO NOT CHANGE the order of the adding a DH and then the last
// headers
// On some tests the datahandler did reset content-type and transfer
// encoding, so this is now the correct order
aMimeBodyPart.setDataHandler (new DataHandler (_getAsDataSource ()));
// After DataHandler!!
aMimeBodyPart.setHeader (CHttpHeader.CONTENT_TYPE, getMimeType ());
aMimeBodyPart.setHeader (CHttpHeader.CONTENT_TRANSFER_ENCODING, getContentTransferEncoding ().getID ());
aMimeMultipart.addBodyPart (aMimeBodyPart);
}
示例5: part
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
private static MimeBodyPart part(final ChainedHttpConfig config, final MultipartContent.MultipartPart multipartPart) throws MessagingException {
final MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setDisposition("form-data");
if (multipartPart.getFileName() != null) {
bodyPart.setFileName(multipartPart.getFileName());
bodyPart.setHeader("Content-Disposition", format("form-data; name=\"%s\"; filename=\"%s\"", multipartPart.getFieldName(), multipartPart.getFileName()));
} else {
bodyPart.setHeader("Content-Disposition", format("form-data; name=\"%s\"", multipartPart.getFieldName()));
}
bodyPart.setDataHandler(new DataHandler(new EncodedDataSource(config, multipartPart.getContentType(), multipartPart.getContent())));
bodyPart.setHeader("Content-Type", multipartPart.getContentType());
return bodyPart;
}
示例6: createMultiPart
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
/**
* 創建複雜的正文
* @return
* @throws MessagingException
*/
private Multipart createMultiPart() throws MessagingException {
// TODO Auto-generated method stub
Multipart multipart=new MimeMultipart();
//第一塊
BodyPart bodyPart1=new MimeBodyPart();
bodyPart1.setText("創建複雜的郵件,此為正文部分");
multipart.addBodyPart(bodyPart1);
//第二塊 以附件形式存在
MimeBodyPart bodyPart2=new MimeBodyPart();
//設置附件的處理器
FileDataSource attachFile=new FileDataSource(ClassLoader.getSystemResource("attach.txt").getFile());
DataHandler dh=new DataHandler(attachFile);
bodyPart2.setDataHandler(dh);
bodyPart2.setDisposition(Part.ATTACHMENT);
bodyPart2.setFileName("test");
multipart.addBodyPart(bodyPart2);
return multipart;
}
示例7: createAttachment
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
private MimeBodyPart createAttachment(File file, String filename, String desc) throws IOException, MessagingException {
MimeBodyPart mbp = new MimeBodyPart();
mbp.attachFile(file);
FileDataSource fds = new FileDataSource(file);
mbp.setDataHandler(new DataHandler(fds));
mbp.setDescription(desc);
mbp.setFileName(MimeUtility.encodeText(filename));
return mbp;
}
示例8: addAttachmentDir
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
/**
* Appends all files in a specified folder as attachments
*
* @param folder the folder containing all files to be attached
* @throws PackageException
*/
@PublicAtsApi
public void addAttachmentDir(
String folder ) throws PackageException {
// fetch list of files in specified directory
File dir = new File(folder);
File[] list = dir.listFiles();
if (null == list) {
throw new PackageException("Could not read from directory '" + folder + "'.");
} else {
// process all files, skipping directories
for (int i = 0; i < list.length; i++) {
if ( (null != list[i]) && (!list[i].isDirectory())) {
// add attachment to multipart content
MimeBodyPart attPart = new MimeBodyPart();
FileDataSource ds = new FileDataSource(list[i].getPath());
try {
attPart.setDataHandler(new DataHandler(ds));
attPart.setDisposition(MimeBodyPart.ATTACHMENT);
attPart.setFileName(ds.getName());
} catch (MessagingException me) {
throw new PackageException(me);
}
addPart(attPart, PART_POSITION_LAST);
}
}
}
}
示例9: addAttachment
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
private void addAttachment(Multipart mp, byte[] attachmentData, String mimeType, String filename)
throws MessagingException {
if (mp == null) {
return;
}
ByteArrayDataSource dataSrc = new ByteArrayDataSource(attachmentData, mimeType);
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName(filename);
attachment.setDataHandler(new DataHandler(dataSrc));
///attachment.setContent( attachmentData, mimeType );
mp.addBodyPart(attachment);
}
示例10: addAttachmentStream
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
private void addAttachmentStream(Multipart mp, InputStream attachmentStream, String mimeType,
String filename, BodyPart message)
throws MessagingException, IOException {
if (mp == null) {
return;
}
ByteArrayDataSource dataSrc = new ByteArrayDataSource(attachmentStream, mimeType);
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName(filename);
attachment.setDataHandler(new DataHandler(dataSrc));
///attachment.setContent( attachmentData, mimeType );
mp.addBodyPart(message);
mp.addBodyPart(attachment);
}
示例11: addAttachment
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
/**
* Add an attachment to the MimeMessage, taking the content from a
* {@code javax.activation.DataSource}.
* <p>Note that the InputStream returned by the DataSource implementation
* needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
* {@code getInputStream()} multiple times.
* @param attachmentFilename the name of the attachment as it will
* appear in the mail (the content type will be determined by this)
* @param dataSource the {@code javax.activation.DataSource} to take
* the content from, determining the InputStream and the content type
* @throws MessagingException in case of errors
* @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
* @see #addAttachment(String, java.io.File)
*/
public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {
Assert.notNull(attachmentFilename, "Attachment filename must not be null");
Assert.notNull(dataSource, "DataSource must not be null");
try {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
getRootMimeMultipart().addBodyPart(mimeBodyPart);
}
catch (UnsupportedEncodingException ex) {
throw new MessagingException("Failed to encode attachment filename", ex);
}
}
示例12: addFile
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
public MailSender addFile(File file) throws Exception {
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(body, "text/html");
multipart.addBodyPart(mbp1);
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(file);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
multipart.addBodyPart(mbp2);
return this;
}
示例13: createAttachMail
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
/**
* @Method: createAttachMail
* @Description: ����һ����������ʼ�
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createAttachMail(String subject, String content, String filePath) throws Exception{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
if(recipients.contains(";")){
List<InternetAddress> list = new ArrayList<InternetAddress>();
String []median=recipients.split(";");
for(int i=0;i<median.length;i++){
list.add(new InternetAddress(median[i]));
}
InternetAddress[] address =list.toArray(new InternetAddress[list.size()]);
message.setRecipients(Message.RecipientType.TO,address);
}else{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
}
message.setSubject(subject);
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(filePath));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
return message;
}
示例14: createImageMail
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
/**
* @Method: createImageMail
* @Description: ����һ���ʼ����Ĵ�ͼƬ���ʼ�
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createImageMail(String subject, String content, String imagePath) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
if(recipients.contains(";")){
List<InternetAddress> list = new ArrayList<InternetAddress>();
String []median=recipients.split(";");
for(int i=0;i<median.length;i++){
list.add(new InternetAddress(median[i]));
}
InternetAddress[] address =list.toArray(new InternetAddress[list.size()]);
message.setRecipients(Message.RecipientType.TO,address);
}else{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
}
message.setSubject(subject);
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(imagePath));
image.setDataHandler(dh);
image.setContentID("xxx.jpg");
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
message.setContent(mm);
message.saveChanges();
return message;
}
示例15: createMixedMail
import javax.mail.internet.MimeBodyPart; //導入方法依賴的package包/類
/**
* @Method: createMixedMail
* @Description: ����һ��������ʹ�ͼƬ���ʼ�
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createMixedMail(String subject, String content, String imagePath, String filePath) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
if(recipients.contains(";")){
List<InternetAddress> list = new ArrayList<InternetAddress>();
String []median=recipients.split(";");
for(int i=0;i<median.length;i++){
list.add(new InternetAddress(median[i]));
}
InternetAddress[] address =list.toArray(new InternetAddress[list.size()]);
message.setRecipients(Message.RecipientType.TO,address);
}else{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
}
message.setSubject(subject);
MimeBodyPart text = new MimeBodyPart();
text.setContent(content,"text/html;charset=UTF-8");
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource(imagePath)));
image.setContentID("aaa.jpg");
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(filePath));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(image);
mp.setSubType("related");
MimeBodyPart bodyContent = new MimeBodyPart();
bodyContent.setContent(mp);
message.saveChanges();
return message;
}