diff --git a/src/main/java/Bundle.properties b/src/main/java/Bundle.properties index b3732f8f8b8..062940884e6 100755 --- a/src/main/java/Bundle.properties +++ b/src/main/java/Bundle.properties @@ -593,6 +593,7 @@ minute=minute notification.email.checksumfail.subject={0}: Your upload failed checksum validation notification.email.import.filesystem.subject=Dataset {0} has been successfully uploaded and verified notification.email.import.checksum.subject={0}: Your file checksum job has completed +contact.delegation={0} on behalf of {1} # passwordreset.xhtml pageTitle.passwdReset.pre=Account Password Reset diff --git a/src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java index 25dcff462b9..9513d061385 100644 --- a/src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java @@ -31,6 +31,7 @@ import javax.annotation.Resource; import javax.ejb.EJB; import javax.ejb.Stateless; +import javax.mail.Address; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; @@ -79,7 +80,7 @@ public class MailServiceBean implements java.io.Serializable { public MailServiceBean() { } - public void sendMail(String host, String from, String to, String subject, String messageText) { + public void sendMail(String host, String reply, String to, String subject, String messageText) { Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getDefaultInstance(props, null); @@ -89,7 +90,11 @@ public void sendMail(String host, String from, String to, String subject, String String[] recipientStrings = to.split(","); InternetAddress[] recipients = new InternetAddress[recipientStrings.length]; try { - msg.setFrom(new InternetAddress(from, charset)); + InternetAddress fromAddress=getSystemAddress(); + fromAddress.setPersonal(BundleUtil.getStringFromBundle("contact.delegation", Arrays.asList( + fromAddress.getPersonal(), reply)), charset); + msg.setFrom(fromAddress); + msg.setReplyTo(new Address[] {new InternetAddress(reply, charset)}); for (int i = 0; i < recipients.length; i++) { recipients[i] = new InternetAddress(recipientStrings[i], "", charset); } @@ -164,16 +169,25 @@ public void sendMail(String from, String to, String subject, String messageText) sendMail(from, to, subject, messageText, new HashMap<>()); } - public void sendMail(String from, String to, String subject, String messageText, Map extraHeaders) { + public void sendMail(String reply, String to, String subject, String messageText, Map extraHeaders) { try { MimeMessage msg = new MimeMessage(session); - if (from.matches(EMAIL_PATTERN)) { - msg.setFrom(new InternetAddress(from)); + //Always send from system address to avoid email being blocked + InternetAddress fromAddress=getSystemAddress(); + try { + fromAddress.setPersonal(BundleUtil.getStringFromBundle("contact.delegation", Arrays.asList( + fromAddress.getPersonal(), reply)), charset); + } catch (UnsupportedEncodingException ex) { + logger.severe(ex.getMessage()); + } + msg.setFrom(fromAddress); + + if (reply.matches(EMAIL_PATTERN)) { + //But set the reply-to address to direct replies to the requested 'from' party if it is a valid email address + msg.setReplyTo(new Address[] {new InternetAddress(reply)}); } else { - // set fake from address; instead, add it as part of the message - //msg.setFrom(new InternetAddress("invalid.email.address@mailinator.com")); - msg.setFrom(getSystemAddress()); - messageText = "From: " + from + "\n\n" + messageText; + //Otherwise include the invalid 'from' address in the message + messageText = "From: " + reply + "\n\n" + messageText; } msg.setSentDate(new Date()); msg.setRecipients(Message.RecipientType.TO,