Send e-mail

One can send an e-mail from a strategy by using the JFUtils.sendMail method, which has text size and send frequency restrictions. This method allows one to send an e-mail in Remote Run mode.

Note: Available with JForex-API 2.9.10.

        JFUtils utils = context.getUtils();

        try {
             Future<IEmailResponse> future = utils.sendMail("[email protected]", "subject", "text");
             console.getOut().println("Waiting on response");
             IEmailResponse response = future.get(30, TimeUnit.SECONDS);
             if (response.isError()) {
                 console.getErr().println(response);
             } else {
                 console.getOut().println("e-mail successfully sent!");
             }
         } catch (Exception e) {
             e.printStackTrace(console.getErr());
         }

In order to send e-mails without the aforementioned restrictions, one can use the java mail library. The following strategy sends e-mail from the [email protected] to the specified recipient.

@RequiresFullAccess
@Library("C:/temp/mail.jar")
public class TestMail implements IStrategy {

    @Configurable("Recipient mail")
    public String mailAddress = "[email protected]";
    @Configurable("SMTP Server")
    public String smtpServer = "mailserver.company.com";
    private IConsole console;

    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        try {
            sendMail("[email protected]", mailAddress, "Hello 2", "Test mail \n Bye.");
        } catch (Exception e) {
            console.getErr().println("Failed to send email: " + e);
            e.printStackTrace(console.getErr());
            context.stop();
        }
        console.getOut().println("No exception -> check the e-mail!");
        context.stop();
    }

    public boolean sendMail(String from, String to, String subj, String text) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", smtpServer);
        Session session = Session.getInstance(props);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(RecipientType.TO, new InternetAddress(to));
        msg.setSubject(subj);
        msg.setText(text);
        Transport.send(msg);
        return true;
    }
//...

TestMail.java

The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.