Search in sources :

Example 11 with Session

use of javax.mail.Session in project jodd by oblac.

the class Pop3Server method createSession.

/**
	 * {@inheritDoc}
	 */
public ReceiveMailSession createSession() {
    Session session = Session.getInstance(sessionProperties, authenticator);
    Store store;
    try {
        store = getStore(session);
    } catch (NoSuchProviderException nspex) {
        throw new MailException("Failed to create POP3 session", nspex);
    }
    return new ReceiveMailSession(session, store);
}
Also used : Store(javax.mail.Store) NoSuchProviderException(javax.mail.NoSuchProviderException) Session(javax.mail.Session)

Example 12 with Session

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

the class MailHealthIndicatorTests method setup.

@Before
public void setup() {
    Session session = Session.getDefaultInstance(new Properties());
    session.addProvider(new Provider(Type.TRANSPORT, "success", SuccessTransport.class.getName(), "Test", "1.0.0"));
    this.mailSender = mock(JavaMailSenderImpl.class);
    given(this.mailSender.getHost()).willReturn("smtp.acme.org");
    given(this.mailSender.getPort()).willReturn(25);
    given(this.mailSender.getSession()).willReturn(session);
    this.indicator = new MailHealthIndicator(this.mailSender);
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties) Session(javax.mail.Session) Provider(javax.mail.Provider) Before(org.junit.Before)

Example 13 with Session

use of javax.mail.Session in project google-app-engine-jappstart by taylorleese.

the class MailService method sendActivationEmail.

/**
     * Sends the activation e-mail to the given user.
     *
     * @param user the user
     * @param locale the locale
     * @throws MessagingException messaging exception
     */
public final void sendActivationEmail(final UserAccount user, final String locale) throws MessagingException {
    final Properties props = new Properties();
    final Session session = Session.getDefaultInstance(props, null);
    final Message message = new MimeMessage(session);
    final Multipart multipart = new MimeMultipart();
    final MimeBodyPart htmlPart = new MimeBodyPart();
    final MimeBodyPart textPart = new MimeBodyPart();
    message.setFrom(new InternetAddress(getFromAddress()));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
    message.setSubject(messageSource.getMessage("mail.subject", null, new Locale(locale)));
    textPart.setContent(messageSource.getMessage("mail.body.txt", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/plain");
    htmlPart.setContent(messageSource.getMessage("mail.body.html", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/html");
    multipart.addBodyPart(textPart);
    multipart.addBodyPart(htmlPart);
    message.setContent(multipart);
    Transport.send(message);
}
Also used : Locale(java.util.Locale) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Properties(java.util.Properties) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Example 14 with Session

use of javax.mail.Session in project gocd by gocd.

the class Pop3MailClient method getInboxFolder.

private Folder getInboxFolder() throws MessagingException {
    Properties pop3Props = new Properties();
    pop3Props.setProperty("mail.pop3.host", host);
    Authenticator auth = new PopupAuthenticator();
    Session session = Session.getInstance(pop3Props, auth);
    URLName url = new URLName("pop3", host, port, "", username, password);
    Store store = session.getStore(url);
    store.connect();
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    return folder;
}
Also used : URLName(javax.mail.URLName) Store(javax.mail.Store) Properties(java.util.Properties) Folder(javax.mail.Folder) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Example 15 with Session

use of javax.mail.Session in project nhin-d by DirectProject.

the class DSNGenerator method createDSNMessage.

/**
     * Creates a DSN message message.
     * @param originalSender The original sender of the message
     * @param originalSubject The subject of the original message
     * @param postmaster The postmaster address that the DSN message will be from
     * @param recipientDSNHeaders A list of recipient DSN headers to populate the delivery status part of the DSN message
     * @param messageDSNHeaders  The message DSN headers to populate the delivery status part of the DSN message
     * @param humanReadableText The human readable part (the first part) or the DSN message
     * @return A mime message containing the full DSN message
     * @throws MessagingException
     */
public MimeMessage createDSNMessage(InternetAddress originalSender, String originalSubject, InternetAddress postmaster, List<DSNRecipientHeaders> recipientDSNHeaders, DSNMessageHeaders messageDSNHeaders, MimeBodyPart humanReadableText) throws MessagingException {
    final DeliveryStatus deliveryStatus = createDeliveryStatus(recipientDSNHeaders, messageDSNHeaders);
    // assemble multipart report
    final MultipartReport multipartReport = new MultipartReport("", deliveryStatus);
    // set name of the delivery status file
    multipartReport.getBodyPart(DELIVERY_STATUS_MULTIPART_INDEX).setFileName("status.dat");
    // set text body part
    multipartReport.setTextBodyPart(humanReadableText);
    // create mime message to send from the MultipartReport
    final Properties properties = new Properties();
    properties.setProperty("mail.from", postmaster.getAddress());
    final Session session = Session.getInstance(properties);
    final MimeMessage destinationMessage = new MimeMessage(session);
    destinationMessage.setSentDate(Calendar.getInstance().getTime());
    destinationMessage.setContent(multipartReport);
    destinationMessage.setFrom(postmaster);
    destinationMessage.addRecipient(RecipientType.TO, originalSender);
    destinationMessage.setSubject(subjectPrefix + originalSubject);
    destinationMessage.setHeader(MailStandard.Headers.InReplyTo, messageDSNHeaders.getOriginalMessageId());
    destinationMessage.saveChanges();
    return destinationMessage;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) DeliveryStatus(com.sun.mail.dsn.DeliveryStatus) MultipartReport(com.sun.mail.dsn.MultipartReport) 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