Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to fetch email and get there body content in spring boot

    @Throws(IOException::class, MessagingException::class)
    private fun getTextFromMessage(message: Message): String {
        var result: String = ""
        if (message.isMimeType("text/plain")) {
            result = message.content.toString()
        }
        else if (message.isMimeType("multipart/*")) {
            val mimeMultipart =
                message.content as MimeMultipart
            result = getTextFromMimeMultipart(mimeMultipart)
        }
        else if(message.isMimeType("text/html")){
            result = message.content.toString()
        }
        return result
    }

    @Throws(IOException::class, MessagingException::class)
    private fun getTextFromMimeMultipart(
        mimeMultipart: MimeMultipart
    ): String {
        val count = mimeMultipart.count
        if (count == 0) throw MessagingException("Multipart with no body parts not supported.")

        val multipartRelated = ContentType(mimeMultipart.contentType).match("multipart/related")


        if(multipartRelated){
            val part = mimeMultipart.getBodyPart(0)
            val multipartAlt = ContentType(part.contentType).match("multipart/alternative")
            if(multipartAlt) {
                return getTextFromMimeMultipart(part.content as MimeMultipart)
            }
        }else{
            val multipartAlt = ContentType(mimeMultipart.contentType).match("multipart/alternative")
            if (multipartAlt) {
                for (i in 0 until count) {
                    val part = mimeMultipart.getBodyPart(i)
                    if (part.isMimeType("text/html")) {
                        return getTextFromBodyPart(part)
                    }
                }
            }
        }


        var result: String = ""
        for (i in 0 until count) {
            val bodyPart = mimeMultipart.getBodyPart(i)
            result += getTextFromBodyPart(bodyPart)
        }
        return result
    }

    @Throws(IOException::class, MessagingException::class)
    private fun getTextFromBodyPart(
        bodyPart: BodyPart
    ): String {
        var result: String = ""
        if (bodyPart.isMimeType("text/plain")) {
            result = bodyPart.content as String
        } else if (bodyPart.isMimeType("text/html")) {
            val html = bodyPart.content as String
            result = html
        } else if (bodyPart.content is MimeMultipart) {
            result =
                getTextFromMimeMultipart(bodyPart.content as MimeMultipart)
        }
        return result
    }
Comment

PREVIOUS NEXT
Code Example
Java :: exit for loop java stack overflow 
Java :: Java Add elements to a LinkedList 
Java :: xml definition file for spring 
Java :: run java bytecode 
Java :: exception handling and reprompting 
Java :: spigot item break 
Java :: what is resource bundle class in java 
Java :: android java update image dynamically 
Java :: nikita kaksharov 
Java :: how much epsom salt should strawberries need 
Java :: Example: My favorite cities 
Java :: java casting method 
Java :: hdfs get size of directory java 
Java :: jdbc code 
Java :: Java program to calculate cubic capacity cc in bikes 
Java :: minecraft 21w14a bugs 
Java :: thymeleaf Expression Object dialects 
Java :: java operater == char 
Java :: index out of bounds exception java 
Java :: what is this code (long millis=System.currentTimeMillis(); java.sql.Date date=new java.sql.Date(millis); 
Java :: using condition for each loop 
Java :: android java sqlite alter table 
Java :: how to generate a random number in libgdx 
Java :: java delete instance of class 
Java :: how to add classpath in spring boot 
Java :: testng with cucumber 
Java :: how to use a switch statement in java 
Java :: java join array 
Java :: find power of number in method java 
Java :: how to be good at javasctript 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =