KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > communication > EMailSender


1 /*
2  * Created on 07.04.2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.contineo.communication;
8
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 import javax.activation.DataHandler JavaDoc;
14 import javax.activation.DataSource JavaDoc;
15 import javax.activation.FileDataSource JavaDoc;
16 import javax.mail.Address JavaDoc;
17 import javax.mail.Multipart JavaDoc;
18 import javax.mail.Session JavaDoc;
19 import javax.mail.Transport JavaDoc;
20 import javax.mail.internet.InternetAddress JavaDoc;
21 import javax.mail.internet.MimeBodyPart JavaDoc;
22 import javax.mail.internet.MimeMessage JavaDoc;
23 import javax.mail.internet.MimeMultipart JavaDoc;
24
25 import org.contineo.core.config.SettingConfigurator;
26 /**
27  * @author Michael Scholz
28  *
29  * To change the template for this generated type comment go to
30  * Window>Preferences>Java>Code Generation>Code and Comments
31  */

32 public class EMailSender {
33
34     /**
35      *
36      */

37     public EMailSender() {
38     }
39
40     /**
41      * This method sends an email using the smtp-protocol. The email can be a simple mail
42      * or a multipart mail containing up to 5 attachments.
43      * @param account E-Mail account of the sender.
44      * @param email E-Mail which should be sent.
45      * @throws Exception
46      */

47     public void send(EMailAccount account, EMail email) throws Exception JavaDoc {
48         Properties JavaDoc props = new Properties JavaDoc();
49         props.put( "mail.smtp.host", account.getSmtpHost() );
50         props.put( "mail.smtp.auth", "true" );
51         Session JavaDoc sess = Session.getDefaultInstance(props);
52         javax.mail.Message JavaDoc message = new MimeMessage JavaDoc(sess);
53         InternetAddress JavaDoc from = new InternetAddress JavaDoc(account.getMailAddress());
54         InternetAddress JavaDoc[] to = email.getAddresses();
55         message.setFrom(from);
56         message.setRecipients(javax.mail.Message.RecipientType.TO, to);
57         message.setSubject(email.getSubject());
58         if (email.getAttachmentCount() > 0) {
59             SettingConfigurator conf = new SettingConfigurator();
60             String JavaDoc userdir = conf.getValue("userdir");
61             String JavaDoc filename = userdir + "/" + account.getUserName() + "/mails/" + email.getMessageId() + "/";
62             MimeBodyPart JavaDoc body = new MimeBodyPart JavaDoc();
63             body.setContent(email.getMessageText(), "text/plain");
64             Multipart JavaDoc mpMessage = new MimeMultipart JavaDoc();
65             mpMessage.addBodyPart(body);
66             Collection JavaDoc attachments = email.getAttachments();
67             Iterator JavaDoc iter = attachments.iterator();
68             while (iter.hasNext()) {
69                 Attachment att = (Attachment)iter.next();
70                 DataSource JavaDoc fdSource = new FileDataSource JavaDoc(filename + att.getFilename());
71                 DataHandler JavaDoc fdHandler = new DataHandler JavaDoc(fdSource);
72                 MimeBodyPart JavaDoc part = new MimeBodyPart JavaDoc();
73                 part.setDataHandler(fdHandler);
74                 part.setFileName(att.getFilename());
75                 mpMessage.addBodyPart(part);
76             }
77             message.setContent(mpMessage);
78         } else {
79             message.setContent(email.getMessageText(), "text/plain");
80         }
81         Transport JavaDoc trans = sess.getTransport("smtp");
82         trans.connect(account.getSmtpHost(), account.getAccountUser(), account.getAccountPassword());
83         Address JavaDoc adr[] = message.getAllRecipients();
84         trans.sendMessage(message, adr);
85         trans.close();
86     }
87 }
88
Popular Tags