Search in sources :

Example 11 with Multipart

use of javax.mail.Multipart in project translationstudio8 by heartsome.

the class MessageParser method getText.

/**
	 * 取得文本内容,如果邮件是 HTML 格式的,则会返回所有 HTML 标签.
	 * @param p
	 *            Part 对象
	 * @return String
	 * 			  文本内容
	 * @throws MessagingException
	 *             the messaging exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
private String getText(Part p) throws MessagingException, IOException {
    if (p.isMimeType("text/*")) {
        if (p.isMimeType("text/html")) {
            htmlResource = new ResourceFileBean("html-file.html", p.getInputStream());
        }
        String s = (String) p.getContent();
        s = MimeUtility.decodeText(s);
        String contenttype = p.getContentType();
        String[] types = contenttype.split(";");
        for (String i : types) {
            if (i.toLowerCase().indexOf("charset") > 0) {
                String[] values = i.split("=");
                if (values != null && values.length >= 2) {
                    String charSet = values[1].trim();
                    if (charSet.indexOf("\"") != -1 || charSet.indexOf("\'") != -1) {
                        charSet = charSet.substring(1, charSet.length() - 1);
                    }
                    textCharset = charSet;
                }
            }
        }
        return s;
    }
    if (p.isMimeType("multipart/alternative")) {
        Multipart mp = (Multipart) p.getContent();
        String text = null;
        for (int i = 0; i < mp.getCount(); i++) {
            Part bp = mp.getBodyPart(i);
            if (bp.isMimeType("text/plain")) {
                if (text == null) {
                    text = getText(bp);
                }
                break;
            } else if (bp.isMimeType("text/html")) {
                String s = getText(bp);
                if (s != null) {
                    return s;
                }
            } else {
                return getText(bp);
            }
        }
        return text;
    } else if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
            String s = getText(mp.getBodyPart(i));
            if (s != null) {
                return s;
            }
        }
    }
    return null;
}
Also used : Multipart(javax.mail.Multipart) Part(javax.mail.Part)

Example 12 with Multipart

use of javax.mail.Multipart in project jforum2 by rafaelsteil.

the class POPMessage method extractMessageContents.

private void extractMessageContents(Message m) throws MessagingException {
    Part messagePart = m;
    if (this.message instanceof Multipart) {
        messagePart = ((Multipart) this.message).getBodyPart(0);
    }
    if (contentType.startsWith("text/html") || contentType.startsWith("text/plain")) {
        InputStream is = null;
        BufferedReader reader = null;
        try {
            is = messagePart.getInputStream();
            is.reset();
            reader = new BufferedReader(new InputStreamReader(is));
            StringBuffer sb = new StringBuffer(512);
            int c = 0;
            char[] ch = new char[2048];
            while ((c = reader.read(ch)) != -1) {
                sb.append(ch, 0, c);
            }
            this.messageContents = sb.toString();
        } catch (IOException e) {
            throw new MailException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
        }
    }
}
Also used : Multipart(javax.mail.Multipart) InputStreamReader(java.io.InputStreamReader) Part(javax.mail.Part) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) MailException(net.jforum.exceptions.MailException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MailException(net.jforum.exceptions.MailException)

Example 13 with Multipart

use of javax.mail.Multipart in project quickutil by quickutil.

the class MailUtil method send.

public static boolean send(String[] toMails, String[] ccMails, String[] bccMails, String title, String text, String[] attachments) {
    try {
        InternetAddress[] toAddresses = new InternetAddress[toMails.length];
        for (int i = 0; i < toMails.length; i++) {
            toAddresses[i] = new InternetAddress(toMails[i]);
        }
        if (ccMails == null)
            ccMails = new String[0];
        InternetAddress[] ccAddresses = new InternetAddress[ccMails.length];
        for (int i = 0; i < ccMails.length; i++) {
            ccAddresses[i] = new InternetAddress(ccMails[i]);
        }
        if (bccMails == null)
            bccMails = new String[0];
        InternetAddress[] bccAddresses = new InternetAddress[bccMails.length];
        for (int i = 0; i < bccMails.length; i++) {
            bccAddresses[i] = new InternetAddress(bccMails[i]);
        }
        Session mailSession = Session.getInstance(mailProperties, authenticator);
        MimeMessage message = new MimeMessage(mailSession);
        message.setFrom(new InternetAddress(mailProperties.getProperty("mail.user")));
        message.setSubject(title);
        message.setRecipients(RecipientType.TO, toAddresses);
        message.setRecipients(RecipientType.CC, ccAddresses);
        message.setRecipients(RecipientType.BCC, bccAddresses);
        Multipart multipart = new MimeMultipart();
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(text, "text/html;charset=UTF-8");
        multipart.addBodyPart(messageBodyPart);
        for (String filePath : attachments) {
            MimeBodyPart attachPart = new MimeBodyPart();
            File file = new File(filePath);
            if (!file.exists()) {
                throw new RuntimeException("not exist file: " + filePath);
            }
            attachPart.attachFile(filePath);
            multipart.addBodyPart(attachPart);
        }
        message.setContent(multipart);
        message.saveChanges();
        Transport.send(message);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) Session(javax.mail.Session)

Example 14 with Multipart

use of javax.mail.Multipart in project ddf by codice.

the class SmtpClientImplITCase method testSend.

@Test
public void testSend() throws IOException, MessagingException, ExecutionException, InterruptedException {
    int port = findAvailablePort();
    SimpleSmtpServer server = SimpleSmtpServer.start(port);
    SmtpClientImpl emailService = new SmtpClientImpl();
    emailService.setHostName(HOSTNAME);
    emailService.setPortNumber(port);
    Session session = emailService.createSession();
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress(FROM_ADDR));
    mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_ADDR));
    mimeMessage.setSubject(SUBJECT);
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(BODY);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    mimeMessage.setContent(multipart);
    emailService.send(mimeMessage).get();
    server.stop();
    assertThat(server.getReceivedEmailSize(), is(1));
    Iterator emailIterator = server.getReceivedEmail();
    SmtpMessage email = (SmtpMessage) emailIterator.next();
    assertThat(email.getHeaderValue(SUBJECT_HEADER), is(SUBJECT));
    assertThat(email.getHeaderValue(FROM_HEADER), containsString(FROM_ADDR));
    assertThat(email.getHeaderValue(TO_HEADER), containsString(TO_ADDR));
    assertThat(email.getBody(), containsString(BODY));
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SmtpMessage(com.dumbster.smtp.SmtpMessage) SimpleSmtpServer(com.dumbster.smtp.SimpleSmtpServer) Iterator(java.util.Iterator) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session) Test(org.junit.Test)

Example 15 with Multipart

use of javax.mail.Multipart in project Gargoyle by callakrsos.

the class EmailAttachmentSender method sendEmailWithAttachments.

public static void sendEmailWithAttachments(String host, String port, final String userName, final String password, String toAddress, String subject, String message, String[] attachFiles) throws AddressException, MessagingException {
    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.user", userName);
    properties.put("mail.password", password);
    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };
    Session session = Session.getInstance(properties, auth);
    // creates a new e-mail message
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    // creates message part
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(message, "text/html");
    // creates multi-part
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    // adds attachments
    if (attachFiles != null && attachFiles.length > 0) {
        for (String filePath : attachFiles) {
            MimeBodyPart attachPart = new MimeBodyPart();
            try {
                attachPart.attachFile(filePath);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            multipart.addBodyPart(attachPart);
        }
    }
    // sets the multi-part as e-mail's content
    msg.setContent(multipart);
    // sends the e-mail
    Transport.send(msg);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Aggregations

Multipart (javax.mail.Multipart)140 MimeMultipart (javax.mail.internet.MimeMultipart)101 MimeBodyPart (javax.mail.internet.MimeBodyPart)87 MimeMessage (javax.mail.internet.MimeMessage)79 BodyPart (javax.mail.BodyPart)60 InternetAddress (javax.mail.internet.InternetAddress)59 MessagingException (javax.mail.MessagingException)54 Session (javax.mail.Session)42 DataHandler (javax.activation.DataHandler)40 Properties (java.util.Properties)34 IOException (java.io.IOException)33 Date (java.util.Date)29 Message (javax.mail.Message)28 FileDataSource (javax.activation.FileDataSource)26 DataSource (javax.activation.DataSource)23 InputStream (java.io.InputStream)22 File (java.io.File)21 Part (javax.mail.Part)19 Test (org.junit.Test)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10