Skip to content

Commit 9ab75e9

Browse files
committed
added all currently possible email type conversions to the EmailConverter
1 parent 5c725da commit 9ab75e9

File tree

1 file changed

+149
-79
lines changed

1 file changed

+149
-79
lines changed

src/main/java/org/simplejavamail/converter/EmailConverter.java

Lines changed: 149 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ private EmailConverter() {
4242
// util / helper class
4343
}
4444

45+
/*
46+
To Email instance
47+
*/
48+
4549
/**
46-
* @param mimeMessage The MimeMessage from which to create the email.
50+
* @param mimeMessage The MimeMessage from which to create the {@link Email}.
4751
*/
4852
public static Email mimeMessageToEmail(@Nonnull final MimeMessage mimeMessage) {
4953
final Email email = new Email(false);
@@ -56,7 +60,7 @@ public static Email mimeMessageToEmail(@Nonnull final MimeMessage mimeMessage) {
5660
}
5761

5862
/**
59-
* @param msgData The content of an Outlook (.msg) message from which to create the email.
63+
* @param msgData The content of an Outlook (.msg) message from which to create the {@link Email}.
6064
*/
6165
public static Email outlookMsgToEmail(@Nonnull final String msgData) {
6266
final Email email = new Email(false);
@@ -66,7 +70,7 @@ public static Email outlookMsgToEmail(@Nonnull final String msgData) {
6670
}
6771

6872
/**
69-
* @param msgfile The content of an Outlook (.msg) message from which to create the email.
73+
* @param msgfile The content of an Outlook (.msg) message from which to create the {@link Email}.
7074
*/
7175
public static Email outlookMsgToEmail(@Nonnull final File msgfile) {
7276
final Email email = new Email(false);
@@ -76,7 +80,7 @@ public static Email outlookMsgToEmail(@Nonnull final File msgfile) {
7680
}
7781

7882
/**
79-
* @param msgInputStream The content of an Outlook (.msg) message from which to create the email.
83+
* @param msgInputStream The content of an Outlook (.msg) message from which to create the {@link Email}.
8084
*/
8185
public static Email outlookMsgToEmail(@Nonnull final InputStream msgInputStream) {
8286
final Email email = new Email(false);
@@ -85,6 +89,147 @@ public static Email outlookMsgToEmail(@Nonnull final InputStream msgInputStream)
8589
return email;
8690
}
8791

92+
/**
93+
* Delegates to {@link #emlToMimeMessage(Session, String)} using a dummy {@link Session} instance and passes the result to {@link
94+
* #mimeMessageToEmail(MimeMessage)};
95+
*/
96+
public static Email emlToEmail(@Nonnull final String eml) {
97+
final MimeMessage mimeMessage = emlToMimeMessage(createDummySession(), checkNonEmptyArgument(eml, "eml"));
98+
return mimeMessageToEmail(mimeMessage);
99+
}
100+
101+
/*
102+
To MimeMessage instance
103+
*/
104+
105+
/**
106+
* @return Result of {@link #outlookMsgToEmail(String)} and {@link #emailToMimeMessage(Email)}
107+
*/
108+
@Nonnull
109+
public static MimeMessage outlookMsgToMimeMessage(@Nonnull final String outlookMsgData) {
110+
checkNonEmptyArgument(outlookMsgData, "outlookMsgData");
111+
return emailToMimeMessage(outlookMsgToEmail(outlookMsgData));
112+
}
113+
114+
/**
115+
* @return Result of {@link #outlookMsgToEmail(File)} and {@link #emailToMimeMessage(Email)}
116+
*/
117+
@Nonnull
118+
public static MimeMessage outlookMsgToMimeMessage(@Nonnull final File outlookMsgFile) {
119+
checkNonEmptyArgument(outlookMsgFile, "outlookMsgFile");
120+
return emailToMimeMessage(outlookMsgToEmail(outlookMsgFile));
121+
}
122+
123+
/**
124+
* @return Result of {@link #outlookMsgToEmail(InputStream)} and {@link #emailToMimeMessage(Email)}
125+
*/
126+
@Nonnull
127+
public static MimeMessage outlookMsgToMimeMessage(@Nonnull final InputStream outloookMsgInputStream) {
128+
checkNonEmptyArgument(outloookMsgInputStream, "outloookMsgInputStream");
129+
return emailToMimeMessage(outlookMsgToEmail(outloookMsgInputStream));
130+
}
131+
132+
/**
133+
* Delegates to {@link #emailToMimeMessage(Email, Session)}, using a new empty {@link Session} instance.
134+
*
135+
* @see #emailToMimeMessage(Email, Session)
136+
*/
137+
public static MimeMessage emailToMimeMessage(@Nonnull final Email email) {
138+
return emailToMimeMessage(checkNonEmptyArgument(email, "email"), createDummySession());
139+
}
140+
141+
/**
142+
* Refer to {@link MimeMessageHelper#produceMimeMessage(Email, Session)}
143+
*/
144+
public static MimeMessage emailToMimeMessage(@Nonnull final Email email, @Nonnull final Session session) {
145+
try {
146+
return produceMimeMessage(checkNonEmptyArgument(email, "email"), checkNonEmptyArgument(session, "session"));
147+
} catch (UnsupportedEncodingException | MessagingException e) {
148+
// this should never happen, so we don't acknowledge this exception (and simply bubble up)
149+
throw new RuntimeException(e.getMessage(), e);
150+
}
151+
}
152+
153+
/**
154+
* Delegates to {@link #emlToMimeMessage(Session, String)} with an empty {@link Session} instance.
155+
*
156+
* @see #emailToMimeMessage(Email, Session)
157+
*/
158+
public static MimeMessage emlToMimeMessage(@Nonnull final String eml) {
159+
return emlToMimeMessage(createDummySession(), checkNonEmptyArgument(eml, "eml"));
160+
}
161+
162+
/**
163+
* Relies on JavaMail's native parser of EML data, {@link MimeMessage#MimeMessage(Session, InputStream)}.
164+
*/
165+
public static MimeMessage emlToMimeMessage(@Nonnull final Session session, @Nonnull final String eml) {
166+
checkNonEmptyArgument(session, "session");
167+
checkNonEmptyArgument(eml, "eml");
168+
try {
169+
return new MimeMessage(session, new ByteArrayInputStream(eml.getBytes(UTF_8)));
170+
} catch (final MessagingException e) {
171+
throw new EmailConverterException(format(EmailConverterException.PARSE_ERROR_EML, e.getMessage()), e);
172+
}
173+
}
174+
175+
/*
176+
To EML String
177+
*/
178+
179+
/**
180+
* @return The result of {@link MimeMessage#writeTo(OutputStream)} which should be in the standard EML format.
181+
*/
182+
public static String mimeMessageToEML(@Nonnull final MimeMessage mimeMessage) {
183+
final ByteArrayOutputStream os = new ByteArrayOutputStream();
184+
try {
185+
checkNonEmptyArgument(mimeMessage, "mimeMessage").writeTo(os);
186+
return os.toString(UTF_8.name());
187+
} catch (IOException | MessagingException e) {
188+
// this should never happen, so we don't acknowledge this exception (and simply bubble up)
189+
throw new RuntimeException(e.getMessage(), e);
190+
}
191+
}
192+
193+
/**
194+
* Delegates to {@link #emailToMimeMessage(Email)} and passes the result to {@link #mimeMessageToEML(MimeMessage)}.
195+
*
196+
* @see #emailToMimeMessage(Email, Session)
197+
*/
198+
public static String emailToEML(@Nonnull final Email email) {
199+
return mimeMessageToEML(emailToMimeMessage(checkNonEmptyArgument(email, "email")));
200+
}
201+
202+
/**
203+
* @return Result of {@link #outlookMsgToEmail(String)} and {@link #emailToEML(Email)}
204+
*/
205+
@Nonnull
206+
public static String outlookMsgToEML(@Nonnull final String outlookMsgData) {
207+
checkNonEmptyArgument(outlookMsgData, "outlookMsgData");
208+
return emailToEML(outlookMsgToEmail(outlookMsgData));
209+
}
210+
211+
/**
212+
* @return Result of {@link #outlookMsgToEmail(File)} and {@link #emailToEML(Email)}
213+
*/
214+
@Nonnull
215+
public static String outlookMsgToEML(@Nonnull final File outlookMsgFile) {
216+
checkNonEmptyArgument(outlookMsgFile, "outlookMsgFile");
217+
return emailToEML(outlookMsgToEmail(outlookMsgFile));
218+
}
219+
220+
/**
221+
* @return Result of {@link #outlookMsgToEmail(InputStream)} and {@link #emailToEML(Email)}
222+
*/
223+
@Nonnull
224+
public static String outlookMsgToEML(@Nonnull final InputStream outloookMsgInputStream) {
225+
checkNonEmptyArgument(outloookMsgInputStream, "outloookMsgInputStream");
226+
return emailToEML(outlookMsgToEmail(outloookMsgInputStream));
227+
}
228+
229+
/*
230+
Helpers
231+
*/
232+
88233
private static void fillEmailFromMimeMessage(@Nonnull final Email email, @Nonnull final MimeMessage mimeMessage)
89234
throws MessagingException, IOException {
90235
checkNonEmptyArgument(email, "email");
@@ -147,81 +292,6 @@ private static void fillEmailFromOutlookMessage(@Nonnull final Email email, @Non
147292
}
148293
}
149294

150-
/**
151-
* Delegates to {@link #emailToMimeMessage(Email, Session)}, using a new empty {@link Session} instance.
152-
*
153-
* @see #emailToMimeMessage(Email, Session)
154-
*/
155-
public static MimeMessage emailToMimeMessage(@Nonnull final Email email) {
156-
return emailToMimeMessage(checkNonEmptyArgument(email, "email"), createDummySession());
157-
}
158-
159-
/**
160-
* Refer to {@link MimeMessageHelper#produceMimeMessage(Email, Session)}
161-
*/
162-
public static MimeMessage emailToMimeMessage(@Nonnull final Email email, @Nonnull final Session session) {
163-
try {
164-
return produceMimeMessage(checkNonEmptyArgument(email, "email"), checkNonEmptyArgument(session, "session"));
165-
} catch (UnsupportedEncodingException | MessagingException e) {
166-
// this should never happen, so we don't acknowledge this exception (and simply bubble up)
167-
throw new RuntimeException(e.getMessage(), e);
168-
}
169-
}
170-
171-
/**
172-
* @return The result of {@link MimeMessage#writeTo(OutputStream)} which should be in the standard EML format.
173-
*/
174-
public static String mimeMessageToEML(@Nonnull final MimeMessage mimeMessage) {
175-
final ByteArrayOutputStream os = new ByteArrayOutputStream();
176-
try {
177-
checkNonEmptyArgument(mimeMessage, "mimeMessage").writeTo(os);
178-
return os.toString(UTF_8.name());
179-
} catch (IOException | MessagingException e) {
180-
// this should never happen, so we don't acknowledge this exception (and simply bubble up)
181-
throw new RuntimeException(e.getMessage(), e);
182-
}
183-
}
184-
185-
/**
186-
* Delegates to {@link #emailToMimeMessage(Email)} and passes the result to {@link #mimeMessageToEML(MimeMessage)}.
187-
*
188-
* @see #emailToMimeMessage(Email, Session)
189-
*/
190-
public static String emailToEML(@Nonnull final Email email) {
191-
return mimeMessageToEML(emailToMimeMessage(checkNonEmptyArgument(email, "email")));
192-
}
193-
194-
/**
195-
* Delegates to {@link #emlToMimeMessage(Session, String)} with an empty {@link Session} instance.
196-
*
197-
* @see #emailToMimeMessage(Email, Session)
198-
*/
199-
public static MimeMessage emlToMimeMessage(@Nonnull final String eml) {
200-
return emlToMimeMessage(createDummySession(), checkNonEmptyArgument(eml, "eml"));
201-
}
202-
203-
/**
204-
* Relies on JavaMail's native parser of EML data, {@link MimeMessage#MimeMessage(Session, InputStream)}.
205-
*/
206-
public static MimeMessage emlToMimeMessage(@Nonnull final Session session, @Nonnull final String eml) {
207-
checkNonEmptyArgument(session, "session");
208-
checkNonEmptyArgument(eml, "eml");
209-
try {
210-
return new MimeMessage(session, new ByteArrayInputStream(eml.getBytes(UTF_8)));
211-
} catch (final MessagingException e) {
212-
throw new EmailConverterException(format(EmailConverterException.PARSE_ERROR_EML, e.getMessage()), e);
213-
}
214-
}
215-
216-
/**
217-
* Delegates to {@link #emlToMimeMessage(Session, String)} using a dummy {@link Session} instance and passes the result to {@link
218-
* #mimeMessageToEmail(MimeMessage)};
219-
*/
220-
public static Email emlToEmail(@Nonnull final String eml) {
221-
final MimeMessage mimeMessage = emlToMimeMessage(createDummySession(), checkNonEmptyArgument(eml, "eml"));
222-
return mimeMessageToEmail(mimeMessage);
223-
}
224-
225295
private static Session createDummySession() {
226296
return Session.getDefaultInstance(new Properties());
227297
}

0 commit comments

Comments
 (0)