KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > mail > EMailSender


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Runtime state of the engine
17  */

18 package org.apache.axis2.transport.mail;
19
20 import org.apache.axis2.engine.AxisFault;
21 import org.apache.axis2.transport.EmailReceiver;
22
23 import javax.mail.*;
24 import javax.mail.internet.AddressException JavaDoc;
25 import javax.mail.internet.InternetAddress JavaDoc;
26 import javax.mail.internet.MimeMessage JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 /**
30  * @author hemapani
31  *
32  * To change the template for this generated type comment go to
33  * Window>Preferences>Java>Code Generation>Code and Comments
34  */

35 public class EMailSender {
36     private String JavaDoc user;
37     private String JavaDoc host;
38     private String JavaDoc smtpPort;
39     private String JavaDoc password;
40
41     public EMailSender(String JavaDoc user, String JavaDoc host, String JavaDoc smtpPort, String JavaDoc password) {
42         this.user = user;
43         this.host = host;
44         this.smtpPort = smtpPort;
45         this.password = password;
46     }
47
48     public void send(String JavaDoc subject, String JavaDoc targetEmail, String JavaDoc message) throws AxisFault {
49         try {
50             final PasswordAuthentication authentication =
51                 new PasswordAuthentication(user, password);
52             Properties JavaDoc props = new Properties JavaDoc();
53             props.put("mail.user", user);
54             props.put("mail.host", host);
55             props.put("mail.store.protocol", "pop3");
56             props.put("mail.transport.protocol", "smtp");
57             props.put("mail.smtp.port", smtpPort);
58             Session session = Session.getInstance(props, new Authenticator() {
59                 protected PasswordAuthentication getPasswordAuthentication() {
60                     return authentication;
61                 }
62             });
63
64             MimeMessage JavaDoc msg = new MimeMessage JavaDoc(session);
65             msg.setFrom(new InternetAddress JavaDoc((user)));
66             msg.addRecipient(Message.RecipientType.TO, new InternetAddress JavaDoc(targetEmail));
67             msg.setSubject(subject);
68             
69             msg.addHeaderLine("Content-Type: text/plain; charset=us-ascii");
70             
71             msg.setText(message);
72             msg.setHeader("Content-Transfer-Encoding", "7bit");
73             Transport.send(msg);
74         } catch (AddressException JavaDoc e) {
75             throw new AxisFault(e);
76         } catch (MessagingException e) {
77             throw new AxisFault(e);
78         }
79     }
80
81     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
82
83         String JavaDoc user = "hemapani";
84         String JavaDoc host = "127.0.0.1";
85         String JavaDoc smtpPort = "25";
86         String JavaDoc password = "hemapani";
87         EMailSender sender = new EMailSender(user, host, smtpPort, password);
88
89         sender.send("Testing mail sending", "hemapani@127.0.0.1", "Hellp, testing");
90
91         EmailReceiver receiver = new EmailReceiver(user, host, "110", password);
92         receiver.connect();
93         Message JavaDoc[] msgs = receiver.receive();
94         if (msgs != null) {
95             for (int i = 0; i < msgs.length; i++) {
96                 MimeMessage JavaDoc msg = (MimeMessage JavaDoc) msgs[i];
97                 if (msg != null) {
98                     System.out.println(msg.getSender());
99                     System.out.println(msg.getContent());
100                 }
101                 msg.setFlag(Flags.Flag.DELETED, true);
102             }
103
104         }
105         receiver.disconnect();
106
107     }
108 }
109
Popular Tags