", "
");
String text = HtmlHelper.getFullText(body);
String language = HtmlHelper.getLanguage(this, message.subject, text);
String preview = HtmlHelper.getPreview(text);
try {
db.beginTransaction();
message.id = null;
message.folder = sent.id;
message.identity = null;
message.from = helper.getFrom();
message.cc = helper.getCc();
message.bcc = helper.getBcc();
message.reply = helper.getReply();
message.subject = helper.getSubject(); // Subject encryption
message.encrypt = parts.getEncryption();
message.ui_encrypt = message.encrypt;
message.received = new Date().getTime();
message.seen = true;
message.ui_seen = true;
message.ui_hide = true;
message.ui_busy = Long.MAX_VALUE; // Needed to keep messages in user folders
message.error = null;
message.id = db.message().insertMessage(message);
File file = EntityMessage.getFile(this, message.id);
Helper.writeText(file, body);
db.message().setMessageContent(message.id,
true,
language,
parts.isPlainOnly(),
preview,
parts.getWarnings(message.warning));
EntityAttachment.copy(this, id, message.id);
Long size = null;
if (body != null)
size = (long) body.length();
Long total = size;
List attachments = db.attachment().getAttachments(message.id);
for (EntityAttachment attachment : attachments)
if (attachment.size != null)
total = (total == null ? 0 : total) + attachment.size;
db.message().setMessageSize(message.id, size, total);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
sid = message.id;
message.id = id;
}
// Create transport
long start, end;
Long max_size = null;
EmailService iservice = new EmailService(
this, ident.getProtocol(), ident.realm, ident.encryption, ident.insecure, debug);
try {
iservice.setUseIp(ident.use_ip, ident.ehlo);
iservice.setUnicode(ident.unicode);
if (message.receipt_request != null && message.receipt_request) {
int receipt_type = prefs.getInt("receipt_type", 2);
if (receipt_type == 1 || receipt_type == 2) // Delivery receipt
iservice.setDsnNotify("SUCCESS,FAILURE,DELAY");
}
// Connect transport
db.identity().setIdentityState(ident.id, "connecting");
iservice.connect(ident);
if (BuildConfig.DEBUG && false)
throw new IOException("Test");
db.identity().setIdentityState(ident.id, "connected");
if (ident.max_size == null)
max_size = iservice.getMaxSize();
Address[] to = imessage.getAllRecipients();
String via = "via " + ident.host + "/" + ident.user +
" to " + (to == null ? null : TextUtils.join(", ", to));
iservice.setReporter(new TraceOutputStream.IReport() {
private int progress = -1;
private long last = SystemClock.elapsedRealtime();
@Override
public void report(int pos, int total) {
int p = (total == 0 ? 0 : 100 * pos / total);
if (p > progress) {
progress = p;
long now = SystemClock.elapsedRealtime();
if (now > last + PROGRESS_UPDATE_INTERVAL) {
last = now;
lastProgress = progress;
nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService().build());
}
}
}
});
// Send message
EntityLog.log(this, "Sending " + via);
start = new Date().getTime();
iservice.getTransport().sendMessage(imessage, to);
end = new Date().getTime();
EntityLog.log(this, "Sent " + via + " elapse=" + (end - start) + " ms");
} catch (MessagingException ex) {
iservice.dump();
Log.e(ex);
if (ex instanceof SMTPSendFailedException) {
SMTPSendFailedException sem = (SMTPSendFailedException) ex;
ex = new SMTPSendFailedException(
sem.getCommand(),
sem.getReturnCode(),
getString(R.string.title_service_auth, sem.getMessage()),
sem.getNextException(),
sem.getValidSentAddresses(),
sem.getValidUnsentAddresses(),
sem.getInvalidAddresses());
}
if (sid != null)
db.message().deleteMessage(sid);
db.identity().setIdentityError(ident.id, Log.formatThrowable(ex));
throw ex;
} finally {
iservice.close();
if (lastProgress >= 0) {
lastProgress = -1;
nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService().build());
}
db.identity().setIdentityState(ident.id, null);
}
try {
db.beginTransaction();
// Delete from outbox
db.message().deleteMessage(message.id);
// Show in sent folder
if (sid != null) {
db.message().setMessageReceived(sid, start);
db.message().setMessageSent(sid, end);
db.message().setMessageUiHide(sid, false);
}
// Mark replied
if (message.inreplyto != null) {
List replieds = db.message().getMessagesByMsgId(message.account, message.inreplyto);
for (EntityMessage replied : replieds)
EntityOperation.queue(this, replied, EntityOperation.ANSWERED, true);
}
// Mark forwarded
if (message.wasforwardedfrom != null) {
List forwardeds = db.message().getMessagesByMsgId(message.account, message.wasforwardedfrom);
for (EntityMessage forwarded : forwardeds)
EntityOperation.queue(this, forwarded,
EntityOperation.KEYWORD, MessageHelper.FLAG_FORWARDED, true);
}
// Update identity
if (max_size != null)
db.identity().setIdentityMaxSize(ident.id, max_size);
db.identity().setIdentityConnected(ident.id, new Date().getTime());
db.identity().setIdentityError(ident.id, null);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
nm.cancel("send:" + message.id, NotificationHelper.NOTIFICATION_TAGGED);
// Check sent message
if (sid != null) {
try {
db.beginTransaction();
// Message could have been deleted
EntityMessage orphan = db.message().getMessage(sid);
if (orphan != null)
EntityOperation.queue(this, orphan, EntityOperation.EXISTS);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
ServiceSynchronize.eval(this, "sent");
}
static void boot(final Context context) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
EntityLog.log(context, "Boot send service");
DB db = DB.getInstance(context);
EntityFolder outbox = db.folder().getOutbox();
if (outbox != null) {
int operations = db.operation().getOperations(EntityOperation.SEND).size();
if (operations > 0)
start(context);
else {
db.folder().setFolderState(outbox.id, null);
db.folder().setFolderSyncState(outbox.id, null);
}
}
} catch (Throwable ex) {
Log.e(ex);
}
}
});
}
static void start(Context context) {
try {
ContextCompat.startForegroundService(context, new Intent(context, ServiceSend.class));
} catch (Throwable ex) {
Log.e(ex);
}
}
static void stop(Context context) {
context.stopService(new Intent(context, ServiceSend.class));
}
static void schedule(Context context, long delay) {
Intent intent = new Intent(context, ServiceSend.class);
PendingIntent pi = PendingIntentCompat.getForegroundService(
context, PI_SEND, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long trigger = System.currentTimeMillis() + delay;
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi);
AlarmManagerCompatEx.setAndAllowWhileIdle(context, am, AlarmManager.RTC_WAKEUP, trigger, pi);
}
static void watchdog(Context context) {
boot(context);
}
}