Search in sources :

Example 81 with MimeMessage

use of javax.mail.internet.MimeMessage in project spring-framework by spring-projects.

the class JavaMailSenderTests method testJavaMailSenderWithMimeMessage.

public void testJavaMailSenderWithMimeMessage() throws MessagingException {
    MockJavaMailSender sender = new MockJavaMailSender();
    sender.setHost("host");
    sender.setUsername("username");
    sender.setPassword("password");
    MimeMessage mimeMessage = sender.createMimeMessage();
    mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("you@mail.org"));
    sender.send(mimeMessage);
    assertEquals("host", sender.transport.getConnectedHost());
    assertEquals("username", sender.transport.getConnectedUsername());
    assertEquals("password", sender.transport.getConnectedPassword());
    assertTrue(sender.transport.isCloseCalled());
    assertEquals(1, sender.transport.getSentMessages().size());
    assertEquals(mimeMessage, sender.transport.getSentMessage(0));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage)

Example 82 with MimeMessage

use of javax.mail.internet.MimeMessage in project spring-framework by spring-projects.

the class JavaMailSenderTests method javaMailSenderWithMimeMessagePreparators.

@Test
public void javaMailSenderWithMimeMessagePreparators() {
    MockJavaMailSender sender = new MockJavaMailSender();
    sender.setHost("host");
    sender.setUsername("username");
    sender.setPassword("password");
    final List<Message> messages = new ArrayList<>();
    MimeMessagePreparator preparator1 = new MimeMessagePreparator() {

        @Override
        public void prepare(MimeMessage mimeMessage) throws MessagingException {
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("he@mail.org"));
            messages.add(mimeMessage);
        }
    };
    MimeMessagePreparator preparator2 = new MimeMessagePreparator() {

        @Override
        public void prepare(MimeMessage mimeMessage) throws MessagingException {
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("she@mail.org"));
            messages.add(mimeMessage);
        }
    };
    sender.send(preparator1, preparator2);
    assertEquals("host", sender.transport.getConnectedHost());
    assertEquals("username", sender.transport.getConnectedUsername());
    assertEquals("password", sender.transport.getConnectedPassword());
    assertTrue(sender.transport.isCloseCalled());
    assertEquals(2, sender.transport.getSentMessages().size());
    assertEquals(messages.get(0), sender.transport.getSentMessage(0));
    assertEquals(messages.get(1), sender.transport.getSentMessage(1));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 83 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class GmailUsersMessagesIntegrationTest method createTestEmail.

private Message createTestEmail() throws MessagingException, IOException {
    com.google.api.services.gmail.model.Profile profile = requestBody("google-mail://users/getProfile?inBody=userId", CURRENT_USERID);
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    MimeMessage mm = new MimeMessage(session);
    mm.addRecipients(javax.mail.Message.RecipientType.TO, profile.getEmailAddress());
    mm.setSubject("Hello from camel-google-mail");
    mm.setContent("Camel rocks!", "text/plain");
    Message createMessageWithEmail = createMessageWithEmail(mm);
    return createMessageWithEmail;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Session(javax.mail.Session)

Example 84 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class GmailUsersMessagesIntegrationTest method createMessageWithEmail.

private Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    email.writeTo(baos);
    String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
    Message message = new Message();
    message.setRaw(encodedEmail);
    return message;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 85 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class GmailUsersThreadsIntegrationTest method createMessageWithEmail.

private Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    email.writeTo(baos);
    String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
    Message message = new Message();
    message.setRaw(encodedEmail);
    return message;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)1146 Test (org.junit.Test)374 InternetAddress (javax.mail.internet.InternetAddress)334 MessagingException (javax.mail.MessagingException)299 Session (javax.mail.Session)222 Properties (java.util.Properties)219 MimeMultipart (javax.mail.internet.MimeMultipart)208 MimeBodyPart (javax.mail.internet.MimeBodyPart)178 Date (java.util.Date)153 IOException (java.io.IOException)137 Message (javax.mail.Message)120 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)107 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)97 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)83 InputStream (java.io.InputStream)82 ArrayList (java.util.ArrayList)81 Multipart (javax.mail.Multipart)75 DataHandler (javax.activation.DataHandler)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)72 BodyPart (javax.mail.BodyPart)70