Search in sources :

Example 71 with InternetAddress

use of javax.mail.internet.InternetAddress in project opennms by OpenNMS.

the class JavaMailAckReaderIT method workingWithMultiPartMessages.

/**
     * tests the ability to create acknowledgments from an email for a multi-part text.  This test
     * creates a message from scratch rather than reading from an inbox.  This message creation
     * may not actually represent what comes from a mail server.
     */
@Test
public void workingWithMultiPartMessages() throws JavaMailerException, MessagingException {
    List<Message> msgs = new ArrayList<Message>();
    Properties props = new Properties();
    Message msg = new MimeMessage(Session.getDefaultInstance(props));
    Address[] addrs = new Address[1];
    addrs[0] = new InternetAddress("david@opennms.org");
    msg.addFrom(addrs);
    msg.addRecipient(RecipientType.TO, new InternetAddress("david@opennms.org"));
    msg.setSubject("Re: Notice #1234 JavaMailReaderImplTest Test Message");
    Multipart mpContent = new MimeMultipart();
    BodyPart textBp = new MimeBodyPart();
    BodyPart htmlBp = new MimeBodyPart();
    textBp.setText("ack");
    htmlBp.setContent("<html>\n" + " <head>\n" + "  <title>\n" + "   Acknowledge\n" + "  </title>\n" + " </head>\n" + " <body>\n" + "  <h1>\n" + "   ack\n" + "  </h1>\n" + " </body>\n" + "</html>", "text/html");
    mpContent.addBodyPart(textBp);
    mpContent.addBodyPart(htmlBp);
    msg.setContent(mpContent);
    msgs.add(msg);
    List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
    Assert.assertEquals(1, acks.size());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
    Assert.assertEquals("david@opennms.org", acks.get(0).getAckUser());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
    Assert.assertEquals(new Integer(1234), acks.get(0).getRefId());
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 72 with InternetAddress

use of javax.mail.internet.InternetAddress in project opennms by OpenNMS.

the class JavaMailAckReaderIT method workingWithSimpleTextMessages.

/**
     * tests the ability to create acknowledgments from an email for plain text.  This test
     * creates a message from scratch rather than reading from an inbox. 
     */
@Test
public void workingWithSimpleTextMessages() {
    Properties props = new Properties();
    Message msg = new MimeMessage(Session.getDefaultInstance(props));
    try {
        Address[] addrs = new Address[1];
        addrs[0] = new InternetAddress("david@opennms.org");
        msg.addFrom(addrs);
        msg.addRecipient(javax.mail.internet.MimeMessage.RecipientType.TO, addrs[0]);
        msg.setSubject("Re: Notice #1234 JavaMailReaderImplTest Test Message");
        msg.setText("ACK");
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    List<Message> msgs = new ArrayList<Message>(1);
    msgs.add(msg);
    List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
    Assert.assertEquals(1, acks.size());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
    Assert.assertEquals("david@opennms.org", acks.get(0).getAckUser());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
    Assert.assertEquals(new Integer(1234), acks.get(0).getRefId());
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) Test(org.junit.Test)

Example 73 with InternetAddress

use of javax.mail.internet.InternetAddress in project pratilipi by Pratilipi.

the class EmailUtil method sendMail.

public static void sendMail(String senderName, String senderEmail, InternetAddress[] recipients, InternetAddress[] cc, String subject, String body) throws UnexpectedServerException {
    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(senderEmail, senderName));
        msg.addRecipients(Message.RecipientType.TO, recipients);
        msg.addRecipients(Message.RecipientType.CC, cc);
        // TODO: Uncomment when we migrate to other email service providers
        //			msg.addRecipient( Message.RecipientType.BCC, new InternetAddress( "mail-archive@pratilipi.com", "Mail Archive" ) );
        msg.setSubject(subject);
        msg.setContent(body, "text/html; charset=UTF-8");
        logger.log(Level.INFO, "Sending mail");
        Transport.send(msg);
    } catch (UnsupportedEncodingException | MessagingException e) {
        //			logger.log( Level.SEVERE, "Failed to send mail to " + recipientEmail + ".", e );
        throw new UnexpectedServerException();
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 74 with InternetAddress

use of javax.mail.internet.InternetAddress in project Smartcity-Smarthouse by TechnionYP5777.

the class Communicate method throughEmail.

/**
     * based on:
     * http://www.javatpoint.com/example-of-sending-email-using-java-mail-api-through-gmail-server
     * 
     * @param a
     *            an Authentactor of the fromMail email address param
     * @return a string representing the state of the request, or null if the
     *         request failed
     */
public static String throughEmail(final String fromMail, final Authenticator a, final String $, final String msg) {
    final Properties properties = System.getProperties();
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.socketFactory.port", "465");
    properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.port", "465");
    final Session session = Session.getDefaultInstance(properties, a);
    try {
        final MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromMail));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress($));
        message.setSubject("SmartHouse Alert!");
        message.setText(msg);
        // Send message
        Transport.send(message);
    } catch (final MessagingException mex) {
        log.error("A messaging error occurred", mex);
        return null;
    }
    return "The e-mail was sent to:" + $;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Session(javax.mail.Session)

Example 75 with InternetAddress

use of javax.mail.internet.InternetAddress in project zm-mailbox by Zimbra.

the class JavaMailInternetAddress method parseHeader.

public static InternetAddress[] parseHeader(String addresslist, boolean strict) throws AddressException {
    if (ZPARSER) {
        List<com.zimbra.common.mime.InternetAddress> addrs = com.zimbra.common.mime.InternetAddress.parseHeader(addresslist);
        InternetAddress[] jmaddrs = new InternetAddress[addrs.size()];
        for (int i = 0; i < addrs.size(); i++) {
            InternetAddress jmaddr = new JavaMailInternetAddress(addrs.get(i), true);
            if (strict) {
                jmaddr.validate();
            }
            jmaddrs[i] = jmaddr;
        }
        return jmaddrs;
    } else {
        return InternetAddress.parseHeader(addresslist, strict);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress)

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