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.

Java mail - Address and Messaging exceptions.
 Post subject: Java mail - Address and Messaging exceptions. Post rating: 0   New post Posted: Fri 30 Sep, 2011, 19:54 

User rating: 0
Joined: Fri 30 Sep, 2011, 19:35
Posts: 3
Location: GB
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


 
 Post subject: Re: Java mail - Address and Messaging exceptions. Post rating: 0   New post Posted: Mon 03 Oct, 2011, 08:38 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider starting with a simple java application which sends an e-mail and if you succeed - integrate it within JForex strategy. The particular example in wiki assumes that you have an e-mail server available. If you don't - consider starting from here.


 
 Post subject: Re: Java mail - Address and Messaging exceptions. Post rating: 0   New post Posted: Wed 05 Oct, 2011, 17:27 

User rating: 0
Joined: Fri 30 Sep, 2011, 19:35
Posts: 3
Location: GB
Hi!

Thanks for your response.

Your suggestion of creating the simple mail program before trying to incorporate it into a strategy is exactly what I have been trying to do. Unfortunately, I have been trying with the code that you linked to I am still seeing the AddressException and MessagingException problems.

It's like I'm missing a bit of API or something, but for the life of me, I don't know what....

Charlie


 
 Post subject: Re: Java mail - Address and Messaging exceptions. Post rating: 0   New post Posted: Thu 06 Oct, 2011, 07:50 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
for the particular problem - you are likely to need a try/catch block, see:
https://java.syntaxerrors.info/index.php ... oException
On the other hand, as an alternative to sending e-mails, you might consider sound and message alerts:
https://www.dukascopy.com/wiki/index.php ... ce_alerter


 
 Post subject: Re: Java mail - Address and Messaging exceptions. Post rating: 0   New post Posted: Mon 10 Oct, 2011, 20:02 

User rating: 0
Joined: Fri 30 Sep, 2011, 19:35
Posts: 3
Location: GB
Thanks v much.

I'll take a look at all that info.

Charlie


 
 Post subject: Re: Java mail - Address and Messaging exceptions. Post rating: 0   New post Posted: Thu 20 Oct, 2011, 07:53 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You can also take a look at the following thread:
viewtopic.php?f=31&t=29995


 

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