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());
}
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());
}
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();
}
}
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:" + $;
}
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);
}
}
Aggregations