You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MailService.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.service;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Date;
  21. import java.util.List;
  22. import java.util.Properties;
  23. import java.util.Queue;
  24. import java.util.UUID;
  25. import java.util.concurrent.ConcurrentLinkedQueue;
  26. import java.util.regex.Pattern;
  27. import javax.activation.DataHandler;
  28. import javax.activation.FileDataSource;
  29. import javax.mail.Authenticator;
  30. import javax.mail.Message;
  31. import javax.mail.MessagingException;
  32. import javax.mail.PasswordAuthentication;
  33. import javax.mail.SendFailedException;
  34. import javax.mail.Session;
  35. import javax.mail.Transport;
  36. import javax.mail.internet.InternetAddress;
  37. import javax.mail.internet.MimeBodyPart;
  38. import javax.mail.internet.MimeMessage;
  39. import javax.mail.internet.MimeMultipart;
  40. import javax.mail.internet.MimeUtility;
  41. import org.slf4j.Logger;
  42. import org.slf4j.LoggerFactory;
  43. import com.gitblit.IStoredSettings;
  44. import com.gitblit.Keys;
  45. import com.gitblit.models.Mailing;
  46. import com.gitblit.utils.StringUtils;
  47. /**
  48. * The mail service handles sending email messages asynchronously from a queue.
  49. *
  50. * @author James Moger
  51. *
  52. */
  53. public class MailService implements Runnable {
  54. private final Logger logger = LoggerFactory.getLogger(MailService.class);
  55. private final Queue<Message> queue = new ConcurrentLinkedQueue<Message>();
  56. private final Session session;
  57. private final IStoredSettings settings;
  58. public MailService(IStoredSettings settings) {
  59. this.settings = settings;
  60. final String mailUser = settings.getString(Keys.mail.username, null);
  61. final String mailPassword = settings.getString(Keys.mail.password, null);
  62. final boolean smtps = settings.getBoolean(Keys.mail.smtps, false);
  63. final boolean starttls = settings.getBoolean(Keys.mail.starttls, false);
  64. boolean authenticate = !StringUtils.isEmpty(mailUser) && !StringUtils.isEmpty(mailPassword);
  65. String server = settings.getString(Keys.mail.server, "");
  66. if (StringUtils.isEmpty(server)) {
  67. session = null;
  68. return;
  69. }
  70. int port = settings.getInteger(Keys.mail.port, 25);
  71. boolean isGMail = false;
  72. if (server.equals("smtp.gmail.com")) {
  73. port = 465;
  74. isGMail = true;
  75. }
  76. Properties props = new Properties();
  77. props.setProperty("mail.smtp.host", server);
  78. props.setProperty("mail.smtp.port", String.valueOf(port));
  79. props.setProperty("mail.smtp.auth", String.valueOf(authenticate));
  80. props.setProperty("mail.smtp.auths", String.valueOf(authenticate));
  81. props.setProperty("mail.smtp.starttls.enable", String.valueOf(starttls));
  82. if (isGMail || smtps) {
  83. props.setProperty("mail.smtp.starttls.enable", "true");
  84. props.put("mail.smtp.socketFactory.port", String.valueOf(port));
  85. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  86. props.put("mail.smtp.socketFactory.fallback", "false");
  87. }
  88. if (!StringUtils.isEmpty(mailUser) && !StringUtils.isEmpty(mailPassword)) {
  89. // SMTP requires authentication
  90. session = Session.getInstance(props, new Authenticator() {
  91. @Override
  92. protected PasswordAuthentication getPasswordAuthentication() {
  93. PasswordAuthentication passwordAuthentication = new PasswordAuthentication(
  94. mailUser, mailPassword);
  95. return passwordAuthentication;
  96. }
  97. });
  98. } else {
  99. // SMTP does not require authentication
  100. session = Session.getInstance(props);
  101. }
  102. }
  103. /**
  104. * Indicates if the mail executor can send emails.
  105. *
  106. * @return true if the mail executor is ready to send emails
  107. */
  108. public boolean isReady() {
  109. return session != null;
  110. }
  111. /**
  112. * Create a message.
  113. *
  114. * @param mailing
  115. * @return a message
  116. */
  117. public Message createMessage(Mailing mailing) {
  118. if (mailing.subject == null) {
  119. mailing.subject = "";
  120. }
  121. if (mailing.content == null) {
  122. mailing.content = "";
  123. }
  124. Message message = new MailMessageImpl(session, mailing.id);
  125. try {
  126. String fromAddress = settings.getString(Keys.mail.fromAddress, null);
  127. if (StringUtils.isEmpty(fromAddress)) {
  128. fromAddress = "gitblit@gitblit.com";
  129. }
  130. InternetAddress from = new InternetAddress(fromAddress, mailing.from == null ? "Gitblit" : mailing.from,"utf-8");
  131. message.setFrom(from);
  132. Pattern validEmail = Pattern
  133. .compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
  134. // validate & add TO recipients
  135. List<InternetAddress> to = new ArrayList<InternetAddress>();
  136. for (String address : mailing.toAddresses) {
  137. if (StringUtils.isEmpty(address)) {
  138. continue;
  139. }
  140. if (validEmail.matcher(address).find()) {
  141. try {
  142. to.add(new InternetAddress(address));
  143. } catch (Throwable t) {
  144. }
  145. }
  146. }
  147. // validate & add CC recipients
  148. List<InternetAddress> cc = new ArrayList<InternetAddress>();
  149. for (String address : mailing.ccAddresses) {
  150. if (StringUtils.isEmpty(address)) {
  151. continue;
  152. }
  153. if (validEmail.matcher(address).find()) {
  154. try {
  155. cc.add(new InternetAddress(address));
  156. } catch (Throwable t) {
  157. }
  158. }
  159. }
  160. if (settings.getBoolean(Keys.web.showEmailAddresses, true)) {
  161. // full disclosure of recipients
  162. if (to.size() > 0) {
  163. message.setRecipients(Message.RecipientType.TO,
  164. to.toArray(new InternetAddress[to.size()]));
  165. }
  166. if (cc.size() > 0) {
  167. message.setRecipients(Message.RecipientType.CC,
  168. cc.toArray(new InternetAddress[cc.size()]));
  169. }
  170. } else {
  171. // everyone is bcc'd
  172. List<InternetAddress> bcc = new ArrayList<InternetAddress>();
  173. bcc.addAll(to);
  174. bcc.addAll(cc);
  175. message.setRecipients(Message.RecipientType.BCC,
  176. bcc.toArray(new InternetAddress[bcc.size()]));
  177. }
  178. message.setSentDate(new Date());
  179. // UTF-8 encode
  180. message.setSubject(MimeUtility.encodeText(mailing.subject, "utf-8", "B"));
  181. MimeBodyPart messagePart = new MimeBodyPart();
  182. messagePart.setText(mailing.content, "utf-8");
  183. //messagePart.setHeader("Content-Transfer-Encoding", "quoted-printable");
  184. if (Mailing.Type.html == mailing.type) {
  185. messagePart.setHeader("Content-Type", "text/html; charset=\"utf-8\"");
  186. } else {
  187. messagePart.setHeader("Content-Type", "text/plain; charset=\"utf-8\"");
  188. }
  189. MimeMultipart multiPart = new MimeMultipart();
  190. multiPart.addBodyPart(messagePart);
  191. // handle attachments
  192. if (mailing.hasAttachments()) {
  193. for (File file : mailing.attachments) {
  194. if (file.exists()) {
  195. MimeBodyPart filePart = new MimeBodyPart();
  196. FileDataSource fds = new FileDataSource(file);
  197. filePart.setDataHandler(new DataHandler(fds));
  198. filePart.setFileName(fds.getName());
  199. multiPart.addBodyPart(filePart);
  200. }
  201. }
  202. }
  203. message.setContent(multiPart);
  204. } catch (Exception e) {
  205. logger.error("Failed to properly create message", e);
  206. }
  207. return message;
  208. }
  209. /**
  210. * Returns the status of the mail queue.
  211. *
  212. * @return true, if the queue is empty
  213. */
  214. public boolean hasEmptyQueue() {
  215. return queue.isEmpty();
  216. }
  217. /**
  218. * Queue's an email message to be sent.
  219. *
  220. * @param message
  221. * @return true if the message was queued
  222. */
  223. public boolean queue(Message message) {
  224. if (!isReady()) {
  225. return false;
  226. }
  227. try {
  228. message.saveChanges();
  229. } catch (Throwable t) {
  230. logger.error("Failed to save changes to message!", t);
  231. }
  232. queue.add(message);
  233. return true;
  234. }
  235. @Override
  236. public void run() {
  237. if (!queue.isEmpty()) {
  238. if (session != null) {
  239. // send message via mail server
  240. List<Message> failures = new ArrayList<Message>();
  241. Message message = null;
  242. while ((message = queue.poll()) != null) {
  243. try {
  244. if (settings.getBoolean(Keys.mail.debug, false)) {
  245. logger.info("send: '" + StringUtils.trimString(message.getSubject(), 60)
  246. + "' to:" + StringUtils.trimString(Arrays.toString(message.getAllRecipients()), 300));
  247. }
  248. Transport.send(message);
  249. } catch (SendFailedException sfe) {
  250. if (settings.getBoolean(Keys.mail.debug, false)) {
  251. logger.error("Failed to send message: {}", sfe.getMessage());
  252. logger.info(" Invalid addresses: {}", Arrays.toString(sfe.getInvalidAddresses()));
  253. logger.info(" Valid sent addresses: {}", Arrays.toString(sfe.getValidSentAddresses()));
  254. logger.info(" Valid unset addresses: {}", Arrays.toString(sfe.getValidUnsentAddresses()));
  255. logger.info("", sfe);
  256. }
  257. else {
  258. logger.error("Failed to send message: {}", sfe.getMessage(), sfe.getNextException());
  259. }
  260. failures.add(message);
  261. } catch (Throwable e) {
  262. logger.error("Failed to send message", e);
  263. failures.add(message);
  264. }
  265. }
  266. // push the failures back onto the queue for the next cycle
  267. queue.addAll(failures);
  268. }
  269. }
  270. }
  271. public void sendNow(Message message) throws Exception {
  272. Transport.send(message);
  273. }
  274. private static class MailMessageImpl extends MimeMessage {
  275. final String id;
  276. MailMessageImpl(Session session, String id) {
  277. super(session);
  278. this.id = id;
  279. }
  280. @Override
  281. protected void updateMessageID() throws MessagingException {
  282. if (!StringUtils.isEmpty(id)) {
  283. String hostname = "gitblit.com";
  284. String refid = "<" + id + "@" + hostname + ">";
  285. String mid = "<" + UUID.randomUUID().toString() + "@" + hostname + ">";
  286. setHeader("References", refid);
  287. setHeader("In-Reply-To", refid);
  288. setHeader("Message-Id", mid);
  289. }
  290. }
  291. }
  292. }