Confluence mail sending is one of the less documented functions, I found out. So after trying different things and finaly find my solution I document it here.
My first “solution” was to send the Mail directly via SMTP, after finding this article:
https://discursive.atlassian.net/wiki/display/CJCOOK/Sending+Mail+with+SMTP
Code was sth. Like this:
import org.apache.commons.net.smtp.SMTPClient;
SMTPClient client = new SMTPClient( );
client.connect("smtpserver");
client.sendSimpleMessage("myMail@smtpserver.com",
"someone@internet.com",
"TEST" );
client.disconnect( );
Okay. First try and it did not work. Also, I did not see a chance to get the SMTP – Settings from the Confluence installation. And also, where should I put the Subject into?
Hm, there must be a standard confluence possibility, I thought. And found:
http://blog.k15t.com/sending-email-from-confluence-plugins
Code (from that page):
String emailText = "Email Text with HTML";
ConfluenceMailQueueItem item = new ConfluenceMailQueueItem(
contact.getEmail(),
null,
subject,
emailText,
ConfluenceMailQueueItem.MIME_TYPE_HTML);
item.setFromName("Example Inc.");
item.setFromAddress("website@example.com");
taskManager.addTask("mail", item);
Okay, this worked for me. But I wanted to send plain Text, immediately, and overwrite the configured Subject Prefix from the Confluence installation.
With all those premises, I got finally to this conclusion:
public String execute() {
User user = AuthenticatedUserThreadLocal.getUser();
String toAddress ="test@home.net";
String subject = "SMS test";
String body = "Test message sent from user"+user.getName();
try{
SMTPMailServer mailServer = MailFactory.getServerManager().getDefaultSMTPMailServer();
mailServer.setPrefix("");
mailServer.send(new Email(toAddress).setSubject(subject).setBody(body).setMimeType(ConfluenceMailQueueItem.MIME_TYPE_TEXT));
}
catch (Exception e){e.printStackTrace();}
return SUCCESS;
}

