use of javax.mail.Address in project zm-mailbox by Zimbra.
the class DefaultTnefToICalendar method addAttendees.
/**
* @param icalOutput
* @param mimeMsg
* @param partstat
* @param replyWanted
* @throws ParserException
* @throws URISyntaxException
* @throws IOException
* @throws ParseException
* @throws MessagingException
*/
private void addAttendees(ContentHandler icalOutput, MimeMessage mimeMsg, PartStat partstat, boolean replyWanted) throws ParserException, URISyntaxException, IOException, ParseException, MessagingException {
// ATTENDEEs
InternetAddress firstFromIA = null;
String firstFromEmailAddr = null;
// Use for SENT-BY if applicable
String senderMailto = null;
String senderCn = null;
javax.mail.Address[] toRecips = null;
javax.mail.Address[] ccRecips = null;
javax.mail.Address[] bccRecips = null;
javax.mail.Address[] msgFroms = null;
javax.mail.Address msgSender = null;
if (mimeMsg != null) {
toRecips = mimeMsg.getRecipients(javax.mail.Message.RecipientType.TO);
ccRecips = mimeMsg.getRecipients(javax.mail.Message.RecipientType.CC);
bccRecips = mimeMsg.getRecipients(javax.mail.Message.RecipientType.BCC);
msgFroms = mimeMsg.getFrom();
msgSender = mimeMsg.getSender();
}
if (msgFroms != null) {
if (msgFroms.length != 1) {
sLog.debug(msgFroms.length + " From: recipients for " + method.getValue());
}
if (msgFroms.length >= 1) {
firstFromIA = (InternetAddress) msgFroms[0];
firstFromEmailAddr = firstFromIA.getAddress();
}
if (msgSender != null) {
String senderAddr = msgSender.toString();
if (msgSender instanceof InternetAddress) {
InternetAddress senderIA = (InternetAddress) msgSender;
senderAddr = senderIA.getAddress();
senderCn = senderIA.getPersonal();
if (!firstFromIA.equals(senderIA)) {
senderMailto = "Mailto:" + senderAddr;
}
}
}
}
if (method.equals(Method.REPLY) || method.equals(Method.COUNTER)) {
// from ATTENDEE to ORGANIZER
if (toRecips != null) {
if (toRecips.length != 1) {
sLog.debug(toRecips.length + " To: recipients for " + method.getValue());
}
if (toRecips.length >= 1) {
InternetAddress ia = (InternetAddress) toRecips[0];
String email = ia.getAddress();
String displayName = ia.getPersonal();
icalOutput.startProperty(Property.ORGANIZER);
icalOutput.propertyValue("Mailto:" + email);
if (displayName != null) {
icalOutput.parameter(Parameter.CN, displayName);
}
icalOutput.endProperty(Property.ORGANIZER);
}
}
if (firstFromEmailAddr != null) {
String displayName = firstFromIA.getPersonal();
icalOutput.startProperty(Property.ATTENDEE);
icalOutput.propertyValue("Mailto:" + firstFromEmailAddr);
if (displayName != null) {
icalOutput.parameter(Parameter.CN, displayName);
}
icalOutput.parameter(Parameter.CUTYPE, CuType.INDIVIDUAL.getValue());
if (partstat != null) {
icalOutput.parameter(Parameter.PARTSTAT, partstat.getValue());
}
if (senderMailto != null) {
icalOutput.parameter(Parameter.SENT_BY, senderMailto);
}
icalOutput.endProperty(Property.ATTENDEE);
}
} else {
// ORGANIZER to ATTENDEEs - REQUEST or CANCEL
InternetAddress organizerEmail = null;
if (firstFromEmailAddr != null) {
SentBy sentBy = null;
Cn cn = null;
if (senderMailto != null) {
sentBy = new SentBy(senderMailto);
}
organizerEmail = firstFromIA;
String displayName = firstFromIA.getPersonal();
if ((displayName != null) && (!displayName.equals(firstFromEmailAddr))) {
cn = new Cn(displayName);
}
Organizer organizer = new Organizer();
organizer.setValue("Mailto:" + firstFromEmailAddr);
if (cn != null) {
organizer.getParameters().add(cn);
}
if (sentBy != null) {
organizer.getParameters().add(sentBy);
}
IcalUtil.addProperty(icalOutput, organizer);
if (icalType == ICALENDAR_TYPE.VEVENT) {
// Assumption - ORGANIZER is an attendee and is attending.
Attendee attendee = new Attendee("Mailto:" + firstFromEmailAddr);
if (cn != null) {
attendee.getParameters().add(cn);
}
attendee.getParameters().add(CuType.INDIVIDUAL);
attendee.getParameters().add(Role.REQ_PARTICIPANT);
if (!method.equals(Method.CANCEL)) {
PartStat orgPartstat = PartStat.ACCEPTED;
if (ccRecips != null) {
for (Address a : ccRecips) {
InternetAddress ia = (InternetAddress) a;
if (organizerEmail.equals(ia)) {
orgPartstat = PartStat.TENTATIVE;
break;
}
}
}
attendee.getParameters().add(orgPartstat);
}
// Was including SENT-BY but probably not appropriate
// for a request
IcalUtil.addProperty(icalOutput, attendee);
}
}
if (toRecips != null) {
for (Address a : toRecips) {
InternetAddress ia = (InternetAddress) a;
if ((organizerEmail != null) && organizerEmail.equals(ia)) {
// No need to add the information twice
continue;
}
addAttendee(icalOutput, ia, Role.REQ_PARTICIPANT, CuType.INDIVIDUAL, partstat, replyWanted);
}
}
if (ccRecips != null) {
for (Address a : ccRecips) {
InternetAddress ia = (InternetAddress) a;
if ((organizerEmail != null) && organizerEmail.equals(ia)) {
// No need to add the information twice
continue;
}
addAttendee(icalOutput, ia, Role.OPT_PARTICIPANT, CuType.INDIVIDUAL, partstat, replyWanted);
}
}
if (bccRecips != null) {
for (Address a : bccRecips) {
InternetAddress ia = (InternetAddress) a;
addAttendee(icalOutput, ia, Role.NON_PARTICIPANT, CuType.RESOURCE, partstat, replyWanted);
}
}
}
if (senderMailto != null) {
XProperty msOlkSender = new XProperty("X-MS-OLK-SENDER", senderMailto);
if (senderCn != null) {
Cn cn = new Cn(senderCn);
msOlkSender.getParameters().add(cn);
}
IcalUtil.addProperty(icalOutput, msOlkSender);
}
}
use of javax.mail.Address in project Openfire by igniterealtime.
the class EmailService method sendMessage.
/**
* Sends a message, specifying all of its fields.<p>
*
* To have more advanced control over the message sent, use the
* {@link #sendMessage(MimeMessage)} method.<p>
*
* Both a plain text and html body can be specified. If one of the values is null,
* only the other body type is sent. If both body values are set, a multi-part
* message will be sent. If parts of the message are invalid (ie, the toEmail is null)
* the message won't be sent.
*
* @param toName the name of the recipient of this email.
* @param toEmail the email address of the recipient of this email.
* @param fromName the name of the sender of this email.
* @param fromEmail the email address of the sender of this email.
* @param subject the subject of the email.
* @param textBody plain text body of the email, which can be {@code null} if the
* html body is not null.
* @param htmlBody html body of the email, which can be {@code null} if the text body
* is not null.
*/
public void sendMessage(String toName, String toEmail, String fromName, String fromEmail, String subject, String textBody, String htmlBody) {
// Check for errors in the given fields:
if (toEmail == null || fromEmail == null || subject == null || (textBody == null && htmlBody == null)) {
Log.error("Error sending email: Invalid fields: " + ((toEmail == null) ? "toEmail " : "") + ((fromEmail == null) ? "fromEmail " : "") + ((subject == null) ? "subject " : "") + ((textBody == null && htmlBody == null) ? "textBody or htmlBody " : ""));
} else {
try {
String encoding = MimeUtility.mimeCharset("UTF-8");
MimeMessage message = createMimeMessage();
Address to;
Address from;
if (toName != null) {
to = new InternetAddress(toEmail, toName, encoding);
} else {
to = new InternetAddress(toEmail, "", encoding);
}
if (fromName != null) {
from = new InternetAddress(fromEmail, fromName, encoding);
} else {
from = new InternetAddress(fromEmail, "", encoding);
}
// Set the date of the message to be the current date
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", java.util.Locale.US);
format.setTimeZone(JiveGlobals.getTimeZone());
message.setHeader("Date", format.format(new Date()));
message.setHeader("Content-Transfer-Encoding", "8bit");
message.setRecipient(Message.RecipientType.TO, to);
message.setFrom(from);
message.setSubject(StringUtils.replace(subject, "\n", ""), encoding);
// Create HTML, plain-text, or combination message
if (textBody != null && htmlBody != null) {
MimeMultipart content = new MimeMultipart("alternative");
// Plain-text
MimeBodyPart text = new MimeBodyPart();
text.setText(textBody, encoding);
text.setDisposition(Part.INLINE);
content.addBodyPart(text);
// HTML
MimeBodyPart html = new MimeBodyPart();
html.setContent(htmlBody, "text/html; charset=UTF-8");
html.setDisposition(Part.INLINE);
html.setHeader("Content-Transfer-Encoding", "8bit");
content.addBodyPart(html);
// Add multipart to message.
message.setContent(content);
message.setDisposition(Part.INLINE);
sendMessage(message);
} else if (textBody != null) {
MimeBodyPart bPart = new MimeBodyPart();
bPart.setText(textBody, encoding);
bPart.setDisposition(Part.INLINE);
bPart.setHeader("Content-Transfer-Encoding", "8bit");
MimeMultipart mPart = new MimeMultipart();
mPart.addBodyPart(bPart);
message.setContent(mPart);
message.setDisposition(Part.INLINE);
// Add the message to the send list
sendMessage(message);
} else if (htmlBody != null) {
MimeBodyPart bPart = new MimeBodyPart();
bPart.setContent(htmlBody, "text/html; charset=UTF-8");
bPart.setDisposition(Part.INLINE);
bPart.setHeader("Content-Transfer-Encoding", "8bit");
MimeMultipart mPart = new MimeMultipart();
mPart.addBodyPart(bPart);
message.setContent(mPart);
message.setDisposition(Part.INLINE);
// Add the message to the send list
sendMessage(message);
}
} catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
}
use of javax.mail.Address in project ats-framework by Axway.
the class MimePackage method getRecipients.
/**
* Get the recipients of the specified type
*
* @param recipientType
* the type of recipient - to, cc or bcc
* @return array with recipients, emtpy array of no recipients of this type
* are present
* @throws PackageException
*/
@PublicAtsApi
public String[] getRecipients(RecipientType recipientType) throws PackageException {
try {
Address[] recipientAddresses = message.getRecipients(recipientType.toJavamailType());
// return an empty string if no recipients are present
if (recipientAddresses == null) {
return new String[] {};
}
String[] recipients = new String[recipientAddresses.length];
for (int i = 0; i < recipientAddresses.length; i++) {
recipients[i] = recipientAddresses[i].toString();
}
return recipients;
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of javax.mail.Address in project quickstart by wildfly.
the class Email method send.
/**
* Method to send the email based upon values entered in the JSF view. Exception should be handled in a production usage but
* is not handled in this example.
*
* @throws Exception
*/
public void send() throws Exception {
Message message = new MimeMessage(mySession);
message.setFrom(new InternetAddress(from));
Address toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setContent(body, "text/plain");
Transport.send(message);
}
use of javax.mail.Address in project pentaho-kettle by pentaho.
the class ParseMailInputTest method beforeTest.
@Before
public void beforeTest() throws MessagingException, IOException, KettleException {
message = Mockito.mock(Message.class);
MailConnection conn = mock(MailConnection.class);
when(conn.getMessageBody(any(Message.class))).thenReturn(MSG_BODY);
when(conn.getFolderName()).thenReturn(FLD_NAME);
when(conn.getAttachedFilesCount(any(Message.class), any(Pattern.class))).thenReturn(ATTCH_COUNT);
when(conn.getMessageBodyContentType(any(Message.class))).thenReturn(CNTNT_TYPE);
data = mock(MailInputData.class);
data.mailConn = conn;
mailInput = new MailInput(stepMockHelper.stepMeta, data, 0, stepMockHelper.transMeta, stepMockHelper.trans);
Address addrFrom1 = mock(Address.class);
when(addrFrom1.toString()).thenReturn(FROM1);
Address addrFrom2 = mock(Address.class);
when(addrFrom2.toString()).thenReturn(FROM2);
Address addrRep1 = mock(Address.class);
when(addrRep1.toString()).thenReturn(REP1);
Address addrRep2 = mock(Address.class);
when(addrRep2.toString()).thenReturn(REP2);
Address allRec1 = mock(Address.class);
when(allRec1.toString()).thenReturn(REC1);
Address allRec2 = mock(Address.class);
when(allRec2.toString()).thenReturn(REC2);
Address[] adrFr = { addrFrom1, addrFrom2 };
Address[] adrRep = { addrRep1, addrRep2 };
Address[] adrRecip = { allRec1, allRec2 };
message = Mockito.mock(Message.class);
when(message.getMessageNumber()).thenReturn(MSG_NUMB);
when(message.getSubject()).thenReturn(SUBJ);
when(message.getFrom()).thenReturn(adrFr);
when(message.getReplyTo()).thenReturn(adrRep);
when(message.getAllRecipients()).thenReturn(adrRecip);
when(message.getDescription()).thenReturn(DESC);
when(message.getReceivedDate()).thenReturn(DATE1);
when(message.getSentDate()).thenReturn(DATE2);
when(message.getContentType()).thenReturn(CNTNT_TYPE_EMAIL);
when(message.getSize()).thenReturn(CNTNT_SIZE);
Header ex1 = new Header(HDR_EX1, HDR_EX1V);
Header ex2 = new Header(HDR_EX2, HDR_EX2V);
// for fixed [PDI-6532]
when(message.getMatchingHeaders(AdditionalMatchers.aryEq(new String[] { HDR_EX1 }))).thenReturn(getEnum(new Header[] { ex1 }));
when(message.getMatchingHeaders(AdditionalMatchers.aryEq(new String[] { HDR_EX2 }))).thenReturn(getEnum(new Header[] { ex2 }));
when(message.getMatchingHeaders(AdditionalMatchers.aryEq(new String[] { HDR_EX1, HDR_EX2 }))).thenReturn(getEnum(new Header[] { ex1, ex2 }));
// for previous implementation
when(message.getHeader(eq(HDR_EX1))).thenReturn(new String[] { ex1.getValue() });
when(message.getHeader(eq(HDR_EX2))).thenReturn(new String[] { ex2.getValue() });
}
Aggregations