Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCX-24 Add newMessage method to MailSender class #20

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ v1.4.1 [unreleased]
- [#d58ab24](https://github.com/latera/camunda-ext/commit/d58ab24) Fix hid.Hydra#createEquipment method
- [#19c0746](https://github.com/latera/camunda-ext/commit/19c0746) Fix helpers.Hydra#fetchSubscription method
- [#12](https://github.com/latera/camunda-ext/pull/12) Fix helpers.Hydra#parseRegion method
- [#20](https://github.com/latera/camunda-ext/pull/20) Add newMessage method to MailSender class

v1.4 [2020-01-13]
-------------------
Expand Down
62 changes: 44 additions & 18 deletions src/org/camunda/latera/bss/connectors/Mail.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart
import javax.mail.internet.MimeUtility
import static org.camunda.latera.bss.utils.StringUtil.isEmpty
import static org.camunda.latera.bss.utils.ListUtil.firstNotNull

class MailSender {
Expand All @@ -23,14 +24,16 @@ class MailSender {
private Message message
private Multipart multipart
private Properties props
private Transport transport

MailSender(DelegateExecution execution) {
def ENV = System.getenv()

this.host = execution.getVariable('smtpHost') ?: ENV['SMTP_HOST']
this.port = (execution.getVariable('smtpPort') ?: ENV['SMTP_PORT'] ?: 587).toInteger()
this.user = execution.getVariable('smtpUser') ?: ENV['SMTP_USER']
this.password = execution.getVariable('smtpPassword') ?: ENV['SMTP_PASSWORD']
this.host = execution.getVariable('smtpHost') ?: ENV['SMTP_HOST']
this.port = (execution.getVariable('smtpPort') ?: ENV['SMTP_PORT'] ?: 587).toInteger()
this.user = execution.getVariable('smtpUser') ?: ENV['SMTP_USER']
this.password = execution.getVariable('smtpPassword') ?: ENV['SMTP_PASSWORD']
this.transport = session.getTransport('smtp')

Boolean auth = Boolean.valueOf(firstNotNull([
execution.getVariable('smtpAuth'),
Expand Down Expand Up @@ -67,10 +70,27 @@ class MailSender {
this.props.put('mail.smtp.ssl.trust', host)
}

this.session = Session.getDefaultInstance(props, null)
this.message = new MimeMessage(session)
this.multipart = new MimeMultipart()
this.setFrom(user)
this.session = Session.getDefaultInstance(props, null)
this.newMessage()
}

MailSender connect() {
if (!this.transport.isConnected()) {
if (isEmpty(this.host)) {
throw new Exception("Empty host name!")
}
if (isEmpty(this.port)) {
throw new Exception("Empty port number!")
}
if (isEmpty(this.user)) {
throw new Exception("Empty user name!")
}
if (isEmpty(this.password)) {
throw new Exception("Empty password!")
}
this.transport.connect(this.host, this.port, this.user, this.password)
}
return this
}

MailSender getHost() {
Expand Down Expand Up @@ -114,32 +134,39 @@ class MailSender {
}

MailSender setFrom(CharSequence from) {
message.setFrom(new InternetAddress(from.toString()))
this.message.setFrom(new InternetAddress(from.toString()))
return this
}

MailSender newMessage() {
this.message = new MimeMessage(session)
this.multipart = new MimeMultipart()
setFrom(user)
return this
}

MailSender addRecipient(CharSequence recipient, Message.RecipientType type = Message.RecipientType.TO) {
message.addRecipient(type, new InternetAddress(recipient.toString()))
this.message.addRecipient(type, new InternetAddress(recipient.toString()))
return this
}

MailSender setSubject(CharSequence subject) {
message.setSubject(subject.toString())
this.message.setSubject(subject.toString())
return this
}

MailSender addTextPart(CharSequence body) {
MimeBodyPart part = new MimeBodyPart()
part.setText(body.toString())
multipart.addBodyPart(part)
this.multipart.addBodyPart(part)
return this
}

MailSender addFile(CharSequence filename, Object datasource){
MimeBodyPart part = new MimeBodyPart()
part.setDataHandler(new DataHandler(datasource, 'application/octet-stream'))
part.setFileName(filename.toString())
multipart.addBodyPart(part)
this.multipart.addBodyPart(part)
return this
}

Expand All @@ -148,16 +175,15 @@ class MailSender {
URL url = new URL(urlStr)
part.setDataHandler(new DataHandler(url))
part.setFileName(MimeUtility.encodeText(name, 'UTF-8', null))
multipart.addBodyPart(part)
this.multipart.addBodyPart(part)
return this
}

void send(){
Transport transport = session.getTransport('smtp')
transport.connect(host, port, user, password)
connect()
try {
message.setContent(multipart)
transport.sendMessage(message, message.getAllRecipients())
this.message.setContent(this.multipart)
this.transport.sendMessage(this.message, this.message.getAllRecipients())
}
finally {
transport.close()
Expand Down