Search in sources :

Example 61 with Session

use of javax.mail.Session in project camel by apache.

the class GmailUsersMessagesIntegrationTest method toMimeMessage.

private MimeMessage toMimeMessage(Message message) throws MessagingException {
    byte[] emailBytes = Base64.decodeBase64(message.getRaw());
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    return new MimeMessage(session, new ByteArrayInputStream(emailBytes));
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Properties(java.util.Properties) Session(javax.mail.Session)

Example 62 with Session

use of javax.mail.Session in project camel by apache.

the class GmailUsersThreadsIntegrationTest method createThreadedTestEmail.

private Message createThreadedTestEmail(String previousThreadId) 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);
    if (previousThreadId != null) {
        createMessageWithEmail.setThreadId(previousThreadId);
    }
    Map<String, Object> headers = new HashMap<String, Object>();
    // parameter type is String
    headers.put("CamelGoogleMail.userId", CURRENT_USERID);
    // parameter type is com.google.api.services.gmail.model.Message
    headers.put("CamelGoogleMail.content", createMessageWithEmail);
    return requestBodyAndHeaders("google-mail://messages/send", null, headers);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) HashMap(java.util.HashMap) Properties(java.util.Properties) Session(javax.mail.Session)

Example 63 with Session

use of javax.mail.Session in project ice by Netflix.

the class BasicWeeklyCostEmailService method sendEmail.

private void sendEmail(boolean test, AmazonSimpleEmailServiceClient emailService, String email, List<ApplicationGroup> appGroups) throws IOException, MessagingException {
    StringBuilder body = new StringBuilder();
    body.append("<html><head><style type=\"text/css\">a:link, a:visited{color:#006DBA;}a:link, a:visited, a:hover {\n" + "text-decoration: none;\n" + "}\n" + "body {\n" + "color: #333;\n" + "}" + "</style></head>");
    List<MimeBodyPart> mimeBodyParts = Lists.newArrayList();
    int index = 0;
    String subject = "";
    for (ApplicationGroup appGroup : appGroups) {
        boolean hasData = false;
        for (String prodName : appGroup.data.keySet()) {
            if (config.productService.getProductByName(prodName) == null)
                continue;
            hasData = appGroup.data.get(prodName) != null && appGroup.data.get(prodName).size() > 0;
            if (hasData)
                break;
        }
        if (!hasData)
            continue;
        try {
            MimeBodyPart mimeBodyPart = constructEmail(index, appGroup, body);
            index++;
            if (mimeBodyPart != null) {
                mimeBodyParts.add(mimeBodyPart);
                subject = subject + (subject.length() > 0 ? ", " : "") + appGroup.getDisplayName();
            }
        } catch (Exception e) {
            logger.error("Error contructing email", e);
        }
    }
    body.append("</html>");
    if (mimeBodyParts.size() == 0)
        return;
    DateTime end = new DateTime(DateTimeZone.UTC).withDayOfWeek(1).withMillisOfDay(0);
    subject = String.format("%s Weekly AWS Costs (%s - %s)", subject, formatter.print(end.minusWeeks(1)), formatter.print(end));
    String toEmail = test ? testEmail : email;
    Session session = Session.getInstance(new Properties());
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setSubject(subject);
    mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, toEmail);
    if (!test && !StringUtils.isEmpty(bccEmail)) {
        mimeMessage.addRecipients(Message.RecipientType.BCC, bccEmail);
    }
    MimeMultipart mimeMultipart = new MimeMultipart();
    BodyPart p = new MimeBodyPart();
    p.setContent(body.toString(), "text/html");
    mimeMultipart.addBodyPart(p);
    for (MimeBodyPart mimeBodyPart : mimeBodyParts) mimeMultipart.addBodyPart(mimeBodyPart);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    mimeMessage.setContent(mimeMultipart);
    mimeMessage.writeTo(outputStream);
    RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
    SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
    rawEmailRequest.setDestinations(Lists.<String>newArrayList(toEmail));
    rawEmailRequest.setSource(fromEmail);
    logger.info("sending email to " + toEmail + " " + body.toString());
    emailService.sendRawEmail(rawEmailRequest);
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) ApplicationGroup(com.netflix.ice.reader.ApplicationGroup) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SendRawEmailRequest(com.amazonaws.services.simpleemail.model.SendRawEmailRequest) MimeBodyPart(javax.mail.internet.MimeBodyPart) RawMessage(com.amazonaws.services.simpleemail.model.RawMessage) Session(javax.mail.Session)

Example 64 with Session

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

the class POPConnector method openConnection.

/**
	 * Opens a connection to the pop server. 
	 * The method will try to retrieve the <i>INBOX</i> folder in 
	 * <i>READ_WRITE</i> mode
	 */
public void openConnection() {
    try {
        Session session = Session.getDefaultInstance(new Properties());
        this.store = session.getStore(this.mailIntegration.isSSL() ? "pop3s" : "pop3");
        this.store.connect(this.mailIntegration.getPopHost(), this.mailIntegration.getPopPort(), this.mailIntegration.getPopUsername(), this.mailIntegration.getPopPassword());
        this.folder = this.store.getFolder("INBOX");
        if (folder == null) {
            throw new Exception("No Inbox");
        }
        this.folder.open(Folder.READ_WRITE);
    } catch (Exception e) {
        throw new MailException(e);
    }
}
Also used : MailException(net.jforum.exceptions.MailException) Properties(java.util.Properties) MailException(net.jforum.exceptions.MailException) Session(javax.mail.Session)

Example 65 with Session

use of javax.mail.Session in project spring-boot by spring-projects.

the class MailSenderAutoConfigurationTests method configureJndiSession.

private Session configureJndiSession(String name) throws IllegalStateException, NamingException {
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    TestableInitialContextFactory.bind(name, session);
    return session;
}
Also used : Properties(java.util.Properties) Session(javax.mail.Session)

Aggregations

Session (javax.mail.Session)110 MimeMessage (javax.mail.internet.MimeMessage)72 Properties (java.util.Properties)66 InternetAddress (javax.mail.internet.InternetAddress)49 MessagingException (javax.mail.MessagingException)47 Message (javax.mail.Message)31 Test (org.junit.Test)30 Transport (javax.mail.Transport)28 JMSession (com.zimbra.cs.util.JMSession)20 PasswordAuthentication (javax.mail.PasswordAuthentication)19 Date (java.util.Date)17 MimeBodyPart (javax.mail.internet.MimeBodyPart)16 MimeMultipart (javax.mail.internet.MimeMultipart)16 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)15 IOException (java.io.IOException)15 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)15 Authenticator (javax.mail.Authenticator)12 Multipart (javax.mail.Multipart)11 NoSuchProviderException (javax.mail.NoSuchProviderException)10 BodyPart (javax.mail.BodyPart)9