Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

How to use javamail api to send email on remote server
 Post subject: How to use javamail api to send email on remote server Post rating: 0   New post Posted: Tue 04 Oct, 2016, 15:26 

User rating: 0
Joined: Wed 15 Jun, 2016, 12:31
Posts: 2
Location: Malaysia,
hi, i wrote a code to send me email on completion of each range bar...
It works on local run but failed when remotely run....
can you please help me?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package singlejartest;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.ITimedData;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Library;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.PriceRange;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IFeedListener;
import com.dukascopy.api.feed.IRangeBar;
import com.dukascopy.api.feed.util.RangeBarFeedDescriptor;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@Library("mail-1.5.0-b01.jar;activation-1.1.jar")

public class MarketObserver implements IStrategy, IFeedListener{
   
    private IContext myContext;
    private IConsole myConsole;
   
    private IFeedDescriptor feedDescriptor1 ;
    private IFeedDescriptor feedDescriptor2 ;
    private IFeedDescriptor feedDescriptor3 ;
    private IFeedDescriptor feedDescriptor4 ;
   
    private DateTimeFormatter dtf;

    public String mailAddress = "[email protected]";

    public String smtpServer = "mail.smtp.yahoo.com";

   
    @Override
    public void onStart(IContext context) throws JFException {
        this.myContext = context;
        this.myConsole = this.myContext.getConsole();
       
        feedDescriptor1 = new RangeBarFeedDescriptor(Instrument.EURUSD, PriceRange.valueOf(3), OfferSide.BID);
        feedDescriptor2 = new RangeBarFeedDescriptor(Instrument.EURUSD, PriceRange.valueOf(6), OfferSide.BID);
        feedDescriptor3 = new RangeBarFeedDescriptor(Instrument.EURUSD, PriceRange.valueOf(12), OfferSide.BID);
        feedDescriptor4 = new RangeBarFeedDescriptor(Instrument.EURUSD, PriceRange.valueOf(60), OfferSide.BID);

        this.myContext.setSubscribedInstruments(java.util.Collections.singleton(feedDescriptor1.getInstrument()), true);
       
       
        this.myContext.subscribeToFeed(feedDescriptor1, this);
        this.myContext.subscribeToFeed(feedDescriptor2, this);
        this.myContext.subscribeToFeed(feedDescriptor3, this);
        this.myContext.subscribeToFeed(feedDescriptor4, this);
   
       
        dtf = DateTimeFormatter.ofPattern("EEEE dd/MM/yyyy hh:mm:ss.SSS a").withZone(ZoneId.systemDefault());
       
       

    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
       
    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    @Override
    public void onMessage(IMessage message) throws JFException {
    }

    @Override
    public void onAccount(IAccount account) throws JFException {
    }

    @Override
    public void onStop() throws JFException {
    }

    @Override
    public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
         IRangeBar thisbar = (IRangeBar) feedData;
         
                try {
            SendMail temp = new SendMail();
            temp.sendMail("[email protected]", mailAddress,
                    feedDescriptor.getInstrument().toString()+" - "+feedDescriptor.getPriceRange().getPipCount()+" pips",
                   "Start Time: " + dtf.format(Instant.ofEpochMilli(thisbar.getTime()))+"\n"+
                           "End Time: " + dtf.format(Instant.ofEpochMilli(thisbar.getEndTime()))+"\n"+
                           "Open: " + thisbar.getOpen() +"\n"+
                           "High: " + thisbar.getHigh() +"\n"+
                           "Low: " + thisbar.getLow() + "\n"+
                           "Close: " + thisbar.getClose() + "\n"+
                           "Volume: " + thisbar.getVolume() +" million" + "\n"+
                           "Tick Count: " + thisbar.getFormedElementsCount()+ "\n"+
                           "Direction: " + direct(thisbar)
        );
        } catch (Exception e) {
            this.myConsole.getErr().println(e.toString());
        }

    }
   
    private int direct(IRangeBar arg1){
        if (arg1.getClose()>arg1.getOpen()){return 1;} else if (arg1.getClose()<arg1.getOpen()){return -1;}
        return 0;
    }
   
public class SendMail {
   
   private final String mailSMTPServer;
   private final String mailSMTPServerPort;
   
   SendMail() {
      mailSMTPServer = "smtp.gmail.com";
      mailSMTPServerPort = "465";
   }
   
   SendMail(String mailSMTPServer, String mailSMTPServerPort) {
      this.mailSMTPServer = mailSMTPServer;
      this.mailSMTPServerPort = mailSMTPServerPort;
   }
   
   public void sendMail(String from, String to, String subject, String message) {
       
      Properties props = new Properties();
 
                /*
                props.setProperty("proxySet","true");
                props.setProperty("socksProxyHost","192.168.155.1");
                props.setProperty("socksProxyPort","1080");
                */
 
      props.put("mail.transport.protocol", "smtp");
      props.put("mail.smtp.starttls.enable","true");
      props.put("mail.smtp.host", mailSMTPServer);
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.user", from);
      props.put("mail.debug", "true");
      props.put("mail.smtp.port", mailSMTPServerPort);
      props.put("mail.smtp.socketFactory.port", mailSMTPServerPort);
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");
       
       
      SimpleAuth auth = new SimpleAuth (from,"mypassword");
       
      Session session = Session.getInstance(props, auth);
      session.setDebug(true);
      Message msg = new MimeMessage(session);
 
      try {
         
         msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
         
         msg.setFrom(new InternetAddress(from));
         
         msg.setSubject(subject);
         
         msg.setContent(message,"text/plain");
 
      } catch (Exception e) {
         System.out.println(">> Error: Need to complete message");
      }
       
       
      Transport tr;
      try {
         tr = session.getTransport("smtp");
         
         tr.connect(mailSMTPServer, "login", "password");
         msg.saveChanges();
         
         tr.sendMessage(msg, msg.getAllRecipients());
         tr.close();
      } catch (Exception e) {
         
         System.out.println(">> Error: Send message");
      }
 
   }
}
 
class SimpleAuth extends Authenticator {
   public String username = null;
   public String password = null;
 
 
   public SimpleAuth(String user, String pwd) {
      username = user;
      password = pwd;
   }
 
   @Override
   protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication (username,password);
   }
}

}


 
 Post subject: Re: How to use javamail api to send email on remote server Post rating: 0   New post Posted: Tue 18 Oct, 2016, 06:46 
User avatar

User rating: 0
Joined: Wed 05 Mar, 2014, 17:24
Posts: 30
Location: Russian Federation, Tomsk
See here: https://www.dukascopy.com/wiki/#Send_e-mail


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com