Skip to content

Commit

Permalink
Fixing mail sending
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Dec 31, 2022
1 parent c083273 commit cf77339
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions src/main/java/org/luwrain/app/message/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2021 Michael Pozhidaev <[email protected]>
Copyright 2012-2022 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
Expand All @@ -17,6 +17,7 @@
package org.luwrain.app.message;

import java.util.*;
import java.io.*;

import org.luwrain.core.*;
import org.luwrain.core.events.*;
Expand All @@ -27,6 +28,8 @@
import org.luwrain.pim.contacts.*;
import org.luwrain.io.json.*;

import static org.luwrain.pim.mail.BinaryMessage.*;

public final class App extends AppBase<Strings>
{
final Message message;
Expand Down Expand Up @@ -66,79 +69,60 @@ public App(Message message)

boolean send(MailMessage message, boolean useAnotherAccount)
{
NullCheck.notNull(message, "message");
try {
if (useAnotherAccount)
{
final MailAccount account = conv.accountToSend();
if (account == null)
return false;
send(account, message);
return true;
}
} //useAnotherAccount
final MailAccount account;
final MailAccount defaultAccount = mailStoring.getAccounts().getDefault(MailAccount.Type.SMTP);
if (defaultAccount == null)
account = conv.accountToSend(); else
account = defaultAccount;
send(account, message);
return true;
}
catch(PimException e)
{
getLuwrain().crash(e);
return false;
}
}

private void send(MailAccount account, MailMessage message) throws PimException
private void send(MailAccount account, MailMessage message)
{
NullCheck.notNull(account, "account");
NullCheck.notNull(message, "message");
message.setFrom(getFromLine(account));
if (message.getFrom().trim().isEmpty())
throw new RuntimeException("No sender address");//FIXME:
//FIXME: message.setExtInfo(mailStoring.getAccounts().getUniRef(account));
throw new PimException("No sender address");//FIXME:
final MessageSendingData sendingData = new MessageSendingData();
sendingData.setAccountId(mailStoring.getAccounts().getId(account));
message.setExtInfo(sendingData.toString());
fillMessageData(message);
/*
final MailFolder folder = mailStoring.getFolders().findFirstByProperty("defaultOutgoing", "true");
final MailFolder folder = mailStoring.getFolders().findFirstByProperty(MailFolders.PROP_DEFAULT_OUTGOING, "true");
if (folder == null)
throw new RuntimeException("Unable to prepare a folder for pending messages");
throw new PimException("Unable to prepare a folder for pending messages");
mailStoring.getMessages().save(folder, message);
*/
// app.getLuwrain().runWorker(org.luwrain.pim.workers.Smtp.NAME);
final TaskId taskId = newTaskId();

runTask(taskId, ()->{
try {
mailStoring.getAccounts().sendDirectly(account, message);
}
catch(PimException e)
{
crash(e);
return;
}
finishedTask(taskId, ()->closeApp());
});
getLuwrain().runWorker(org.luwrain.pim.workers.Smtp.NAME);
}

private void fillMessageData(MailMessage message) throws PimException
private void fillMessageData(MailMessage message)
{
NullCheck.notNull(message, "message");
message.setSentDate(new Date());
message.setContentType("text/plain; charset=utf-8");//FIXME:
final Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", getUserAgent());
message.setRawMessage(mailStoring.getMessages().toByteArray(message, headers));
try {
message.setRawMessage(toByteArray(message, headers));
}
catch(IOException e)
{
throw new PimException(e);
}
}

Conv getConv() { return this.conv; }
ContactsStoring getContactsStoring() { return this.contactsStoring; }
MailStoring getMailStoring() { return this.mailStoring; }

private String getFromLine(MailAccount account) throws PimException
private String getFromLine(MailAccount account)
{
NullCheck.notNull(account, "account");
final org.luwrain.core.Settings.PersonalInfo sett = org.luwrain.core.Settings.createPersonalInfo(getLuwrain().getRegistry());
final String personal;
final String addr;
Expand Down

0 comments on commit cf77339

Please sign in to comment.