Saturday 2 October 2010

Send HTML email from alfresco 3.3 with attachment tutorial and code








In my first blog I will provide you with feature to send email using alfresco javascript by executing action and sending HTML email, also you can send attachment with this mail you can use templates and so on, same as using old mail action.

first javascript:

var mail = actions.create("mail2");
mail.parameters.to = "savic.prvoslav[at]gmail.com";
mail.parameters.subject = "Html test";
mail.parameters.from = "savic.prvoslav[at]gmail.com";
mail.parameters.text = "My name is Savić Prvoslav<b> if you see this it is very bold</b>";

mail.parameters.node=document;
//here you set document to sent via email, you can comment this line if you do not want to send doc :)
mail.execute(document);








you can download jar-source-file .


How to install: 
 put jar file in alfresco/web-inf/lib , restart alfresco, use the javascript.


How to set up Outbound Email Configuration for Gmail:
alfresco3.3G\tomcat\shared\classes\alfresco-global.properties


#
# Outbound Email Configuration
#-------------
mail.host=smtp.gmail.com
mail.port=465
mail.protocol=smtps
mail.username=username
mail.password=pass
mail.smtps.starttls.enable=true
mail.smtps.auth=true
mail.from.default=defaultEmail

















In case of passing the rapidshare.limit just post comment and I will update file for you.

