Java 类javax.mail.internet.MimeMultipart 实例源码
项目:Cognizant-Intelligent-Test-Scripter
文件:Mailer.java
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;
}
项目:opentest
文件:ReadEmailImap.java
/**
* Extracts the text content of a multipart email message
*/
private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws Exception {
String result = "";
int partCount = mimeMultipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result = result + "\n" + bodyPart.getContent();
break; // without break same text appears twice in my tests
} else if (bodyPart.isMimeType("text/html")) {
String html = (String) bodyPart.getContent();
// result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
result = html;
} else if (bodyPart.getContent() instanceof MimeMultipart) {
result = result + getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent());
}
}
return result;
}
项目:Camel
文件:MailConverters.java
/**
* Converts the given JavaMail message to a String body.
* Can return null.
*/
@Converter
public static String toString(Message message) throws MessagingException, IOException {
Object content = message.getContent();
if (content instanceof MimeMultipart) {
MimeMultipart multipart = (MimeMultipart) content;
if (multipart.getCount() > 0) {
BodyPart part = multipart.getBodyPart(0);
content = part.getContent();
}
}
if (content != null) {
return content.toString();
}
return null;
}
项目:JavaRushTasks
文件:Solution.java
public static void setAttachment(Message message, String filename) throws MessagingException {
// Create a multipar message
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
//Set File
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
//Add "file part" to multipart
multipart.addBodyPart(messageBodyPart);
//Set multipart to message
message.setContent(multipart);
}
项目:camunda-bpm-mail
文件:MailTestUtil.java
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;
}
项目:scada
文件:MailUtils.java
/**
* �ж��ʼ����Ƿ��������
* @param msg �ʼ�����
* @return �ʼ��д��ڸ�������true�������ڷ���false
*/
public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
boolean flag = false;
if (part.isMimeType("multipart/*")) {
MimeMultipart multipart = (MimeMultipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String disp = bodyPart.getDisposition();
if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
flag = true;
} else if (bodyPart.isMimeType("multipart/*")) {
flag = isContainAttachment(bodyPart);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf("application") != -1) {
flag = true;
}
if (contentType.indexOf("name") != -1) {
flag = true;
}
}
if (flag) break;
}
} else if (part.isMimeType("message/rfc822")) {
flag = isContainAttachment((Part)part.getContent());
}
return flag;
}
项目:alimama
文件:POP3ReceiveMailTest.java
/**
* 判断邮件中是否包含附件
* @param msg 邮件内容
* @return 邮件中存在附件返回true,不存在返回false
* @throws MessagingException
* @throws IOException
*/
public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
boolean flag = false;
if (part.isMimeType("multipart/*")) {
MimeMultipart multipart = (MimeMultipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String disp = bodyPart.getDisposition();
if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
flag = true;
} else if (bodyPart.isMimeType("multipart/*")) {
flag = isContainAttachment(bodyPart);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf("application") != -1) {
flag = true;
}
if (contentType.indexOf("name") != -1) {
flag = true;
}
}
if (flag) break;
}
} else if (part.isMimeType("message/rfc822")) {
flag = isContainAttachment((Part)part.getContent());
}
return flag;
}
项目:alfresco-greenmail
文件:GreenMailUtil.java
public static boolean hasNonTextAttachments(Part m) {
try {
Object content = m.getContent();
if (content instanceof MimeMultipart) {
MimeMultipart mm = (MimeMultipart) content;
for (int i=0;i<mm.getCount();i++) {
BodyPart p = mm.getBodyPart(i);
if (hasNonTextAttachments(p)) {
return true;
}
}
return false;
} else {
return !m.getContentType().trim().toLowerCase().startsWith("text");
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
项目:ats-framework
文件:MimePackage.java
private String addContentTrace(
Object content,
String level ) throws MessagingException {
final String level1 = level;
final String level2 = "\t" + level1;
StringBuilder msg = new StringBuilder();
if (content instanceof String) {
msg.append(level2 + "BODY STRING START:\n");
msg.append((String) content);
msg.append(level2 + "BODY STRING END:\n");
} else if (content instanceof MimeMultipart) {
MimeMultipart multipart = (MimeMultipart) content;
for (int i = 0; i < multipart.getCount(); i++) {
msg.append(addBodyPartTrace(multipart.getBodyPart(i), level2));
}
} else {
msg.append(level2 + "*** CANNOT CONVERT UNSUPPORTED CONTENT: "
+ content.getClass().getCanonicalName() + " ***\n");
}
return msg.toString();
}
项目:lams
文件:MimeMessageHelper.java
/**
* Set the given plain text and HTML text as alternatives, offering
* both options to the email client. Requires multipart mode.
* <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText};
* else, mail readers might not be able to resolve inline references correctly.
* @param plainText the plain text for the message
* @param htmlText the HTML text for the message
* @throws MessagingException in case of errors
*/
public void setText(String plainText, String htmlText) throws MessagingException {
Assert.notNull(plainText, "Plain text must not be null");
Assert.notNull(htmlText, "HTML text must not be null");
MimeMultipart messageBody = new MimeMultipart(MULTIPART_SUBTYPE_ALTERNATIVE);
getMainPart().setContent(messageBody, CONTENT_TYPE_ALTERNATIVE);
// Create the plain text part of the message.
MimeBodyPart plainTextPart = new MimeBodyPart();
setPlainTextToMimePart(plainTextPart, plainText);
messageBody.addBodyPart(plainTextPart);
// Create the HTML text part of the message.
MimeBodyPart htmlTextPart = new MimeBodyPart();
setHtmlTextToMimePart(htmlTextPart, htmlText);
messageBody.addBodyPart(htmlTextPart);
}
项目:OrigamiSMTP
文件:Message.java
public void processMessage()
{
System.out.println("Process message");
try
{
Session session = Session.getDefaultInstance(new Properties());
InputStream inputStream = new ByteArrayInputStream(message.getBytes());
MimeMessage mimeMessage = new MimeMessage(session,inputStream);
if(mimeMessage.isMimeType(InboxVariables.plainMime))
{
plainMessage = mimeMessage.getContent().toString();
}
else if(mimeMessage.isMimeType(InboxVariables.multipartMime))
{
MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent();
processMimeMultipart(mimeMultipart);
}
subject = mimeMessage.getSubject();
System.out.println("Message processed");
}
catch(Exception ex)
{
ex.printStackTrace(System.err);
}
}
项目:parabuild-ci
文件:Tester.java
/**
* 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) {
}
}
项目:dswork
文件:EmailUtil.java
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 "";
}
项目:onestop-endpoints
文件:EmailTask.java
@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); }
}
项目:hub-email-extension
文件:MimeMultipartBuilder.java
private MimeBodyPart buildEmailBodyPart() throws MessagingException {
final MimeMultipart emailContent = new MimeMultipart("alternative");
// add from low fidelity to high fidelity
if (StringUtils.isNotBlank(text)) {
final MimeBodyPart textBodyPart = buildTextBodyPart();
emailContent.addBodyPart(textBodyPart);
}
if (StringUtils.isNotBlank(html)) {
final MimeBodyPart htmlBodyPart = buildHtmlBodyPart();
emailContent.addBodyPart(htmlBodyPart);
}
final MimeBodyPart emailBodyPart = new MimeBodyPart();
emailBodyPart.setContent(emailContent);
return emailBodyPart;
}
项目:hub-email-extension
文件:EmailMessagingService.java
private Message createMessage(final String emailAddress, final String subjectLine, final Session session,
final MimeMultipart mimeMultipart, final ExtensionProperties properties) throws MessagingException {
final List<InternetAddress> addresses = new ArrayList<>();
try {
addresses.add(new InternetAddress(emailAddress));
} catch (final AddressException e) {
log.warn(String.format("Could not create the address from %s: %s", emailAddress, e.getMessage()));
}
if (addresses.isEmpty()) {
throw new RuntimeException("There were no valid email addresses supplied.");
}
final Message message = new MimeMessage(session);
message.setContent(mimeMultipart);
message.setFrom(new InternetAddress(properties.getEmailFromAddress()));
message.setRecipients(Message.RecipientType.TO, addresses.toArray(new Address[addresses.size()]));
message.setSubject(subjectLine);
return message;
}
项目:carrier
文件:SMTPMessageFactory.java
/**
* Creates an unsigned MIME message used for forwarding the given incoming e-mail.
*
* @param email The parsed e-mail.
* @param fromAddress The 'from' address.
* @param toAddress The 'to' address.
* @return The MIME message.
* @throws Exception If there is an error while creating the unsigned MIME message.
*/
private MimeMessage createUnsignedMimeMessage(Email email, InternetAddress fromAddress, InternetAddress toAddress)
throws Exception {
Session session = Session.getDefaultInstance(System.getProperties());
MimeMessage message = new MimeMessage(session);
message.setFrom(fromAddress);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(email.getEmailSubject());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(createMimeMessageBody(email));
for(BodyPart attachment : createMimeMessageAttachments(email)) {
multipart.addBodyPart(attachment);
}
message.setContent(multipart);
return message;
}
项目:communote-server
文件:MailMessageHelperTest.java
/**
* @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;
}
项目:cloud-meter
文件:MailReaderSampler.java
private void appendMultiPart(SampleResult child, StringBuilder cdata,
MimeMultipart mmp) throws MessagingException, IOException {
String preamble = mmp.getPreamble();
if (preamble != null ){
cdata.append(preamble);
}
child.setResponseData(cdata.toString(),child.getDataEncodingNoDefault());
int count = mmp.getCount();
for (int j=0; j<count;j++){
BodyPart bodyPart = mmp.getBodyPart(j);
final Object bodyPartContent = bodyPart.getContent();
final String contentType = bodyPart.getContentType();
SampleResult sr = new SampleResult();
sr.setSampleLabel("Part: "+j);
sr.setContentType(contentType);
sr.setDataEncoding(RFC_822_DEFAULT_ENCODING);
sr.setEncodingAndType(contentType);
sr.sampleStart();
if (bodyPartContent instanceof InputStream){
sr.setResponseData(IOUtils.toByteArray((InputStream) bodyPartContent));
} else if (bodyPartContent instanceof MimeMultipart){
appendMultiPart(sr, cdata, (MimeMultipart) bodyPartContent);
} else {
sr.setResponseData(bodyPartContent.toString(),sr.getDataEncodingNoDefault());
}
sr.setResponseOK();
if (sr.getEndTime()==0){// not been set by any child samples
sr.sampleEnd();
}
child.addSubResult(sr);
}
}
项目:xframium-java
文件:DefaultReceiveEmailProvider.java
public MessageWrapper _getEmail( String hostName, MessageFilter[] messageFilters, Map<String, String> propertyMap )
{
Properties mailProps = new Properties();
mailProps.putAll( propertyMap );
List<Message> messageList = new ArrayList<Message>( 10 );
MessageWrapper messageWrapper = null;
try
{
Session emailSession = Session.getDefaultInstance( mailProps );
Store store = emailSession.getStore( propertyMap.get( PROTOCOL ) );
store.connect( hostName, propertyMap.get( USER_NAME ), propertyMap.get( PASSWORD ) );
Folder emailFolder = store.getFolder( propertyMap.get( FOLDER_NAME ) != null ? propertyMap.get( FOLDER_NAME ) : DEFAULT_FOLDER_NAME );
emailFolder.open( Folder.READ_WRITE );
Message[] messages = emailFolder.getMessages();
int messageCount = emailFolder.getMessageCount();
if ( messageCount > MAX_MESSAGES )
messageCount = MAX_MESSAGES;
if ( log.isInfoEnabled() )
log.info( "Processing " + messageCount + " messages" );
for ( int i = 0; i < messageCount; i++ )
{
if ( applyFilters( messages[i], messageFilters ) )
{
messageList.add( messages[i] );
}
}
if ( messageList.size() == 0 )
throw new ScriptException( "Failed to find any email messages that met the criteria" );
Collections.sort( messageList, new DateComparator() );
String messageContent = null;
String contentType = messageList.get( 0 ).getContentType();
if ( contentType.startsWith( "text/" ) )
messageContent = messageList.get( 0 ).getContent().toString();
else if ( messageList.get( 0 ).isMimeType( "multipart/*" ) )
messageContent = getTextFromMimeMultipart( (MimeMultipart) messageList.get( 0 ).getContent() );
else
messageContent = messageList.get( 0 ).getContent().getClass().getName();
messageWrapper = new MessageWrapper( messageList.size(), messageList.get( 0 ).getFrom()[0].toString(), messageList.get( 0 ).getSubject(), messageContent, contentType );
emailFolder.close( false );
store.close();
}
catch ( Exception e )
{
e.printStackTrace();
throw new ScriptException( "Failed to find email" );
}
return messageWrapper;
}
项目:xframium-java
文件:DefaultReceiveEmailProvider.java
private String getTextFromMimeMultipart( MimeMultipart mimeMultipart ) throws IOException, MessagingException
{
int count = mimeMultipart.getCount();
if ( count == 0 )
throw new MessagingException( "Multipart with no body parts not supported." );
boolean multipartAlt = new ContentType( mimeMultipart.getContentType() ).match( "multipart/alternative" );
if ( multipartAlt )
return getTextFromBodyPart( mimeMultipart.getBodyPart( count - 1 ) );
String result = "";
for ( int i = 0; i < count; i++ )
{
BodyPart bodyPart = mimeMultipart.getBodyPart( i );
result += getTextFromBodyPart( bodyPart );
}
return result;
}
项目:aml
文件:AbstractGenerator.java
/**
* <p>addBodyParameters.</p>
*
* @param bodyMimeType a {@link org.aml.apimodel.MimeType} object.
* @param method a {@link com.sun.codemodel.JMethod} object.
* @param javadoc a {@link com.sun.codemodel.JDocComment} object.
* @throws java.lang.Exception if any.
*/
protected void addBodyParameters(final MimeType bodyMimeType,
final JMethod method, final JDocComment javadoc) throws Exception {
if (bodyMimeType == null) {
return;
} else if (MediaType.APPLICATION_FORM_URLENCODED.equals(bodyMimeType
.getType())) {
addFormParameters(bodyMimeType, method, javadoc);
} else if (MediaType.MULTIPART_FORM_DATA.equals(bodyMimeType.getType())) {
// use a "catch all" javax.mail.internet.MimeMultipart parameter
addCatchAllFormParametersArgument(bodyMimeType, method, javadoc,
types.getGeneratorType(MimeMultipart.class));
} else {
addPlainBodyArgument(bodyMimeType, method, javadoc);
}
}
项目:aml
文件:AbstractGenerator.java
/**
* <p>addBodyParameters.</p>
*
* @param bodyMimeType a {@link org.aml.apimodel.MimeType} object.
* @param method a {@link com.sun.codemodel.JMethod} object.
* @param javadoc a {@link com.sun.codemodel.JDocComment} object.
* @throws java.lang.Exception if any.
*/
protected void addBodyParameters(final MimeType bodyMimeType,
final JMethod method, final JDocComment javadoc) throws Exception {
if (bodyMimeType == null) {
return;
} else if (MediaType.APPLICATION_FORM_URLENCODED.equals(bodyMimeType
.getType())) {
addFormParameters(bodyMimeType, method, javadoc);
} else if (MediaType.MULTIPART_FORM_DATA.equals(bodyMimeType.getType())) {
// use a "catch all" javax.mail.internet.MimeMultipart parameter
addCatchAllFormParametersArgument(bodyMimeType, method, javadoc,
types.getGeneratorType(MimeMultipart.class));
} else {
addPlainBodyArgument(bodyMimeType, method, javadoc);
}
}