KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > email > MailProc


1 package email;
2
3 import org.enhydra.shark.api.internal.toolagent.AppParameter;
4
5 import java.util.Properties JavaDoc;
6 import javax.mail.*;
7 import javax.mail.internet.*;
8
9 /** MailProc sends an e-mail using specified parameters:
10  *
11  * param1 - text_msg The text of the message
12  * param2 - subject Subject of the e-mail
13  * param3 - source_address Address of the sender (XXX@XXXX.XXX)
14  * param4 - destination_address Address of the receiver (XXX@XXXX.XXX)
15  * param5 - user If the server requires athentication
16  * Can be an empty string ""
17  * param6 - password Password the user needs to authenticate
18  * Can be an empty string ""
19  * param7 - server IP address of de server
20  * param8 - port Number of the port (usually 25)
21  *
22  * @throws MessagingException If exists an error while sending the message
23  *
24  * @author Paloma Trigueros Cabezon
25  * @version 1.0
26  */

27
28
29 public class MailProc {
30    public static void execute (AppParameter param1, AppParameter param2, AppParameter param3, AppParameter param4, AppParameter param5, AppParameter param6, AppParameter param7, AppParameter param8)
31    {
32         try
33         {
34             System.out.println("Beginning MailProc...");
35
36             //Extracting parameters
37
System.out.println("Extracting parameters...");
38
39             /* This is an example of how to send a number (the result of
40       a calculation, for example) as the text of the message:
41                long text = param1.extract_longlong();
42                Long t = new Long(text);
43                String text_msg = t.toString(text);
44             */

45
46             String JavaDoc text_msg = param1.the_value.toString();
47             String JavaDoc subject = param2.the_value.toString();
48             String JavaDoc source_address = param3.the_value.toString();
49             String JavaDoc destination_address = param4.the_value.toString();
50             String JavaDoc user = param5.the_value.toString();
51             String JavaDoc password = param6.the_value.toString();
52             String JavaDoc server = param7.the_value.toString();
53             long port = ((Long JavaDoc)param8.the_value).longValue();
54
55             // Get system properties
56
Properties JavaDoc props = new Properties JavaDoc();
57
58             // Setup mail server
59
props.put("mail.smtp.host", server);
60             props.put("mail.smtp.port", "" + port);
61
62             // User name
63
props.put("mail.smtp.user", user);
64             props.put("mail.smtp.auth", "true");
65
66             // Get session
67
javax.mail.Session JavaDoc session = Session.getInstance(props,new SmtpAuthenticator(user,password));
68
69             // Define message
70
MimeMessage message = new MimeMessage(session);
71
72             message.setFrom(new InternetAddress(source_address));
73
74             message.addRecipient(Message.RecipientType.TO,
75                              new InternetAddress(destination_address));
76
77             message.setSubject(subject);
78
79             message.setContent(text_msg, "text/plain");
80
81             // Send message
82
Transport.send(message);
83
84         }
85         catch (Exception JavaDoc ex)
86         {
87             System.out.println("MailProc - Problems during execution of MailProc" + ex);
88         }
89         System.out.println("Finishing MailProc...");
90    }
91 }
92
Popular Tags