Skip to Content
Recently, we came accross the task of sending mails to every registered user in the portal. This was solved by combining the capabilities of Java mail api’s and the IUser and User factory api’s for Portal. ImportsThe imports are organised as  import java.util.Properties;  import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;  import com.sap.security.api.ISearchResult; import com.sap.security.api.IUser; import com.sap.security.api.IUserFactory; import com.sap.security.api.UMException; import com.sap.security.api.UMFactory; import com.sapportals.htmlb.Button; import com.sapportals.htmlb.Form; import com.sapportals.htmlb.GridLayout; import com.sapportals.htmlb.Group; import com.sapportals.htmlb.InputField; import com.sapportals.htmlb.Label; import com.sapportals.htmlb.TextEdit; import com.sapportals.htmlb.enum.GroupDesign; import com.sapportals.htmlb.page.DynPage; import com.sapportals.htmlb.page.PageException;  GUIChoose any GUI that appeals to you. I have selected the following GUI:   Form myForm = this.getForm();                 Group myGroup = new Group();                myGroup.setWidth(“300”);                 myGroup.setDesign(GroupDesign.SAPCOLOR);                 GridLayout gl = new GridLayout();                myGroup.addComponent(gl);                 Label lblSubject = new Label(“Subject: “);                InputField subject = new InputField(“subject”);                subject.setWidth(“270”);                gl.addComponent(1, 1, lblSubject);                gl.addComponent(1, 2, subject);                 Label lblmessage = new Label(“Message”);                lblmessage.setLabeledComponentId(“Message”);                gl.addComponent(2, 1, lblmessage);                 TextEdit message = new TextEdit(“Message”);                message.setId(“Message”);                message.setCols(50);                message.setRows(5);                gl.addComponent(2, 2, message);                 myGroup.setTitle(“Mailer”);                Button button = new Button(“Publish”);                button.setText(“Publish”);                 button.setOnClick(“Publish”);                gl.addComponent(3, 2, button);                 myForm.addComponent(myGroup); import com.sapportals.portal.htmlb.page.PageProcessorComponent; import com.sapportals.portal.prt.component.IPortalComponentRequest; import com.sapportals.portal.prt.component.IPortalComponentResponse;  Mailing SectionThis section of code deals with selecting the emails of the users and sending mails to each and everyone of them individually:  IUserFactory userfactory = UMFactory.getUserFactory();                IPortalComponentResponse res =                     (IPortalComponentResponse) this.getResponse();                IUser iuser;                InternetAddress recipientAddress = null;                String uniqueid, userid, email;                //               Mailing                 Properties p = new Properties();                p.put(“mail.transport.protocol”, “smtp”);                p.put(“mail.smtp.host”, “server name”);                p.put(“mail.smtp.port”, “25”);                // counting & displaying all existing Portal users.                ISearchResult isr;                try {                     isr = userfactory.getUniqueIDs();                     for (int i = 0; i<isr.size(); i++) {                          uniqueid = isr.next().toString();                          iuser = userfactory.getUser(uniqueid);                          email = iuser.getEmail();                          res.write(email);                          /* Mailer */                          InputField sub =                               (InputField) this.getPageContext().getComponentForId(                                    “subject”);                          String subject = sub.getValue().toString();                           TextEdit body =                               (TextEdit) this.getPageContext().getComponentForId(                                    “Message”);                          String bodystr = body.getText();                           try {                               IPortalComponentRequest request =                                    (IPortalComponentRequest) this.getRequest();                               IUser user = request.getUser();                                Session s = Session.getInstance(p);                               s.setDebug(false);                               MimeMessage m = new MimeMessage(s);                               InternetAddress myAddress =                                    new InternetAddress(user.getEmail());                                                         //VALIDATIONS TO BE DONE /*         There may be some email ids to which you may not wish to send the mails to, for example a person may hold a position known as poll administrator which has its own email id. You may not wish to send redundent emails to a single person through his different ids. Validate such things here before you insert the email id as the reciepient email. */                            recipientAddress = new InternetAddress(email);                                                              m.setFrom(myAddress);                               m.setRecipient(Message.RecipientType.TO, recipientAddress);                               m.setSubject(subject);                               m.setContent(bodystr, “text/plain”);                               Transport.send(m);   Organise this code into a DynPage and execute. Also see to it that you have the right firewall permissions to send your mails accross from your server to SMTP server.  Regards, Vijai Mohan
To report this post you need to login first.

14 Comments

You must be Logged on to comment or reply to a post.

  1. Punit Maini
    Hi Vijay,

    It will really helpful for my organisation.
    Can you please send me the PAR file for this.

    Please also send the steps how i will configure into my Portal server.

    I look forward to your reply my email id is punit.maini@gmail.com

    Thanxs and Regards

    Punit Maini

    (0) 
    1. Vijai Mohan Post author
      Hi, I was involved in some task that I completely forgot to check the updates. Sorry about that. Anyway, i will mail you and everyone the par file. In case you may have any problems in making it work, please mail me and I will be happy to sort things out.

      Vijai

      (0) 

Leave a Reply