Search in sources :

Example 21 with InternetAddress

use of javax.mail.internet.InternetAddress in project jforum2 by rafaelsteil.

the class Spammer method dispatchMessages.

public boolean dispatchMessages() {
    try {
        int sendDelay = SystemGlobals.getIntValue(ConfigKeys.MAIL_SMTP_DELAY);
        if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_AUTH)) {
            if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
                boolean ssl = SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_SSL);
                Transport transport = this.session.getTransport(ssl ? "smtps" : "smtp");
                try {
                    String host = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST);
                    transport.connect(host, username, password);
                    if (transport.isConnected()) {
                        for (Iterator userIter = this.users.iterator(); userIter.hasNext(); ) {
                            User user = (User) userIter.next();
                            if (this.needCustomization) {
                                this.defineUserMessage(user);
                            }
                            Address address = new InternetAddress(user.getEmail());
                            logger.debug("Sending mail to: " + user.getEmail());
                            this.message.setRecipient(Message.RecipientType.TO, address);
                            transport.sendMessage(this.message, new Address[] { address });
                            if (sendDelay > 0) {
                                try {
                                    Thread.sleep(sendDelay);
                                } catch (InterruptedException ie) {
                                    logger.error("Error while Thread.sleep." + ie, ie);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new MailException(e);
                } finally {
                    try {
                        transport.close();
                    } catch (Exception e) {
                    }
                }
            }
        } else {
            for (Iterator iter = this.users.iterator(); iter.hasNext(); ) {
                User user = (User) iter.next();
                if (this.needCustomization) {
                    this.defineUserMessage(user);
                }
                Address address = new InternetAddress(user.getEmail());
                logger.debug("Sending mail to: " + user.getEmail());
                this.message.setRecipient(Message.RecipientType.TO, address);
                Transport.send(this.message, new Address[] { address });
                if (sendDelay > 0) {
                    try {
                        Thread.sleep(sendDelay);
                    } catch (InterruptedException ie) {
                        logger.error("Error while Thread.sleep." + ie, ie);
                    }
                }
            }
        }
    } catch (MessagingException e) {
        logger.error("Error while dispatching the message." + e, e);
    }
    return true;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) User(net.jforum.entities.User) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Iterator(java.util.Iterator) MailException(net.jforum.exceptions.MailException) Transport(javax.mail.Transport) MessagingException(javax.mail.MessagingException) MailException(net.jforum.exceptions.MailException)

Example 22 with InternetAddress

use of javax.mail.internet.InternetAddress in project jforum2 by rafaelsteil.

the class Spammer method prepareMessage.

/**
	 * Prepares the mail message for sending.
	 * 
	 * @param subject the subject of the email
	 * @param messageFile the path to the mail message template
	 * @throws MailException
	 */
protected void prepareMessage(String subject, String messageFile) throws MailException {
    if (this.messageId != null) {
        this.message = new IdentifiableMimeMessage(session);
        ((IdentifiableMimeMessage) this.message).setMessageId(this.messageId);
    } else {
        this.message = new MimeMessage(session);
    }
    this.templateParams.put("forumName", SystemGlobals.getValue(ConfigKeys.FORUM_NAME));
    try {
        this.message.setSentDate(new Date());
        this.message.setFrom(new InternetAddress(SystemGlobals.getValue(ConfigKeys.MAIL_SENDER)));
        this.message.setSubject(subject, SystemGlobals.getValue(ConfigKeys.MAIL_CHARSET));
        if (this.inReplyTo != null) {
            this.message.addHeader("In-Reply-To", this.inReplyTo);
        }
        this.createTemplate(messageFile);
        this.needCustomization = this.isCustomizationNeeded();
        // then build the generic text right now
        if (!this.needCustomization) {
            String text = this.processTemplate();
            this.defineMessageText(text);
        }
    } catch (Exception e) {
        throw new MailException(e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MailException(net.jforum.exceptions.MailException) Date(java.util.Date) MessagingException(javax.mail.MessagingException) MailException(net.jforum.exceptions.MailException)

Example 23 with InternetAddress

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

the class MimeMessageHelper method parseAddress.

private InternetAddress parseAddress(String address) throws MessagingException {
    InternetAddress[] parsed = InternetAddress.parse(address);
    if (parsed.length != 1) {
        throw new AddressException("Illegal address", address);
    }
    InternetAddress raw = parsed[0];
    try {
        return (getEncoding() != null ? new InternetAddress(raw.getAddress(), raw.getPersonal(), getEncoding()) : raw);
    } catch (UnsupportedEncodingException ex) {
        throw new MessagingException("Failed to parse embedded personal name to correct encoding", ex);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 24 with InternetAddress

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

the class JavaMailSenderTests method javaMailSenderWithMimeMessages.

@Test
public void javaMailSenderWithMimeMessages() throws MessagingException {
    MockJavaMailSender sender = new MockJavaMailSender();
    sender.setHost("host");
    sender.setUsername("username");
    sender.setPassword("password");
    MimeMessage mimeMessage1 = sender.createMimeMessage();
    mimeMessage1.setRecipient(Message.RecipientType.TO, new InternetAddress("he@mail.org"));
    MimeMessage mimeMessage2 = sender.createMimeMessage();
    mimeMessage2.setRecipient(Message.RecipientType.TO, new InternetAddress("she@mail.org"));
    sender.send(mimeMessage1, mimeMessage2);
    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(mimeMessage1, sender.transport.getSentMessage(0));
    assertEquals(mimeMessage2, sender.transport.getSentMessage(1));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test)

Example 25 with InternetAddress

use of javax.mail.internet.InternetAddress 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)

Aggregations

InternetAddress (javax.mail.internet.InternetAddress)255 MimeMessage (javax.mail.internet.MimeMessage)106 MessagingException (javax.mail.MessagingException)69 Session (javax.mail.Session)49 Properties (java.util.Properties)45 ArrayList (java.util.ArrayList)42 Address (javax.mail.Address)41 Message (javax.mail.Message)40 Date (java.util.Date)38 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)36 AddressException (javax.mail.internet.AddressException)34 X509Certificate (java.security.cert.X509Certificate)32 MimeBodyPart (javax.mail.internet.MimeBodyPart)30 Test (org.junit.Test)29 IOException (java.io.IOException)26 MimeMultipart (javax.mail.internet.MimeMultipart)26 PolicyExpression (org.nhindirect.policy.PolicyExpression)18 HashMap (java.util.HashMap)17 CertificateResolver (org.nhindirect.stagent.cert.CertificateResolver)17 PolicyResolver (org.nhindirect.stagent.policy.PolicyResolver)17