下面列出了怎么用javax.mail.MessageRemovedException的API类实例代码及写法,或者点击链接到github查看源代码。
private static void onSeen(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
// Mark message (un)seen
DB db = DB.getInstance(context);
if (!ifolder.getPermanentFlags().contains(Flags.Flag.SEEN)) {
db.message().setMessageSeen(message.id, false);
db.message().setMessageUiSeen(message.id, false);
return;
}
boolean seen = jargs.getBoolean(0);
if (message.seen.equals(seen))
return;
Message imessage = ifolder.getMessageByUID(message.uid);
if (imessage == null)
throw new MessageRemovedException();
imessage.setFlag(Flags.Flag.SEEN, seen);
db.message().setMessageSeen(message.id, seen);
}
private static void onFlag(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
// Star/unstar message
DB db = DB.getInstance(context);
if (!ifolder.getPermanentFlags().contains(Flags.Flag.FLAGGED)) {
db.message().setMessageFlagged(message.id, false);
db.message().setMessageUiFlagged(message.id, false, null);
return;
}
boolean flagged = jargs.getBoolean(0);
if (message.flagged.equals(flagged))
return;
Message imessage = ifolder.getMessageByUID(message.uid);
if (imessage == null)
throw new MessageRemovedException();
imessage.setFlag(Flags.Flag.FLAGGED, flagged);
db.message().setMessageFlagged(message.id, flagged);
}
private static void onAnswered(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
// Mark message (un)answered
DB db = DB.getInstance(context);
if (!ifolder.getPermanentFlags().contains(Flags.Flag.ANSWERED)) {
db.message().setMessageAnswered(message.id, false);
db.message().setMessageUiAnswered(message.id, false);
return;
}
boolean answered = jargs.getBoolean(0);
if (message.answered.equals(answered))
return;
// This will be fixed when moving the message
if (message.uid == null)
return;
Message imessage = ifolder.getMessageByUID(message.uid);
if (imessage == null)
throw new MessageRemovedException();
imessage.setFlag(Flags.Flag.ANSWERED, answered);
db.message().setMessageAnswered(message.id, answered);
}
private static void onKeyword(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
// Set/reset user flag
DB db = DB.getInstance(context);
// https://tools.ietf.org/html/rfc3501#section-2.3.2
String keyword = jargs.getString(0);
boolean set = jargs.getBoolean(1);
if (TextUtils.isEmpty(keyword))
throw new IllegalArgumentException("keyword/empty");
if (!ifolder.getPermanentFlags().contains(Flags.Flag.USER)) {
db.message().setMessageKeywords(message.id, DB.Converters.fromStringArray(null));
return;
}
if (message.uid == null)
throw new IllegalArgumentException("keyword/uid");
Message imessage = ifolder.getMessageByUID(message.uid);
if (imessage == null)
throw new MessageRemovedException();
Flags flags = new Flags(keyword);
imessage.setFlags(flags, set);
}
private static void onBody(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException {
// Download message body
DB db = DB.getInstance(context);
if (message.content)
return;
// Get message
Message imessage = ifolder.getMessageByUID(message.uid);
if (imessage == null)
throw new MessageRemovedException();
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
MessageHelper.MessageParts parts = helper.getMessageParts();
String body = parts.getHtml(context);
File file = message.getFile(context);
Helper.writeText(file, body);
db.message().setMessageContent(message.id,
true,
HtmlHelper.getLanguage(context, body),
parts.isPlainOnly(),
HtmlHelper.getPreview(body),
parts.getWarnings(message.warning));
}
/**
* Will register message sending modules dynamically. This method is used to bind the notification sending
* modules in to msg mgt component
*
* @param module MessageSendingModule
*/
protected void addNotificationSendingModule(NotificationSendingModule module) throws MessageRemovedException {
ModuleConfiguration moduleConfiguration;
if (StringUtils.isEmpty(module.getModuleName())) {
if (log.isDebugEnabled()) {
log.debug("Cannot register module without a valid module name");
}
return;
}
if (log.isDebugEnabled()) {
log.debug("Registering a message sending module " + module.getModuleName());
}
if (configBuilder != null) {
moduleConfiguration = configBuilder.getModuleConfigurations(module.getModuleName());
} else {
moduleConfiguration = new ModuleConfiguration();
}
try {
module.init(moduleConfiguration);
notificationSendingModules.add(module);
} catch (NotificationManagementException e) {
log.error("Error while initializing Notification sending module " + module.getModuleName(), e);
}
}
private static void onHeaders(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException {
// Download headers
DB db = DB.getInstance(context);
if (message.headers != null)
return;
IMAPMessage imessage = (IMAPMessage) ifolder.getMessageByUID(message.uid);
if (imessage == null)
throw new MessageRemovedException();
MessageHelper helper = new MessageHelper(imessage, context);
db.message().setMessageHeaders(message.id, helper.getHeaders());
}
private static void onRaw(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException, JSONException {
// Download raw message
DB db = DB.getInstance(context);
if (message.raw == null || !message.raw) {
IMAPMessage imessage = (IMAPMessage) ifolder.getMessageByUID(message.uid);
if (imessage == null)
throw new MessageRemovedException();
File file = message.getRawFile(context);
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
imessage.writeTo(os);
}
db.message().setMessageRaw(message.id, true);
}
if (jargs.length() > 0) {
// Cross account move
long tid = jargs.getLong(0);
EntityFolder target = db.folder().getFolder(tid);
if (target == null)
throw new FolderNotFoundException();
Log.i(folder.name + " queuing ADD id=" + message.id + ":" + target.id);
EntityOperation operation = new EntityOperation();
operation.account = target.account;
operation.folder = target.id;
operation.message = message.id;
operation.name = EntityOperation.ADD;
operation.args = jargs.toString();
operation.created = new Date().getTime();
operation.id = db.operation().insertOperation(operation);
}
}
private static void onAttachment(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws JSONException, MessagingException, IOException {
// Download attachment
DB db = DB.getInstance(context);
long id = jargs.getLong(0);
// Get attachment
EntityAttachment attachment = db.attachment().getAttachment(id);
if (attachment == null)
attachment = db.attachment().getAttachment(message.id, (int) id); // legacy
if (attachment == null)
throw new IllegalArgumentException("Local attachment not found");
if (attachment.available)
return;
// Get message
Message imessage = ifolder.getMessageByUID(message.uid);
if (imessage == null)
throw new MessageRemovedException();
// Get message parts
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
MessageHelper.MessageParts parts = helper.getMessageParts();
// Download attachment
parts.downloadAttachment(context, attachment);
}
/**
* Will register message sending modules dynamically. This method is used to bind the notification sending
* modules in to msg mgt component
*
* @param module MessageSendingModule
*/
@Reference(
name = "ldap.tenant.manager.listener.service",
service = NotificationSendingModule.class,
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
unbind = "removeNotificationSendingModule")
protected void addNotificationSendingModule(NotificationSendingModule module) throws MessageRemovedException {
ModuleConfiguration moduleConfiguration;
if (StringUtils.isEmpty(module.getModuleName())) {
if (log.isDebugEnabled()) {
log.debug("Cannot register module without a valid module name");
}
return;
}
if (log.isDebugEnabled()) {
log.debug("Registering a message sending module " + module.getModuleName());
}
if (configBuilder != null) {
moduleConfiguration = configBuilder.getModuleConfigurations(module.getModuleName());
} else {
moduleConfiguration = new ModuleConfiguration();
}
try {
module.init(moduleConfiguration);
notificationSendingModules.add(module);
} catch (NotificationManagementException e) {
log.error("Error while initializing Notification sending module " + module.getModuleName(), e);
}
}
static String formatThrowable(Throwable ex, String separator, boolean sanitize) {
if (sanitize) {
if (ex instanceof MessageRemovedException)
return null;
if (ex instanceof AuthenticationFailedException &&
ex.getCause() instanceof SocketException)
return null;
if (ex instanceof MessagingException &&
("connection failure".equals(ex.getMessage()) ||
"failed to create new store connection".equals(ex.getMessage())))
return null;
if (ex instanceof MessagingException &&
ex.getCause() instanceof ConnectionException &&
ex.getCause().getMessage() != null &&
(ex.getCause().getMessage().contains("Read error") ||
ex.getCause().getMessage().contains("Write error") ||
ex.getCause().getMessage().contains("Unexpected end of ZLIB input stream") ||
ex.getCause().getMessage().contains("Socket is closed")))
return null;
// javax.mail.MessagingException: AU3 BAD User is authenticated but not connected.;
// nested exception is:
// com.sun.mail.iap.BadCommandException: AU3 BAD User is authenticated but not connected.
// javax.mail.MessagingException: AU3 BAD User is authenticated but not connected.;
// nested exception is:
// com.sun.mail.iap.BadCommandException: AU3 BAD User is authenticated but not connected.
// at com.sun.mail.imap.IMAPFolder.logoutAndThrow(SourceFile:1156)
// at com.sun.mail.imap.IMAPFolder.open(SourceFile:1063)
// at com.sun.mail.imap.IMAPFolder.open(SourceFile:977)
// at eu.faircode.email.ServiceSynchronize.monitorAccount(SourceFile:890)
// at eu.faircode.email.ServiceSynchronize.access$1500(SourceFile:85)
// at eu.faircode.email.ServiceSynchronize$7$1.run(SourceFile:627)
// at java.lang.Thread.run(Thread.java:764)
// Caused by: com.sun.mail.iap.BadCommandException: AU3 BAD User is authenticated but not connected.
// at com.sun.mail.iap.Protocol.handleResult(SourceFile:415)
// at com.sun.mail.imap.protocol.IMAPProtocol.select(SourceFile:1230)
// at com.sun.mail.imap.IMAPFolder.open(SourceFile:1034)
if (ex instanceof MessagingException &&
ex.getCause() instanceof BadCommandException &&
ex.getCause().getMessage() != null &&
ex.getCause().getMessage().contains("User is authenticated but not connected"))
return null;
if (ex instanceof IOException &&
ex.getCause() instanceof MessageRemovedException)
return null;
if (ex instanceof ConnectionException)
return null;
if (ex instanceof StoreClosedException ||
ex instanceof FolderClosedException || ex instanceof FolderClosedIOException)
return null;
if (ex instanceof IllegalStateException &&
("Not connected".equals(ex.getMessage()) ||
"This operation is not allowed on a closed folder".equals(ex.getMessage())))
return null;
}
StringBuilder sb = new StringBuilder();
if (BuildConfig.DEBUG)
sb.append(ex.toString());
else
sb.append(ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());
Throwable cause = ex.getCause();
while (cause != null) {
if (BuildConfig.DEBUG)
sb.append(separator).append(cause.toString());
else
sb.append(separator).append(cause.getMessage() == null ? cause.getClass().getName() : cause.getMessage());
cause = cause.getCause();
}
return sb.toString();
}
/**
* This method read verification e-mail from Gmail inbox and returns the verification URL.
*
* @return verification redirection URL.
* @throws Exception
*/
public static String readGmailInboxForVerification() throws Exception {
boolean isEmailVerified = false;
long waitTime = 10000;
String pointBrowserURL = "";
Properties props = new Properties();
props.load(new FileInputStream(new File(
TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator
+ "smtp.properties")));
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString());
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_WRITE);
Thread.sleep(waitTime);
long startTime = System.currentTimeMillis();
long endTime = 0;
int count = 1;
while (endTime - startTime < 180000 && !isEmailVerified) {
Message[] messages = inbox.getMessages();
for (Message message : messages) {
if (!message.isExpunged()) {
try {
log.info("Mail Subject:- " + message.getSubject());
if (message.getSubject().contains("EmailVerification")) {
pointBrowserURL = getBodyFromMessage(message);
isEmailVerified = true;
}
// Optional : deleting the mail
message.setFlag(Flags.Flag.DELETED, true);
} catch (MessageRemovedException e){
log.error("Could not read the message subject. Message is removed from inbox");
}
}
}
endTime = System.currentTimeMillis();
Thread.sleep(waitTime);
endTime += count*waitTime;
count++;
}
inbox.close(true);
store.close();
return pointBrowserURL;
}
/**
* This method read e-mails from Gmail inbox and find whether the notification of particular type is found.
*
* @param notificationType Notification types supported by publisher and store.
* @return whether email is found for particular type.
* @throws Exception
*/
public static boolean readGmailInboxForNotification(String notificationType) throws Exception {
boolean isNotificationMailAvailable = false;
long waitTime = 10000;
Properties props = new Properties();
props.load(new FileInputStream(new File(
TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator
+ "smtp.properties")));
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString());
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_WRITE);
Thread.sleep(waitTime);
long startTime = System.currentTimeMillis();
long endTime = 0;
int count = 1;
while (endTime - startTime < 180000 && !isNotificationMailAvailable) {
Message[] messages = inbox.getMessages();
for (Message message : messages) {
if(!message.isExpunged()) {
try {
log.info("Mail Subject:- " + message.getSubject());
if (message.getSubject().contains(notificationType)) {
isNotificationMailAvailable = true;
}
// Optional : deleting the mail
message.setFlag(Flags.Flag.DELETED, true);
} catch (MessageRemovedException e) {
log.error("Could not read the message subject. Message is removed from inbox");
}
}
}
endTime = System.currentTimeMillis();
Thread.sleep(waitTime);
endTime += count*waitTime;
count++;
}
inbox.close(true);
store.close();
return isNotificationMailAvailable;
}
/**
* This method read verification e-mail from Gmail inbox and returns the verification URL.
*
* @return verification redirection URL.
* @throws Exception
*/
public static String readGmailInboxForVerification() throws Exception {
boolean isEmailVerified = false;
long waitTime = 10000;
String pointBrowserURL = "";
Properties props = new Properties();
props.load(new FileInputStream(new File(
TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator
+ "smtp.properties")));
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString());
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_WRITE);
Thread.sleep(waitTime);
long startTime = System.currentTimeMillis();
long endTime = 0;
int count = 1;
while (endTime - startTime < 180000 && !isEmailVerified) {
Message[] messages = inbox.getMessages();
for (Message message : messages) {
if (!message.isExpunged()) {
try {
log.info("Mail Subject:- " + message.getSubject());
if (message.getSubject().contains("EmailVerification")) {
pointBrowserURL = getBodyFromMessage(message);
isEmailVerified = true;
}
// Optional : deleting the mail
message.setFlag(Flags.Flag.DELETED, true);
} catch (MessageRemovedException e){
log.error("Could not read the message subject. Message is removed from inbox");
}
}
}
endTime = System.currentTimeMillis();
Thread.sleep(waitTime);
endTime += count*waitTime;
count++;
}
inbox.close(true);
store.close();
return pointBrowserURL;
}
/**
* This method read e-mails from Gmail inbox and find whether the notification of particular type is found.
*
* @param notificationType Notification types supported by publisher and store.
* @return whether email is found for particular type.
* @throws Exception
*/
public static boolean readGmailInboxForNotification(String notificationType) throws Exception {
boolean isNotificationMailAvailable = false;
long waitTime = 10000;
Properties props = new Properties();
props.load(new FileInputStream(new File(
TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator
+ "smtp.properties")));
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString());
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_WRITE);
Thread.sleep(waitTime);
long startTime = System.currentTimeMillis();
long endTime = 0;
int count = 1;
while (endTime - startTime < 180000 && !isNotificationMailAvailable) {
Message[] messages = inbox.getMessages();
for (Message message : messages) {
if(!message.isExpunged()) {
try {
log.info("Mail Subject:- " + message.getSubject());
if (message.getSubject().contains(notificationType)) {
isNotificationMailAvailable = true;
}
// Optional : deleting the mail
message.setFlag(Flags.Flag.DELETED, true);
} catch (MessageRemovedException e) {
log.error("Could not read the message subject. Message is removed from inbox");
}
}
}
endTime = System.currentTimeMillis();
Thread.sleep(waitTime);
endTime += count*waitTime;
count++;
}
inbox.close(true);
store.close();
return isNotificationMailAvailable;
}