|
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.
Send a mail |
API Support
|
Post subject: Send a mail |
Post rating: 0
|
Posted: Fri 05 Dec, 2008, 23:12
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
The sample sends email to [email protected]. Please notice, JavaMail API library that is not part of standard library set of JForex and have to be downloaded manually from https://java.sun.com/products/javamail/downloads/index.html. You need to provide full path to the downloaded library in @Library annotation. package jforex;
import java.util.Date; import java.util.Properties;
import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;
import com.dukascopy.api.*;
/** * @author Denis Larka 13 Jun 2008 */
@Library("C:\\temp\\mail-1.4.jar") public class TestMail implements IStrategy {
@Configurable("SMTP Server") public String smtpServer = "localhost";
private IContext context;
private IConsole console;
private IEngine engine;
private IChart chart;
public void onAccount(IAccount account) throws JFException { }
public void onMessage(IMessage message) throws JFException { }
public void onStart(IContext context) throws JFException { this.context = context; this.engine = context.getEngine(); this.console = context.getConsole(); }
public void onStop() throws JFException { }
public void onTick(Instrument instrument, ITick tick) throws JFException { try { print(smtpServer); sendMail("[email protected]", "[email protected]", "Hello", "SWFX"); } finally { context.stop(); } }
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { }
private void print(String str) { console.getOut().println(str); }
public boolean sendMail(String from, String to, String subj, String text) throws JFException { try { Properties props = new Properties();//System.getProperties(); // Attaching to default Session, or we could start a new one props.setProperty("mail.smtp.host", smtpServer); Session session = null; session = Session.getInstance(props); // Create a new message Message msg = new MimeMessage(session); // Set the FROM field msg.setFrom(new InternetAddress(from)); // Set the TO fields msg.setRecipient(RecipientType.TO, new InternetAddress(to)); // Set the subject and body text msg.setSubject(subj); msg.setText(text); // Set some other header information msg.setHeader("SWFX-Mailer", "SWFX"); msg.setSentDate(new Date()); // Send the message Transport.send(msg); return true; } catch (Exception e) { throw new JFException(e); } }
}
Attachments: |
TestMail.java [2.54 KiB]
Downloaded 890 times
|
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on
this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control
on their content. Anyone accessing this webpage and downloading or otherwise making use of any document,
data or information found on this webpage shall do it on his/her own risks without any recourse against
Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from
the use and/or reliance on any document, data or information found on this webpage.
|
|
|
|
|
 |
[teknomage]
|
Post subject: Re: Send a mail |
Post rating: 0
|
Posted: Fri 20 Feb, 2009, 18:41
|
|
User rating: 0
Joined: Mon 22 Dec, 2008, 13:00 Posts: 17
|
Hi to the Dukas IT team! When I tried compiling this example, I got quite a few errors. I had downloaded the javamail.jar api and placed it in C drive. I also changed the library annotation to "C:\\javamail.jar". I didn't change any other line of code in your example. Despite this, the following errors were generated (i removed my computer name for the sake of privacy): 2009-02-20 23:01:28 Transport cannot be resolved 2009-02-20 23:01:28 ^^^^^^^^^ 2009-02-20 23:01:28 Transport.send(msg); 2009-02-20 23:01:28 14. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 86) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 InternetAddress cannot be resolved to a type 2009-02-20 23:01:28 ^^^^^^^^^^^^^^^ 2009-02-20 23:01:28 msg.setRecipient(RecipientType.TO, new InternetAddress(to)); 2009-02-20 23:01:28 13. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 78) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 RecipientType cannot be resolved 2009-02-20 23:01:28 ^^^^^^^^^^^^^ 2009-02-20 23:01:28 msg.setRecipient(RecipientType.TO, new InternetAddress(to)); 2009-02-20 23:01:28 12. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 78) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 InternetAddress cannot be resolved to a type 2009-02-20 23:01:28 ^^^^^^^^^^^^^^^ 2009-02-20 23:01:28 msg.setFrom(new InternetAddress(from)); 2009-02-20 23:01:28 11. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 76) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 MimeMessage cannot be resolved to a type 2009-02-20 23:01:28 ^^^^^^^^^^^ 2009-02-20 23:01:28 Message msg = new MimeMessage(session); 2009-02-20 23:01:28 10. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 74) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 Message cannot be resolved to a type 2009-02-20 23:01:28 ^^^^^^^ 2009-02-20 23:01:28 Message msg = new MimeMessage(session); 2009-02-20 23:01:28 9. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 74) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 Session cannot be resolved 2009-02-20 23:01:28 ^^^^^^^ 2009-02-20 23:01:28 session = Session.getInstance(props); 2009-02-20 23:01:28 8. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 72) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 Session cannot be resolved to a type 2009-02-20 23:01:28 ^^^^^^^ 2009-02-20 23:01:28 Session session = null; 2009-02-20 23:01:28 7. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 71) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 The import javax.mail cannot be resolved 2009-02-20 23:01:28 ^^^^^^^^^^ 2009-02-20 23:01:28 import javax.mail.internet.MimeMessage; 2009-02-20 23:01:28 6. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 11) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 The import javax.mail cannot be resolved 2009-02-20 23:01:28 ^^^^^^^^^^ 2009-02-20 23:01:28 import javax.mail.internet.InternetAddress; 2009-02-20 23:01:28 5. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 10) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 The import javax.mail cannot be resolved 2009-02-20 23:01:28 ^^^^^^^^^^ 2009-02-20 23:01:28 import javax.mail.Message.RecipientType; 2009-02-20 23:01:28 4. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 9) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 The import javax.mail cannot be resolved 2009-02-20 23:01:28 ^^^^^^^^^^ 2009-02-20 23:01:28 import javax.mail.Transport; 2009-02-20 23:01:28 3. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 8) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 The import javax.mail cannot be resolved 2009-02-20 23:01:28 ^^^^^^^^^^ 2009-02-20 23:01:28 import javax.mail.Session; 2009-02-20 23:01:28 2. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 7) 2009-02-20 23:01:28 ---------- 2009-02-20 23:01:28 The import javax.mail cannot be resolved 2009-02-20 23:01:28 ^^^^^^^^^^ 2009-02-20 23:01:28 import javax.mail.Message; 2009-02-20 23:01:28 1. ERROR in C:\DOCUME~1\.....\LOCALS~1\Temp\jfxide\tmp\TestMail.java (at line 6) Please help me with this...
|
|
|
|
 |
API Support
|
Post subject: Re: Send a mail |
Post rating: 0
|
Posted: Tue 22 Sep, 2009, 13:33
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Hi, when you download javamail archive, it comes as ZIP archive. For example create directory in "C:\tmp", then unpack your downloaded "javamail-1.4.2.zip" archive to this directory. You should get new directory "C:\tmp\javamail-1.4.2". There is your needed file mail.jar .So in this case in @Library annotation, you should put "C:\\tmp\\javamail-1.4.2\\mail.jar".
|
|
|
|
 |
[mharding]
|
Post subject: Re: Send a mail |
Post rating: 0
|
Posted: Wed 14 Oct, 2009, 12:49
|
|
User rating: 0
Joined: Tue 13 Oct, 2009, 20:35 Posts: 7
|
Hi Support Is there a way to suppress the security alerts associated with this file ? Attachment:
File comment: screenshot of security alerts
alerts.GIF [22.19 KiB]
Downloaded 847 times
Thanks in advance Mark
|
|
|
|
 |
API Support
|
Post subject: Re: Send a mail |
Post rating: 0
|
Posted: Wed 14 Oct, 2009, 13:35
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
|
|
|
 |
[mharding]
|
Post subject: Re: Send a mail |
Post rating: 0
|
Posted: Wed 14 Oct, 2009, 16:07
|
|
User rating: 0
Joined: Tue 13 Oct, 2009, 20:35 Posts: 7
|
I added this line to \Program Files\Java\jre6\lib\security\java.policy :
permission java.net.SocketPermission "*", "accept,connect,resolve";
it works !
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|