Асинхронная рассылка.

This commit is contained in:
2023-11-07 19:28:38 +03:00
parent 4523575b74
commit 895da62639
2 changed files with 75 additions and 70 deletions

5
.idea/workspace.xml generated
View File

@@ -8,10 +8,7 @@
<component name="ChangeListManager">
<list default="true" id="e42177c3-2328-4b27-8a01-35779b2beb99" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/SapforTestingSystem/Json/SapforTasksResults_json.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/SapforTestingSystem/Json/SapforTasksResults_json.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/SapforTestingSystem/SapforTasksPackage/SapforTasksPackage.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/SapforTestingSystem/SapforTasksPackage/SapforTasksPackage.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/SapforTestingSystem/SapforTasksPackage/SapforTasksPackageInterface.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/SapforTestingSystem/SapforTasksPackage/SapforTasksPackageInterface.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Visual_DVM_2021/UI/Main/SapforPackagesComparisonForm.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Visual_DVM_2021/UI/Main/SapforPackagesComparisonForm.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Repository/RepositoryServer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Repository/RepositoryServer.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />

View File

@@ -82,73 +82,81 @@ public abstract class RepositoryServer<D extends Database> {
}
}
public void Email(EmailMessage message_in, File... directAttachements) throws Exception {
System.out.println("EMAIL STARTED");
Properties props = new Properties();
props.put("mail.smtp.host", Global.properties.SMTPHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", String.valueOf(Global.properties.SMTPPort));
props.put("mail.smtp.socketFactory.port", String.valueOf(Global.properties.MailSocketPort));
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.connectiontimeout", String.valueOf(Global.properties.SocketTimeout));
props.put("mail.smtp.timeout", String.valueOf(Global.properties.SocketTimeout));
props.put("mail.smtp.writetimeout", String.valueOf(Global.properties.SocketTimeout));
//------------------------------
LinkedHashMap<String, File> innerFiles = new LinkedHashMap<>();
for (String aName : message_in.files.keySet()) {
File f = Utils.getTempFileName(aName);
Utils.unpackFile(message_in.files.get(aName), f);
innerFiles.put(aName, f);
}
Vector<String> targets_ = new Vector<>();
targets_.add(Constants.MailAddress); //себе.
targets_.addAll(message_in.targets);
//------------------------------
Session session = Session.getDefaultInstance(props,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
Constants.MailAddress,
Constants.MailPassword);
}
});
for (String target : targets_) {
System.out.println("target=" + target);
if (needsEmail(target)) {
System.out.println("needs email");
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(Constants.MailAddress));
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(target));
message.setSubject(message_in.subject);
Multipart multipart = new MimeMultipart();
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText(message_in.text);
multipart.addBodyPart(textBodyPart);
for (String aName : innerFiles.keySet()) {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(innerFiles.get(aName));
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(aName);
multipart.addBodyPart(attachmentBodyPart);
}
for (File f : directAttachements) {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(f);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(f.getName());
multipart.addBodyPart(attachmentBodyPart);
}
message.setContent(multipart);
Transport.send(message);
System.out.println("message sent");
} catch (Exception ex) {
ex.printStackTrace();
Print(ex.getMessage());
Thread thread = new Thread(() -> {
System.out.println("EMAIL THREAD STARTED");
try {
Properties props = new Properties();
props.put("mail.smtp.host", Global.properties.SMTPHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", String.valueOf(Global.properties.SMTPPort));
props.put("mail.smtp.socketFactory.port", String.valueOf(Global.properties.MailSocketPort));
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.connectiontimeout", String.valueOf(Global.properties.SocketTimeout));
props.put("mail.smtp.timeout", String.valueOf(Global.properties.SocketTimeout));
props.put("mail.smtp.writetimeout", String.valueOf(Global.properties.SocketTimeout));
//------------------------------
LinkedHashMap<String, File> innerFiles = new LinkedHashMap<>();
for (String aName : message_in.files.keySet()) {
File f = Utils.getTempFileName(aName);
Utils.unpackFile(message_in.files.get(aName), f);
innerFiles.put(aName, f);
}
} else System.out.println("does not need email");
}
System.out.println("EMAIL ENDED");
Vector<String> targets_ = new Vector<>();
targets_.add(Constants.MailAddress); //себе.
targets_.addAll(message_in.targets);
//------------------------------
Session session = Session.getDefaultInstance(props,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
Constants.MailAddress,
Constants.MailPassword);
}
});
for (String target : targets_) {
System.out.println("target=" + target);
if (needsEmail(target)) {
System.out.println("needs email");
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(Constants.MailAddress));
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(target));
message.setSubject(message_in.subject);
Multipart multipart = new MimeMultipart();
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText(message_in.text);
multipart.addBodyPart(textBodyPart);
for (String aName : innerFiles.keySet()) {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(innerFiles.get(aName));
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(aName);
multipart.addBodyPart(attachmentBodyPart);
}
for (File f : directAttachements) {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(f);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(f.getName());
multipart.addBodyPart(attachmentBodyPart);
}
message.setContent(multipart);
Transport.send(message);
System.out.println("message sent");
} catch (Exception ex) {
System.out.println("Исключение во время отправки сообщения абоненту " + Utils.Brackets(target));
ex.printStackTrace();
}
} else System.out.println("does not need email");
}
} catch (Exception ex) {
System.out.println("Исключение во время выполнения рассылки.");
ex.printStackTrace();
}
System.out.println("EMAIL THREAD ENDED");
});
thread.start();
}
public boolean needsEmail(String email) {
return true;