Hi
I have been trying to get a simple mail program working- eventually, I'd like a strategy or indicator to send me an email when an order is filled, but I'm having a few issues well before that stage.
I have downloaded and experimented with a couple of examples on this forum, the Java Mail Wikipedia page, and elsewhere, but I always seem to come up with 'address exception' and/or messaging exception. Here's my latest attempt:
package jforex;
import java.util.*;
import com.dukascopy.api.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Address;
import javax.activation.*;
import java.util.Properties;
import javax.mail.Message.RecipientType;
import com.sun.mail.smtp.*;
//import JFXMail;
public class MailDemo implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.context = context;
this.indicators = context.getIndicators();
this.userInterface = context.getUserInterface();
sendMail ();
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException {
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
private void sendMail() {
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = "[email protected]";
String from = "[email protected]";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "smtp.googlemail.com";
// Create properties, get Session
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
// To see what is going on behind the scene
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
//try {
// Instantiate a message
Message msg = new MimeMessage(session);
//Set message attributes
InternetAddress fromAddr = new InternetAddress ("to@gmailcom" ,false);
msg.setFrom (fromAddr);
//msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through Java");
msg.setSentDate(new Date());
// Set message content
msg.setText("This is a test of sending a " +
"plain text e-mail through Java.\n" +
"Here is line 2.");
//Send the message
Transport.send(msg);
//}
//catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
//mex.printStackTrace();
//}
}
}
I took out the try/catch parts to try to isolate the problem, before I did that, the code compiled, but didn't really seem to do anything. Here's the errors:
18:36:00 ----------
18:36:00 Unhandled exception type MessagingException
18:36:00 ^^^^^^^^^^^^^^^^^^^
18:36:00 Transport.send(msg);
18:36:00 8. ERROR in /tmp/jfxide/tmp/MailDemo.java (at line 90)
18:36:00 ----------
18:36:00 Unhandled exception type MessagingException
18:36:00 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18:36:00 "Here is line 2.");
18:36:00 "plain text e-mail through Java.\n" +
18:36:00 msg.setText("This is a test of sending a " +
18:36:00 7. ERROR in /tmp/jfxide/tmp/MailDemo.java (at line 85)
18:36:00 ----------
18:36:00 Unhandled exception type MessagingException
18:36:00 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
18:36:00 msg.setSentDate(new Date());
18:36:00 6. ERROR in /tmp/jfxide/tmp/MailDemo.java (at line 82)
18:36:00 ----------
18:36:00 Unhandled exception type MessagingException
18:36:00 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18:36:00 msg.setSubject("Test E-Mail through Java");
18:36:00 5. ERROR in /tmp/jfxide/tmp/MailDemo.java (at line 81)
18:36:00 ----------
18:36:00 Unhandled exception type MessagingException
18:36:00 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18:36:00 msg.setRecipients(Message.RecipientType.TO, address);
18:36:00 4. ERROR in /tmp/jfxide/tmp/MailDemo.java (at line 80)
18:36:00 ----------
18:36:00 Unhandled exception type AddressException
18:36:00 ^^^^^^^^^^^^^^^^^^^^^^^
18:36:00 InternetAddress[] address = {new InternetAddress(to)};
18:36:00 3. ERROR in /tmp/jfxide/tmp/MailDemo.java (at line 79)
18:36:00 ----------
18:36:00 Unhandled exception type MessagingException
18:36:00 ^^^^^^^^^^^^^^^^^^^^^^
18:36:00 msg.setFrom (fromAddr);
18:36:00 2. ERROR in /tmp/jfxide/tmp/MailDemo.java (at line 77)
18:36:00 ----------
18:36:00 Unhandled exception type AddressException
18:36:00 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18:36:00 InternetAddress fromAddr = new InternetAddress ("to@gmailcom" ,false);
18:36:00 1. ERROR in /tmp/jfxide/tmp/MailDemo.java (at line 76)
18:36:00 ----------
18:35:59 Compiling MailDemo.java
I have written quite a few (losing!) MT4 robots, but I'm pretty new to Java. Any ideas what on earth can be going on here? I'd be most grateful for any clues you can give.
Charlie