JavaMail is er goed en uitgebreid, maar daarom ook vrij complex. Ik gebruik zelf een eigen wrapper om JavaMail om simpele teksts mailtjes te versturen. Er zitten een paar faciliteiten in die je er even uit moet halen (de Workers), maar verder kan je het wel gebruiken of als voorbeeld nemen.
Er zijn drie klassen:
MailMessage
MailSystem
MailAccount
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| package org.pandoramix.mail;
import javax.mail.internet.InternetAddress;
public interface MailAccount
{
/**
* Returns the protocol which must be used for sending mail.
* Usually this will be 'smtp'.
*/
public String getOutGoingMailProtocol();
/**
* Returns the address of the server which must be used to send mail.
* This should be something like 'smtp.yourprovider.com'.
*/
public String getOutGoingMailServer();
/**
* Return the InternetAddress (which is a wrapper around a name and
* e-mail address) of the owner of this account.
*/
public InternetAddress getOwner();
} |
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
| package org.pandoramix.mail;
import javax.mail.internet.InternetAddress;
public class MailMessage
{
private InternetAddress to;
private String subject;
private String content;
/**
* Creates a new MailMessage without a recipient, subject and content.
*/
public MailMessage()
{
this(null);
}
/**
* Creates a new MailMessage to the specified recipient without subject and content.
*
* @param recipient the recipient of the MailMessage
*/
public MailMessage(InternetAddress recipient)
{
this(recipient, "");
}
/**
* Creates a new MailMessage to the specified recipient and with the specified subject without content.
*
* @param recipient the recipient of the MailMessage
* @param subject the subject of the MailMessage
*/
public MailMessage(InternetAddress recipient, String subject)
{
this(recipient, subject, "");
}
/**
* Creates a new MailMessage to the specified recipient and with the specified subject without content.
*
* @param recipient the recipient of the MailMessage
* @param subject the subject of the MailMessage
* @param content the content of the MailMessage
*/
public MailMessage(InternetAddress recipient, String subject, String content)
{
super();
this.to = recipient;
this.content = content;
this.subject = subject;
}
/**
* Returns the recipient of this MailMessage
*/
public InternetAddress getRecipient()
{
return to;
}
/**
* Sets the recipient of this MailMessage
*
* @param address the new recipient of this MailMessage
*/
public void setRecipient(InternetAddress address)
{
this.to = address;
}
/**
* Returns the subject of this MailMessage
*/
public String getSubject()
{
return subject;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public String getContent()
{
return content;
}
public void setContent(String content)
{
this.content = content;
}
} |
en tot slot MailSystem (deze moet je even wat aanpassen)
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
| package org.pandoramix.mail;
import org.pandoramix.work.ProgressingWorker;
import org.pandoramix.work.helpers.DefaultProgressingWorker;
import org.pandoramix.work.helpers.DefaultProgressingWorkerRunnable;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* MailSystem is a simple wrapper around JavaMail(TM) for sending MailMessages.
* A MailSystem can be created in an easy way with a MailAccount. A MailSystem
* is able to send MailMessages in a ProgressingWorker or just immediately.
*
* @version 1.0 27/01/2001
* @author Martin Bravenboer
*/
public class MailSystem
{
private MailAccount account;
private Properties accountProperties;
/**
* Creates a MailSystem with the specified MailAccount
*
* @param account the MailAccount of this MailSystem
*/
public MailSystem(MailAccount account)
{
this.account = account;
accountProperties = System.getProperties();
String protocol = "mail." + account.getOutGoingMailProtocol() + ".host";
accountProperties.put(protocol, account.getOutGoingMailServer());
}
/**
* Sends the specified MailMessage.
* When the method returns the message is send. When
* the MailSystem is unable to send the message an
* Exception will be thrown.
*
* @param message the MailMessage which must be send
*/
public void sendMail(MailMessage message) throws MessagingException
{
Message mailMessage = createMessage(message);
Transport.send(mailMessage);
}
/**
* Sends the specified MailMessag in a ControlledWorker.
* The ControlledWorker is not started and is not cancellable.
* A program might want to use the ControlledWorker in a userinterface
* to show the progress of the transport or to show exceptions.
* This message returns immediately without doing anything with
* respect to sendin the message.
*
* @param message the MailMessage which must be send
*/
public ProgressingWorker sendMailInWorker(MailMessage message) throws MessagingException
{
return new DefaultProgressingWorker(new Sender(message), "Pandoramix.org Mail is sending your e-mail.", 0, 2);
}
private Message createMessage(MailMessage message) throws MessagingException
{
Session session = Session.getDefaultInstance(accountProperties);
Message result = new MimeMessage(session);
result.setFrom(account.getOwner());
result.setRecipients(Message.RecipientType.TO, new InternetAddress[]{message.getRecipient()});
result.setSubject(message.getSubject());
result.setText(message.getContent());
result.setHeader("X-mailer","Pandoramix.org Mail");
result.setSentDate(new Date());
return result;
}
private class Sender extends DefaultProgressingWorkerRunnable
{
private MailMessage message;
public Sender(MailMessage message)
{
super();
this.message = message;
}
public void run()
{
ProgressingWorker worker = super.getProgressingWorker();
try
{
worker.setStage("Preparing e-mail for transport");
Message mailMessage = createMessage(message);
worker.increaseProgress();
worker.setStage("Sending e-mail");
Transport.send(mailMessage);
worker.increaseProgress();
worker.setFinished();
}
catch(Exception exc)
{
worker.fatalExceptionOccured(exc);
}
}
}
} |
Sommige dingen kunnen nog wat mooier, maar het is in ieder geval een makkelijk voorbeeld

.