attachment problem resolved

This commit is contained in:
2024-11-06 19:52:31 +05:30
parent 603a7278e2
commit 9cf823b469
3 changed files with 26 additions and 10 deletions

View File

@@ -26,6 +26,7 @@ import java.util.Properties;
import javax.mail.Folder; import javax.mail.Folder;
import javax.mail.Session; import javax.mail.Session;
import javax.mail.Store; import javax.mail.Store;
import javax.mail.Part;
public class EmailSendingService extends Service<Void> { public class EmailSendingService extends Service<Void> {
@@ -90,6 +91,7 @@ public class EmailSendingService extends Service<Void> {
DataSource source = new FileDataSource(file.getAbsolutePath()); DataSource source = new FileDataSource(file.getAbsolutePath());
mimeBodyPart.setDataHandler(new DataHandler(source)); mimeBodyPart.setDataHandler(new DataHandler(source));
mimeBodyPart.setFileName(file.getName()); mimeBodyPart.setFileName(file.getName());
mimeBodyPart.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(mimeBodyPart); multipart.addBodyPart(mimeBodyPart);
} }
} }

View File

@@ -20,6 +20,8 @@ import javax.mail.internet.MimeBodyPart;
import com.secure_mailer.frontend.EmailMessage; import com.secure_mailer.frontend.EmailMessage;
import javafx.concurrent.Service; import javafx.concurrent.Service;
import javafx.concurrent.Task; import javafx.concurrent.Task;
import javax.mail.Part;
public class MessageRendererService extends Service<Object>{ public class MessageRendererService extends Service<Object>{
private EmailMessage emailMessage; private EmailMessage emailMessage;
@@ -71,7 +73,13 @@ public class MessageRendererService extends Service<Object>{
for (int i = multipart.getCount() - 1; i >=0; i--) { for (int i = multipart.getCount() - 1; i >=0; i--) {
BodyPart bodyPart = multipart.getBodyPart(i); BodyPart bodyPart = multipart.getBodyPart(i);
String contentType = bodyPart.getContentType(); String contentType = bodyPart.getContentType();
if (isSimpleType(contentType)) {
// Check if it is an attachment
String disposition = bodyPart.getDisposition();
if (disposition != null && disposition.equalsIgnoreCase(Part.ATTACHMENT) ) {
MimeBodyPart mbp = (MimeBodyPart) bodyPart;
if (!emailMessage.isAttachmentLoaded())emailMessage.addAttachment(mbp);
} else if (isSimpleType(contentType)) {
if( emailMessage.getIsAuthenticated() ) { if( emailMessage.getIsAuthenticated() ) {
stringBuffer.append(bodyPart.getContent().toString()); stringBuffer.append(bodyPart.getContent().toString());
@@ -83,9 +91,7 @@ public class MessageRendererService extends Service<Object>{
} else if (isMultipartType(contentType)){ } else if (isMultipartType(contentType)){
Multipart multipart2 = (Multipart) bodyPart.getContent(); Multipart multipart2 = (Multipart) bodyPart.getContent();
loadMultipart(multipart2,stringBuffer); loadMultipart(multipart2,stringBuffer);
} else if (!isTextPlain(contentType)) { // } else if (!isTextPlain(contentType)) {
MimeBodyPart mbp = (MimeBodyPart) bodyPart;
if (!emailMessage.isAttachmentLoaded())emailMessage.addAttachment(mbp);
} }
} }
} }

View File

@@ -65,6 +65,8 @@ import javax.mail.BodyPart;
import javax.mail.Message; import javax.mail.Message;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.Multipart; import javax.mail.Multipart;
import javax.mail.Part;
import java.util.List; import java.util.List;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
@@ -718,20 +720,26 @@ public class MainWindowController extends BaseController implements Initializabl
private String loadMultipart(Multipart multipart) throws MessagingException, IOException { private String loadMultipart(Multipart multipart) throws MessagingException, IOException {
String msg = ""; String msg = "";
for (int i = 0; i < multipart.getCount() ; i++) { for (int i = multipart.getCount() - 1; i >= 0 ; i--) {
BodyPart bodyPart = multipart.getBodyPart(i); BodyPart bodyPart = multipart.getBodyPart(i);
String contentType = bodyPart.getContentType(); String contentType = bodyPart.getContentType();
if (isSimpleType(contentType)) {
// Check if it is an attachment
String disposition = bodyPart.getDisposition();
if (disposition != null && disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
} else if (isSimpleType(contentType)) {
msg = bodyPart.getContent().toString(); msg = bodyPart.getContent().toString();
} else if (isMultipartType(contentType)){ } else if (isMultipartType(contentType)){
Multipart multipart2 = (Multipart) bodyPart.getContent(); Multipart multipart2 = (Multipart) bodyPart.getContent();
msg = loadMultipart(multipart2); msg = loadMultipart(multipart2);
} else if (!isTextPlain(contentType)) {
// MimeBodyPart mbp = (MimeBodyPart) bodyPart;
// if (!emailMessage.isAttachmentLoaded())emailMessage.addAttachment(mbp);
} }
// } else if (!isTextPlain(contentType)) {
//// MimeBodyPart mbp = (MimeBodyPart) bodyPart;
//// if (!emailMessage.isAttachmentLoaded())emailMessage.addAttachment(mbp);
// }
} }
return msg; return msg;