81 comments:

  1. Very Good article. Worked fine for me. In addition to this, Can u update the script for the wf. Like
    mail.parameters.node=bpm_package.children[0];
    mail.execute(bpm_package);

    Thanks Prvoslav for your post.

    ReplyDelete
  2. My plan is to make change to be able to send bunch of files that are in wf.

    mail.parameters.node=bpm_package;
    mail.execute(bpm_package);

    ReplyDelete
  3. Hi Savic,

    Thanks for the article! By the way have you had any problems with the smtp.gmail.com server? When I try it I get the next exception:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    Thanks,

    Luis

    ReplyDelete
  4. No problem, will be many more ...
    about your problem this is common problem, i think that I solved it with this ( but no guarantee on this)
    http://blogs.sun.com/gc/entry/unable_to_find_valid_certification

    ReplyDelete
  5. is there another mirror link for the jar file? file not found on rapidshare. thanks

    ReplyDelete
  6. Just updated the download link, you can download now.
    PS: thanks for notifying me.

    ReplyDelete
  7. Hi savic

    is it possible to automatically send an email to the reviewer by just adding the javascript code in workflow-options.jsp?

    added



    function sendMail(){

    var mail = actions.create("mail2");
    mail.parameters.to = "receiveMail[at]gmail.com";
    mail.parameters.subject = "Html test";
    mail.parameters.from = "testMail[at]gmail.com";
    mail.parameters.text = "test";

    mail.parameters.node=document;
    mail.execute(document);

    }




    then create a button for send notication: (onClick='sendMail()')

    Thanks.

    ReplyDelete
  8. I have tried this but did not work. Any how document in javascript ( normal js) is something else :"The JavaScript Document object is the container for all HTML HEAD and BODY objects associated within the HTML tags of an HTML document." ... so sorry for this, what you should do is run this action from java, you can do that too if you need this.

    ReplyDelete
  9. Just tried to download the jar file. Says limit of 10 already reached. Any chance of updating the link again?

    ReplyDelete
  10. Very nice! Thanks you. This is just what I was looking for since email containing content of Alfresco Site Blogs and Discussions need to use html. The content of these is in html, now the email can be in html. Yipppeee!

    ReplyDelete
  11. Hey Savic,
    It seems that link is not working.

    It will be greate fully if you provide some other images.

    Thanks a lot in advance
    Akshat

    ReplyDelete
  12. I downloaded the action today, does this version support sending multiple attachments?

    ReplyDelete
  13. currently no, but you can change the code for this. you would add about 10 lines of code.
    Also would be nice to send me a copy.

    ReplyDelete
    Replies
    1. can you send me your updated jar which will can send multiple attachments?
      thanks!

      medeljr.mendoza@gmail.com

      Delete
    2. I'm having the same issue. I can't send multiple attachment.
      Do you have any code to fix this thing sir ?

      Thanks .

      robin.oria@gmail.com

      Delete
    3. There is a great demand with this feature, i will try in the near future to do an update on this on my new blog.

      Delete
  14. I tried to run the action from a javascript behaviour and received an error when I provide a node value. If node parameter is not used, the email is sent okay. Only when the node parameter is populated is the error generated. I also tried running it from a javascript space rule, in this case, everything works fine. Thanks Steve

    Caused by: java.lang.IllegalArgumentException: FacesContext must not be null
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.web.jsf.FacesContextUtils.getWebApplicationContext(FacesContextUtils.java:50)
    at org.springframework.web.jsf.FacesContextUtils.getRequiredWebApplicationContext(FacesContextUtils.java:81)
    at org.alfresco.web.bean.repository.Repository.getServiceRegistry(Repository.java:584)
    at org.alfresco.sample.CustomMailActionExecuter$1.prepare(CustomMailActionExecuter.java:340)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:353)

    ReplyDelete
    Replies
    1. We are Facing the same Issue using your Jar File(both pre compiles and code) in alfresco 4.0 (Build from svn).

      Have you resolved this Issue.
      Please guide on this issue,we are stuck at this.

      Delete
    2. Are you using this in alfresco browse or in share ?

      Delete
    3. Thanks For your quick response

      In Alfresco Share UI & Content RULEs [execute javascript] it gives this exception;
      We also observe that:
      If we execute it anytime from Alfresco UI, it works well AND afterwards the exception doesnt come in Share UI also.

      Thanks again.

      Delete
    4. I really do not know, but this was designed to work i alfresco browser not share. You have the code, so you can change it as you please. If you make it work in share be kind enough to send me the code please :)

      Delete
    5. Can you please guide us to resolve this issue.

      Caused by: java.lang.IllegalArgumentException: FacesContext must not be null
      at org.springframework.util.Assert.notNull

      Thanks

      Delete
  15. Hi Steve, how are you running the code when it does not work?

    ReplyDelete
  16. Scenario where the error message is generated -- I attach a behaviour to the onNodeUpdate policy which executes a javascript function which lives on the classpath. In the javascript I am writing a new node and once the node is saved, that noderef is used for the mail parameter (node).
    Thanks for your help.

    ReplyDelete
  17. I've noticed in my user interface that triggers the onUpdateNode behaviour, that it waits till the behaviour completes before allowing the user to take another UI action. When I add an attachment, depending on the size of the attachment, I am expecting the delay will become greater. I looked at the source code you provided and I don't see the async paramter specified. In the ActionService there is an async paramter - http://dev.alfresco.com/resource/docs/java/repository/org/alfresco/service/cmr/action/ActionService.html#executeAction%28org.alfresco.service.cmr.action.Action,%20org.alfresco.service.cmr.repository.NodeRef,%20boolean,%20boolean%29 Any idea how to implement this?

    ReplyDelete
  18. Thanks Steve, I will see to fix this problem today or tomorrow and post an update here .
    thanks for your help!

    ReplyDelete
  19. can u Describe about imagemagick and openoffice integration in alfresco with proper steps

    ReplyDelete
  20. There is no need, you already have this online.

    http://wiki.alfresco.com/wiki/Setting_up_OpenOffice_for_Alfresco

    http://wiki.alfresco.com/wiki/ImageMagick_Configuration

    ReplyDelete
  21. Sadly MegaUpload is dead :(
    Can you please put your JAR somewhere else?

    ReplyDelete
  22. yea, chaned the link hope it works for you.

    ReplyDelete
  23. Your source file (sample.zip) you provide above consist of *.java file. When I try to rename zip to jar and load it in Alfresco, surely it gives me "NoClassFound exception". I guess I need to compile *.java file into *.class file.

    Problem is that I'm very far from JAVA programming and I don't know how to do that in a simple way. Can you help me in this matter?
    I'm still using Alfresco Community v3.2.0 (2039) version in my production system. (Yes I know it's old, buggy and sucks :)

    ReplyDelete
  24. Well idea is to give you the source so you can do with it what you want. Here is the link for real jar file, so if it works let me know .

    https://e-75.netdokumenti.com/sd?ID=1331817492796

    ReplyDelete
  25. Thanks! You're as fast as always :)
    Sadly it didn't work for me because Alfresco 3.2.0 doesn't have this TestModeable class/interface. Oh well :(

    To tell the truth, I actually was searching just for original Alfresco 3.2.0 (2039) version MailActionExecutor class fix to not send user initiated mails using user e-mail address in "mail.from" field by default. This is really ridiculous limitation of Alfresco and sadly they will not even try to fix that:
    https://issues.alfresco.com/jira/browse/ALF-1566

    This fix is more than simple and I can fix it in MailActionExecutor.java file by changing 1-2 lines, but I can't compile it.

    As I said I'm far from JAVA programming, but I have >10 year exp.in PHP, CSS, HTML etc...
    Also I couldn't find Alfresco 3.2.0 (2039) version SDK download anymore (I guess I searched for it badly).

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. I have already made a few modifications to spring files in past and so on. It's not a problem.
    My problem is that I can create custom (or modify) *.java source file, but I can't compile it to *.class file and without compiling java file to class file nothing will work, right? :(

    ReplyDelete
  28. You have to know a bit of spring...
    find "action-services-context.xml"
    find <bean id="mail" class="org.alfresco.repo.action.executer.MailActionExecuter"...
    create class that extends mailaction... and add your code.
    put class in my jar, you have xml file there, add a copy paste bean definition just replace the class part. remove all my classes and bean definitions.

    note : override this method afterPropertiesSet().

    ReplyDelete
  29. can you send me code for modification, will send you class file :).

    ReplyDelete
    Replies
    1. Here it is. Only thing is that probably class name should be different than default MailActionExecuter.

      http://files.fm/u/fycbgkg

      Delete
    2. Changes are starting in line 360

      Delete
  30. This comment has been removed by the author.

    ReplyDelete
  31. Will try to send you class file during today or tomorrow

    ReplyDelete
    Replies
    1. Hi!
      I don't want to bug you, but did you manage to compile this classfile ? :)

      Delete
  32. I am really sorry, I was sick and was not working :(

    ReplyDelete
    Replies
    1. No problem mate! I hope you're better now.
      It's just - I have to manually forward ~100 mails daily to our clients because of this and I want to get rid of that :))) lol

      Delete
    2. Oh man, I'm desperately waiting on this :)
      Any news? Or maybe you can point me to some URL where something similar has already been done and updated *.class (not only *.java) file for Alfresco 3.2.0 is downloadable?

      Delete
  33. https://e-75.netdokumenti.com/sd?ID=1332241387913

    here it is.

    ReplyDelete
    Replies
    1. It looks that it's working now as I imagined.

      Thanks a LOT! You're the best :)

      Delete
  34. I wonder if you can help me. After a friend helped me install alfresco, I lost my password for admin. I am the only user so far. You mentioned in the alfresco forum that there is a way to do this "in java coding and adding new servlet". If as I suspect, this is the only thing left to do, I would be very grateful to know how.

    Many thanks
    Huw Roberts

    ReplyDelete
  35. Are you a programmer or just user with admin rights?

    ReplyDelete
  36. I'm not a programmer, so I wouldn't be able to do anything complicated. Was just hoping not to have to reinstall everything.

    ReplyDelete
  37. Oh no, reinstalling is not needed (you can do it if you do not have any data that is important :D )

    Do you know how to add servet, so I would send you only the important code (that resets password about 10 lines of code), or you need jar file, with compiled java class ?

    ReplyDelete
  38. If it's just a matter of resetting 10 lines of code, I'm sure I can do that if you can tell me where exactly.

    If it's not as simple as that, I'll still give it a try if you'll bear with me. I am new to this. Thanks.

    ReplyDelete
  39. Yes, will try to send you code tomorrow. currently am not at home :(

    ReplyDelete
  40. Hi,

    I've try to integrate this jar in my alfresco 3.4 community.
    When i try to send a simple mail without any attachment i have this error :

    org.springframework.extensions.webscripts.WebScriptException: 06100006 Internal error
    caused by:
    java.lang.NullPointerException


    Can you help me?

    Thanks

    ReplyDelete
    Replies
    1. Now it's work, but the from parameter not working.

      Delete
    2. Can you elaborate on your problem on this?

      Delete
  41. Works like a charm! Great post, great job!

    Thanks!

    ReplyDelete
  42. does this work on 4.2.c? if so how do you set it up exactly. instruction are not very clear?

    ReplyDelete
    Replies
    1. Hi, it should work.
      You have to create project in eclipse and import the code there. Then deploy it.
      So if you can run this example you can use this code with no problem.
      http://wiki.alfresco.com/wiki/Custom_Actions

      Delete
    2. i created project in netbeans. compiled the java file. made a jar under build in the netbeans project. copied the jar file to tomcat/shared/lib in alfresco. copied the xml file to extension. get following error below

      Delete
    3. i put the jar under tomcat/webapps/alfresco/webint/lib however the `mail.parameters.node=document;` does not attatch any document. i still get blank email. what am i missing?

      Delete
    4. YOu are running script in alfresco or share ?

      Delete
    5. i got it working. put jar under webapps/alfresco... however on line 364 of java file `message.addAttachment("test", file);` how do i replace "test" with the filename of the node?

      Delete
  43. This comment has been removed by the author.

    ReplyDelete
  44. Hi,

    Thanks for the source, I am facing ArrayOutoFBound Exception while executing this code.


    org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.ArrayIndexOutOfBoundsException: 2
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:367)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:344)
    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:606)
    at org.alfresco.repo.management.subsystems.SubsystemProxyFactory$1.invoke(SubsystemProxyFactory.java:72)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy113.send(Unknown Source)
    at org.alfresco.repo.action.EmailNotification.executeImpl(EmailNotification.java:500)

    Please help.

    ReplyDelete
  45. Can you please tell what is the email templeate to be used with your code to have the email ?

    ReplyDelete
    Replies
    1. Hello,
      you can set template with .template=nodeRef

      Delete
  46. This comment has been removed by the author.

    ReplyDelete
  47. First thanks for your solution
    I installed the jar file in alfresco 4.2e with java 1.6.31 and working properly.
    The problem is that I have another rule to "share" that runs a js file. and it works fine but when I install your solution gives me java errors. If I remove your solution works well.

    ReplyDelete
  48. This comment has been removed by the author.

    ReplyDelete
  49. Hi,

    I am facing following issue.

    2014-07-22 19:15:36,604 ERROR [action.executer.MailActionExecuter] [pool-5-thread-2] Failed to send email to xyz@gmail.com
    org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.NullPointerException
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:367)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:344)
    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.alfresco.repo.management.subsystems.SubsystemProxyFactory$1.invoke(SubsystemProxyFactory.java:72)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy142.send(Unknown Source)

    Please help.

    Thanks

    ReplyDelete
    Replies
    1. From your exception i can not see what is wrong with the code :(

      Delete
  50. Hi,

    Code ::

    var mail = actions.create("mail2");
    mail.parameters.subject = "Reminder" + bpm_workflowDescription;
    mail.parameters.from = initiator.properties["cm:email"];
    mail.parameters.text = "Kindly process the document: " ;

    var members = people.getMembers(bpm_groupAssignee);

    var document = bpm_package.children[0];

    for(var x in members) {
    logger.log("Member is: " + members[x].properties.email);
    mail.parameters.to = members[x].properties.email;
    mail.parameters.node=document;
    mail.execute(bpm_package);
    }

    We have written this web script. I am trying to execute this webscript in alfresco share.

    And we are facing following issue.

    2014-07-22 19:51:45,414 ERROR [transaction.support.TransactionSynchronizationUtils] [pool-5-thread-2] TransactionSynchronization.afterCompletion threw exception
    java.lang.IllegalStateException: No value for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@2500091c] bound to thread [pool-5-thread-2]
    at org.springframework.transaction.support.TransactionSynchronizationManager.unbindResource(TransactionSynchronizationManager.java:199)
    at org.mybatis.spring.SqlSessionUtils$SqlSessionSynchronization.suspend(SqlSessionUtils.java:243)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.doSuspendSynchronization(AbstractPlatformTransactionManager.java:666)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.suspend(AbstractPlatformTransactionManager.java:569)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.handleExistingTransaction(AbstractPlatformTransactionManager.java:418)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:347)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:127)
    at org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:40)

    Please help me.

    ReplyDelete
  51. Hey Savic,
    It seems that link is not working.

    Please provide some other images.

    Thanks a lot.

    ReplyDelete
    Replies
    1. Hello,
      this is done for quite old alfresco version.

      Upgrade is needed :(

      Delete
  52. Update is needed. Link is broken :(
    For Alfresco 5.1e org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mail2' is defined

    ReplyDelete