How do I send an email using Mendix?

9
I want to send out emails as part of a workflow application.
asked
3 answers
16

You can use a Java action to send an email.

How to implement? The following operations have to be carried out in order to implement the send email Java action:

  1. Adding a Java action to a microflow;
  2. Deploy the project;
  3. Add Java code to the Java sources.

After deploying the project, you are able to add the java code to the java sources in the project’s deployment location.

• Go to your deployment location; • Select the map ‘Java source’; • Select the map of the module in which the Java action is added; • Select the ‘actions’ map; • Select the particular Java action and add the following text between ‘// BEGIN USER CODE’ and ‘// END USER CODE’:

After following the steps above, you can deploy and run your Mendix application in order to test your action in the Internet browser.

In addition:

To, From, CC, Subject and Body can be determined in the Mendix Business modeler by creating an object with those attributes. For example, ‘To’ can be set as the selected employee’s email address.

Example of Java code:

// This code is generated by the Mendix Business Modeler version 2.4.0.

// The following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. // Special characters, e.g., é, ö, à, etc. are supported in comments.

package general.actions;

import com.mendix.systemwideinterfaces.core.UserAction;
import java.util.ArrayList; import com.mendix.core.CoreException;
import com.mendix.modules.email.EmailModule;
import com.mendix.modules.email.SMTPConfiguration;
import com.mendix.systemwideinterfaces.core.IMendixObject;

