| 35641 |
aman |
1 |
package com.spice.profitmandi.common.services;
|
|
|
2 |
|
| 37477 |
amit |
3 |
import com.spice.profitmandi.common.mail.MailQueue;
|
|
|
4 |
import com.spice.profitmandi.common.mail.MailRequest;
|
|
|
5 |
import com.spice.profitmandi.common.mail.MailSenderType;
|
|
|
6 |
import com.spice.profitmandi.common.util.Utils;
|
|
|
7 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
8 |
import org.springframework.core.io.FileSystemResource;
|
| 35641 |
aman |
9 |
import org.springframework.core.io.InputStreamSource;
|
|
|
10 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
11 |
import org.springframework.stereotype.Service;
|
|
|
12 |
|
|
|
13 |
import java.io.File;
|
|
|
14 |
import java.io.Serializable;
|
| 37477 |
amit |
15 |
import java.util.ArrayList;
|
| 35641 |
aman |
16 |
import java.util.List;
|
|
|
17 |
import java.util.Map;
|
|
|
18 |
|
| 37477 |
amit |
19 |
/**
|
|
|
20 |
* Deprecated. Every method here now hands the mail to {@link MailQueue} and
|
|
|
21 |
* returns; nothing is sent inline any more.
|
|
|
22 |
*
|
|
|
23 |
* This class used to build its own MimeMessage, hardcode
|
|
|
24 |
* From: noreply@smartdukaan.com in five places, and send straight down whichever
|
|
|
25 |
* JavaMailSender the caller happened to pass. That From is the reason mail was
|
|
|
26 |
* being rejected: an authenticated Workspace session may only send as the
|
|
|
27 |
* account it logged in as, and noreply@ is a different identity in the same
|
|
|
28 |
* domain. It also meant a failed send was simply lost, and took the calling job
|
|
|
29 |
* down with it.
|
|
|
30 |
*
|
|
|
31 |
* The signatures are kept so the ~11 existing callers keep compiling. The
|
|
|
32 |
* JavaMailSender argument is ignored — choosing a transport is not the caller's
|
|
|
33 |
* job. New code should use {@link MailQueue} directly.
|
|
|
34 |
*/
|
|
|
35 |
@Deprecated
|
| 35641 |
aman |
36 |
@Service
|
|
|
37 |
public class EmailServiceImpl implements EmailService {
|
|
|
38 |
|
| 37477 |
amit |
39 |
private final MailQueue mailQueue;
|
| 35641 |
aman |
40 |
|
| 37477 |
amit |
41 |
@Autowired
|
|
|
42 |
public EmailServiceImpl(MailQueue mailQueue) {
|
|
|
43 |
this.mailQueue = mailQueue;
|
| 35641 |
aman |
44 |
}
|
|
|
45 |
|
|
|
46 |
@Override
|
| 37477 |
amit |
47 |
public void sendMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc,
|
|
|
48 |
String subject, String body) {
|
|
|
49 |
send(emailTo, cc, null, subject, body, true, null);
|
| 35641 |
aman |
50 |
}
|
|
|
51 |
|
|
|
52 |
@Override
|
| 37477 |
amit |
53 |
public void sendMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc,
|
|
|
54 |
String subject, String body, List<File> attachments) {
|
|
|
55 |
send(emailTo, cc, null, subject, body, true, fromFiles(attachments));
|
| 35641 |
aman |
56 |
}
|
|
|
57 |
|
|
|
58 |
@Override
|
| 37477 |
amit |
59 |
public void sendMailWithAttachments(JavaMailSender ignored, String emailTo, String[] cc,
|
|
|
60 |
String subject, String body, List<File> attachments) {
|
|
|
61 |
send(new String[]{emailTo}, cc, null, subject, body, true, fromFiles(attachments));
|
| 35641 |
aman |
62 |
}
|
|
|
63 |
|
|
|
64 |
@Override
|
| 37477 |
amit |
65 |
public void sendMailWithAttachment(JavaMailSender ignored, String[] emailTo, String[] cc,
|
|
|
66 |
String subject, String body,
|
|
|
67 |
String fileName, InputStreamSource inputStreamSource) {
|
|
|
68 |
send(emailTo, cc, null, subject, body, true,
|
|
|
69 |
new Utils.Attachment[]{new Utils.Attachment(fileName, inputStreamSource)});
|
| 35641 |
aman |
70 |
}
|
|
|
71 |
|
|
|
72 |
@Override
|
| 37477 |
amit |
73 |
public void sendMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc, String[] bcc,
|
|
|
74 |
String subject, String body, boolean html,
|
|
|
75 |
Attachment... attachments) {
|
|
|
76 |
send(emailTo, cc, bcc, subject, body, html, convert(attachments));
|
| 35641 |
aman |
77 |
}
|
|
|
78 |
|
|
|
79 |
@Override
|
| 37477 |
amit |
80 |
public void sendHtmlMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc,
|
|
|
81 |
String subject, String body, Attachment... attachments) {
|
|
|
82 |
send(emailTo, cc, null, subject, body, true, convert(attachments));
|
| 35641 |
aman |
83 |
}
|
|
|
84 |
|
| 37477 |
amit |
85 |
/**
|
|
|
86 |
* Inline images are not carried through the outbox — it stores attachments,
|
|
|
87 |
* not CID-referenced parts. The images are attached normally instead, so the
|
|
|
88 |
* mail still arrives with its content rather than being dropped.
|
|
|
89 |
*/
|
| 35641 |
aman |
90 |
@Override
|
| 37477 |
amit |
91 |
public void sendEmbeddedHtmlMail(JavaMailSender ignored, String[] emailTo, String[] cc,
|
|
|
92 |
String subject, String body,
|
|
|
93 |
Map<? extends Serializable, File> inlineImages) {
|
|
|
94 |
List<Utils.Attachment> images = new ArrayList<>();
|
| 35641 |
aman |
95 |
if (inlineImages != null) {
|
|
|
96 |
for (Map.Entry<? extends Serializable, File> entry : inlineImages.entrySet()) {
|
| 37477 |
amit |
97 |
File file = entry.getValue();
|
|
|
98 |
if (file != null) {
|
|
|
99 |
images.add(new Utils.Attachment(file.getName(), new FileSystemResource(file)));
|
|
|
100 |
}
|
| 35641 |
aman |
101 |
}
|
|
|
102 |
}
|
| 37477 |
amit |
103 |
send(emailTo, cc, null, subject, body, true, images.toArray(new Utils.Attachment[0]));
|
|
|
104 |
}
|
| 35641 |
aman |
105 |
|
| 37477 |
amit |
106 |
private void send(String[] to, String[] cc, String[] bcc, String subject, String body,
|
|
|
107 |
boolean html, Utils.Attachment[] attachments) {
|
|
|
108 |
MailRequest request = MailRequest.to(to)
|
|
|
109 |
.cc(cc)
|
|
|
110 |
.bcc(bcc)
|
|
|
111 |
.subject(subject)
|
|
|
112 |
.from(MailSenderType.RELAY)
|
|
|
113 |
.source("EmailService");
|
|
|
114 |
if (html) {
|
|
|
115 |
request.html(body);
|
|
|
116 |
} else {
|
|
|
117 |
request.body(body);
|
|
|
118 |
}
|
|
|
119 |
if (attachments != null && attachments.length > 0) {
|
|
|
120 |
request.attach(attachments);
|
|
|
121 |
}
|
|
|
122 |
mailQueue.queue(request);
|
|
|
123 |
}
|
| 35641 |
aman |
124 |
|
| 37477 |
amit |
125 |
private Utils.Attachment[] fromFiles(List<File> files) {
|
|
|
126 |
if (files == null || files.isEmpty()) {
|
|
|
127 |
return null;
|
| 35641 |
aman |
128 |
}
|
| 37477 |
amit |
129 |
List<Utils.Attachment> converted = new ArrayList<>(files.size());
|
|
|
130 |
for (File file : files) {
|
|
|
131 |
if (file != null) {
|
|
|
132 |
converted.add(new Utils.Attachment(file.getName(), new FileSystemResource(file)));
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
return converted.toArray(new Utils.Attachment[0]);
|
| 35641 |
aman |
136 |
}
|
|
|
137 |
|
| 37477 |
amit |
138 |
private Utils.Attachment[] convert(Attachment[] attachments) {
|
|
|
139 |
if (attachments == null || attachments.length == 0) {
|
|
|
140 |
return null;
|
| 35641 |
aman |
141 |
}
|
| 37477 |
amit |
142 |
Utils.Attachment[] converted = new Utils.Attachment[attachments.length];
|
|
|
143 |
for (int i = 0; i < attachments.length; i++) {
|
|
|
144 |
converted[i] = new Utils.Attachment(
|
|
|
145 |
attachments[i].getFileName(), attachments[i].getInputStreamSource());
|
|
|
146 |
}
|
|
|
147 |
return converted;
|
| 35641 |
aman |
148 |
}
|
|
|
149 |
}
|