How to send email from stash plugin?

Jacob Schoen April 23, 2013

I attempted to add the javax.mail jar to my plugin to send emails after a post, but I get an exception

com.sun.mail.handlers.text_plain cannot be cast to javax.activation.DataContentHandler
 java.lang.ClassCastException: com.sun.mail.handlers.text_plain cannot be cast to javax.activation.DataContentHandler
   at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:596)
   at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:550)
   at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:611)
   at javax.activation.DataHandler.writeTo(DataHandler.java:315)
   at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:248)
   at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1268)
   at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2012)
   at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1980)
   at javax.mail.Transport.send(Transport.java:97)
   at com.gray.stash.hook.StashPostHookEmailSender.sendEmail(StashPostHookEmailSender.java:153)
   at com.gray.stash.hook.StashPostHookEmailSender.postReceive(StashPostHookEmailSender.java:96)
   at com.atlassian.stash.internal.hook.repository.AsyncPostReceiveRepositoryHookAdapter$1.visit(AsyncPostReceiveRepositoryHookAdapter.java:64)
   at com.atlassian.stash.internal.hook.repository.AsyncPostReceiveRepositoryHookAdapter$1.visit(AsyncPostReceiveRepositoryHookAdapter.java:59)
   at com.atlassian.stash.internal.hook.repository.DefaultRepositoryHookService$8.doInTransaction(DefaultRepositoryHookService.java:398)
   at com.atlassian.stash.internal.hook.repository.DefaultRepositoryHookService$8.doInTransaction(DefaultRepositoryHookService.java:392)
   at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
   at com.atlassian.stash.internal.hook.repository.DefaultRepositoryHookService.visitEnabledHooks(DefaultRepositoryHookService.java:392)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
   at com.atlassian.stash.internal.aop.ProfilingAspect.profileMethod(ProfilingAspect.java:43)
   at sun.reflect.GeneratedMethodAccessor173.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
   at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
   at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:61)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:124)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy182.visitEnabledHooks(Unknown Source)
   at com.atlassian.stash.internal.hook.repository.AsyncPostReceiveRepositoryHookAdapter.postReceive(AsyncPostReceiveRepositoryHookAdapter.java:59)
   at com.atlassian.stash.internal.hook.repository.AsyncPostReceiveRepositoryHookAdapter.onRepositoryPushEvent(AsyncPostReceiveRepositoryHookAdapter.java:55)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at com.atlassian.event.internal.SingleParameterMethodListenerInvoker.invoke(SingleParameterMethodListenerInvoker.java:36)
   at com.atlassian.event.internal.AsynchronousAbleEventDispatcher$2.run(AsynchronousAbleEventDispatcher.java:66)
   at com.atlassian.sal.core.executor.ThreadLocalDelegateRunnable.run(ThreadLocalDelegateRunnable.java:34)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
   at java.lang.Thread.run(Thread.java:722)

This seems to be a classpath issue that comes up from time to time. So in my search for a solution, I found How to send email from plugin? that seems to cover Jira and Confluence, but I could not find any information on how to accomplish this with stash. Is there something Similar for Stash Plugins that I can access.

I also found atlassian-mail, which seems to be what the answers in the question I found where using. Is this relevant for Stash plugins? If so what do I need to add to pom to include it in my plugin?

5 answers

1 accepted

2 votes
Answer accepted
Colin Goudie
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 23, 2013

As the Atlassian's mentioned, just use MailService. You create a MailMessage

MailMessage message = new MailMessage(toEmails, fromEmailAddress,
                ccEmails, Sets.<String>newHashSet(), Sets.<MailAttachment>newHashSet(),
                text, subject,
                emailHeaders
        );

Then send it

if (mailService.isHostConfigured()) {
            MailMessage mailMessage = buildMessage(...);
            mailService.submit(mailMessage);
        }

Jacob Schoen April 23, 2013

@Colin Thanks for the help. The code sample made it the best answer.

Sebastian Stein August 6, 2014

MailService in Stash API is only an interface, but there seems to be no implementation. How do I create an implementation? I've seen the examples from Confluence where you have to use a TaskManager, but there seems to be no TaskManager in Stash API. Can you please post a more complete example?

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 7, 2014

Hi Sebastian,

You'll notice all of Stash services are interfaces. That's because the implmentation is internal and you need to import /inject the service before you can use it.

class MyClass {
  final MailService mailService;
  public MyClass(MailService mailService) {
    this.mailService = mailService;
  }
}

atlassian-plugin.xml:

<component-import key="mail-service" interface="com.atlassian.stash.mail.MailService" />
<component key="myclass" class="com.example.MyClass" />

https://developer.atlassian.com/display/DOCS/Component+Import+Plugin+Module

I hope that helps.

Charles

1 vote
Michael Heemskerk
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 23, 2013

Hi Jacob,

The simplest option is to use Stash's MailService.

Cheers,

Michael

Jacob Schoen April 23, 2013

Thanks for the point in the right direction.

1 vote
jhinch (Atlassian)
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 23, 2013

Take a look at the MailService provided as part of Stash's public API.

Jacob Schoen April 23, 2013

Thanks for the point in the right direction.

0 votes
Suman Rai April 28, 2013

Thanks for the quick response Seb, but it did not give me any clue. I am already running my Stash in debug mode and configure it into Ecplise. Strange thing is it is happening only for the mail ids having '&' in it.

Please help, I tried all possible options (like replacing & with %26 and amp; etc..). Also tried sending as 'R&Dteam@example.com' in to address.

Thanks

Suman

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 29, 2013

Hi Suman,

We are just using Spring Mail to send the mail. Personally I would try and write a small Java class that sends a message to your mail server and see if that works first. I suspect the problem may lie there and not in Stash directly.

Cheers,

Charles

Suman Rai April 29, 2013

Thanks Charles and Seb. Yes you were correct, problem was at xchange server. I contacted to IT team regarding this and now users in 'R&Dteam' DLs are able to receive the mails. Sorry if I was not clear at begining of my question, I am new user in this world!!

Thanks a lot for promt help and analysis. Hope same will there for any other future issues.

0 votes
Suman Rai April 28, 2013
Hi, I am using Stash's 'MailService' to send mails. But for some Outlooks Distribution lists, which contains '&' in it (for example R&Dteam@example.com) is not receiving mails. Also I am not finding any error message any where. Can I get any help on this Forum. Thanks Suman
seb
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 28, 2013

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events