/** * */ public class SendEmail extends UserAction<boolean> { 
    private String SMTPServerAddress;
    private String SMTPServerUser;
    private String SMTPServerPassword;
    private general.proxies.Email EmailObj;

public SendEmail(String SMTPServerAddress, String SMTPServerUser, String SMTPServerPassword, IMendixObject EmailObj)
{
    super();
    this.SMTPServerAddress = SMTPServerAddress;
    this.SMTPServerUser = SMTPServerUser;
    this.SMTPServerPassword = SMTPServerPassword;
    this.EmailObj = EmailObj == null ? null : general.proxies.Email.initialize(EmailObj);
}

@Override
public Boolean executeAction() throws Exception
{
    // BEGIN USER CODE
    String body = this.EmailObj.getBody();
    String subject = this.EmailObj.getSubject();
    String to = this.EmailObj.getTo();
    String cc = this.EmailObj.getCC();
    if( to == null )
        throw new CoreException( "There is no email address found to send this email to." ); 

    SMTPConfiguration config = new SMTPConfiguration();

    config.setFromAddress( this.EmailObj.getFrom() );

    if( this.SMTPServerAddress == null ) 
        throw new CoreException( "There is no smtp server address specified." );
    config.setSMTPHost( this.SMTPServerAddress );
    config.setSMTPPort(25);

    if( this.SMTPServerUser == null ) 
        throw new CoreException( "There is no user given to access the SMTP server." );
    config.setUserName( this.SMTPServerUser );

    if( this.SMTPServerPassword == null ) 
        throw new CoreException( "There is no password specified for user: " + this.SMTPServerUser );
    config.setUserPass( this.SMTPServerPassword );
    config.setUseSSLSMTP(false);



    ArrayList<String> toList = new ArrayList<String>();
    if( to.contains(";") ) {
        String[] arr = to.split(";");
        for( String email : arr )
        {
            if( !"".equals( email.trim() ) )
                toList.add( email.trim() );
        }
    }
    else
        toList.add(to);

    if( toList.size() == 0 )
        throw new CoreException( "There are no addresses found to send the email to." );

    ArrayList<String> ccList = new ArrayList<String>();
    if( cc == null )
        cc = "";

    if( cc.contains(";") ) {
        String[] arr = cc.split(";");
        for( String email : arr )
        {
            if( !"".equals( email.trim() ) )
                ccList.add( email.trim() );
        }
    }
    else if( !"".equals( cc.trim() ) )
        ccList.add(cc);



    EmailModule.mail( this.getContext(), config, body, subject, toList, (ccList.size() > 0 ? ccList : null), null, null);


    return true;
    // END USER CODE
}

/**
 * Returns a string representation of this action
 */
@Override
public String toString()
{
    return "SendEmail";
}

// BEGIN EXTRA CODE
// END EXTRA CODE
answered
2

See the template store at https://template-exchange.mendix.com. There is a template that you can import in your application for e-mailing.

answered
0

In addition:

To, From, CC, Subject and Body can be determined in the Mendix Business modeler by creating an object with those attributes. For example, ‘To’ can be set as the selected employee’s email address.

Example of Java code:

// This code is generated by the Mendix Business Modeler version 2.4.0.

// The following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. // Special characters, e.g., é, ö, à, etc. are supported in comments.

package general.actions;

import com.mendix.systemwideinterfaces.core.UserAction; import java.util.ArrayList; import com.mendix.core.CoreException; import com.mendix.modules.email.EmailModule; import com.mendix.modules.email.SMTPConfiguration; import com.mendix.systemwideinterfaces.core.IMendixObject;

/** * */ public class SendEmail extends UserAction<boolean> { private String SMTPServerAddress; private String SMTPServerUser; private String SMTPServerPassword; private general.proxies.Email EmailObj;

public SendEmail(String SMTPServerAddress, String SMTPServerUser, String SMTPServerPassword, IMendixObject EmailObj)
{
    super();
    this.SMTPServerAddress = SMTPServerAddress;
    this.SMTPServerUser = SMTPServerUser;
    this.SMTPServerPassword = SMTPServerPassword;
    this.EmailObj = EmailObj == null ? null : general.proxies.Email.initialize(EmailObj);
}

@Override
public Boolean executeAction() throws Exception
{
    // BEGIN USER CODE
    String body = this.EmailObj.getBody();
    String subject = this.EmailObj.getSubject();
    String to = this.EmailObj.getTo();
    String cc = this.EmailObj.getCC();
    if( to == null )
        throw new CoreException( "There is no email address found to send this email to." ); 

    SMTPConfiguration config = new SMTPConfiguration();

    config.setFromAddress( this.EmailObj.getFrom() );

    if( this.SMTPServerAddress == null ) 
        throw new CoreException( "There is no smtp server address specified." );
    config.setSMTPHost( this.SMTPServerAddress );
    config.setSMTPPort(25);

    if( this.SMTPServerUser == null ) 
        throw new CoreException( "There is no user given to access the SMTP server." );
    config.setUserName( this.SMTPServerUser );

    if( this.SMTPServerPassword == null ) 
        throw new CoreException( "There is no password specified for user: " + this.SMTPServerUser );
    config.setUserPass( this.SMTPServerPassword );
    config.setUseSSLSMTP(false);



    ArrayList<String> toList = new ArrayList<String>();
    if( to.contains(";") ) {
        String[] arr = to.split(";");
        for( String email : arr )
        {
            if( !"".equals( email.trim() ) )
                toList.add( email.trim() );
        }
    }
    else
        toList.add(to);

    if( toList.size() == 0 )
        throw new CoreException( "There are no addresses found to send the email to." );

    ArrayList<String> ccList = new ArrayList<String>();
    if( cc == null )
        cc = "";

    if( cc.contains(";") ) {
        String[] arr = cc.split(";");
        for( String email : arr )
        {
            if( !"".equals( email.trim() ) )
                ccList.add( email.trim() );
        }
    }
    else if( !"".equals( cc.trim() ) )
        ccList.add(cc);



    EmailModule.mail( this.getContext(), config, body, subject, toList, (ccList.size() > 0 ? ccList : null), null, null);


    return true;
    // END USER CODE
}

/**
 * Returns a string representation of this action
 */
@Override
public String toString()
{
    return "SendEmail";
}

// BEGIN EXTRA CODE
// END EXTRA CODE

}

answered