Send Email in JIRA servlet plugin

simon xu June 8, 2017

i have created a servlet plugin for jira, and want to send mail in this servlet. it works ok in my local server, when i deployed it to our integration server, it doesn't work, the integration server have configed the mail server and enabled. anyone can give me a suggestion? the code as below:


SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer();

if (mailServer == null)
{
       LOG.debug("Not sending message as the default SMTP Mail Server is not defined.");
return;
}

if (!MailFactory.getSettings().isSendingDisabled())
{
Email mail = new Email(to);
mail.setSubject(subject);

Multipart mainPart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();//创建一个包含HTML内容的MimeBodyPart
//设置HTML内容
messageBodyPart.setContent(body,"text/html; charset=utf-8");
mainPart.addBodyPart(messageBodyPart);
mail.setMultipart(mainPart);

mailServer.send(mail);
}
else
{
LOG.debug("Not sending message as sending is turned off.");
}

3 answers

0 votes
Anil singh June 9, 2017

Please check your SMTP server and email settings

 

simon xu June 9, 2017

i think the SMTP server is ok, because it can send email to me when i assign a task to me in jira system, and i can get the email correctly.

simon xu June 9, 2017

this is the mail i get from my local server, and the same jar upload to intergration server i cannot get any emails, so strange...1496997576(1).png

simon xu June 9, 2017

this is the mail i get from intergration server when i assign a task to me, so i think the smtp server is ok.

1496997763(1).png

simon xu June 12, 2017

hi,

i found the error in another log file, the error like below:

Caused by: javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
boundary="----=_Part_388_1975592549.1496992876233"
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1245)
at com.atlassian.mail.server.impl.SMTPMailServerImpl.sendMimeMessage(SMTPMailServerImpl.java:226)
at com.atlassian.mail.server.managers.EventAwareSMTPMailServer.sendMimeMessage(EventAwareSMTPMailServer.java:25)
at com.atlassian.mail.server.impl.SMTPMailServerImpl.sendWithMessageId(SMTPMailServerImpl.java:176)
... 208 more

0 votes
Anil singh June 9, 2017

Hi 

i have done same email work in my servlet class so please try this.

public void sendEmailToUser(){
System.out.println("Sending email to user....");
try{
String baseUrl = settingsManager.getGlobalSettings().getBaseUrl();
String confluencePageURL = pageManager.getPage(pageId).getUrlPath();
String PageLinkInJira = baseUrl + confluencePageURL;
String to = "anil@abc.com";
String subject = "Data Request Form Error";
String Pagebody = "Test body";
MailQueueItem testingMailQue=new ConfluenceMailQueueItem(to, subject, Pagebody, MIME_TYPE_HTML);
String to2="email2";
MailQueueItem mailque=new ConfluenceMailQueueItem(to2, subject, Pagebody, MIME_TYPE_HTML);
System.out.println("mail sending ........");
taskManager.addTask(MAIL, testingMailQue);
taskManager.addTask(MAIL, mailque);
System.out.println("mail sent .......");
}
catch (Exception e) {
System.out.println("General Exception " + e.getMessage());
e.printStackTrace();
}
}

Hope it will helpful for you

Anil Singh

Programmer Analyst

http://www.mirketa.com/

 

 

 

 

simon xu June 9, 2017

Anil,

how to inject taskManager to this class, because i am completely new comer, so i don't know how to add the mail to a queue, i have tried use SingleMailQueueItem to send mail, i get the same result, in my local server it works ok, but doesn't work on the intergration server, coed like below

public void sendEmail(String to, String subject, String body)
{

try {
Email mail = new Email(to);
mail.setSubject(subject);

Multipart mainPart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();//创建一个包含HTML内容的MimeBodyPart
//设置HTML内容
messageBodyPart.setContent(body,"text/html; charset=utf-8");
mainPart.addBodyPart(messageBodyPart);
mail.setMultipart(mainPart);

MailQueueItem mailQueueItem = new SingleMailQueueItem(mail);

mailQueueItem.send();
} catch (MailException e) {

e.printStackTrace();
} catch (MessagingException e) {

e.printStackTrace();
}

}

0 votes
Bhushan Nagaraj
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 8, 2017

Hey Simon,

What do you see in the logs?

Thanks

Bhushan

simon xu June 8, 2017

Hi Bhushan,

there no any errors in the log file, so i don't know how to solve this problem, after all it works ok in our local server, and i can get the email. in my plugin i will send a mail and then save some data into the database, when i run the plugin in the intergration server i cannot get the mail but the data has ben stored, and i check the log file there no error.

 

thanks

Suggest an answer

Log in or Sign up to answer