是否可以在不使用javamail API的情况下创建MIME消息对象?

时间:2018-01-19 10:58:59

标签: java email javamail mime mail-server

我打算不使用javamail API。我打算通过SSLSocket发送消息。我已建立安全的SSL / TLS连接。但是如何在不导入javax.mail的情况下创建MIME对象。*;包以便通过套接字发送

 protected static MimeMessage attach() throws IOException, MessagingException {

        Properties properties = System.getProperties();

        Session session = Session.getDefaultInstance(properties,
                new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(messageSubject);

        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setText(messageBody);

        MimeBodyPart messageBodyPart2 = new MimeBodyPart();

        String filename = attPath;
        DataSource source = new FileDataSource(filename);
        messageBodyPart2.setDataHandler(new DataHandler(source));
        messageBodyPart2.setFileName(filename);



        Multipart multipart = new MimeMultipart();
        MimeBodyPart messageBodyPart3 = new MimeBodyPart();
        messageBodyPart3.setText("\nBye");



        //ATTACH 3 BODY PARTS
        multipart.addBodyPart(messageBodyPart1);
        multipart.addBodyPart(messageBodyPart2);
        multipart.addBodyPart(messageBodyPart3);


        message.setContent(multipart);


        return message;

    }

0 个答案:

没有答案