-
Notifications
You must be signed in to change notification settings - Fork 0
/
Envelope.java
86 lines (73 loc) · 2.37 KB
/
Envelope.java
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
package edu.ucm.cs.lab2;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
/* $Id: Envelope.java,v 1.8 1999/09/06 16:43:20 kangasha Exp $ */
/**
* SMTP envelope for one mail message.
*
* @author Jussi Kangasharju
*/
public class Envelope {
/* SMTP-sender of the message (in this case, contents of From-header. */
public String sender;
/* SMTP-recipient, or contents of To-header. */
public String recipient;
String recipientInCc;
String recipientInBcc;
/* Target MX-host */
public String destHost;
public InetAddress destAddr;
/* The actual message */
public MessageBuilder message;
/* Create the envelope. */
public Envelope(MessageBuilder mb, String localServer) throws UnknownHostException {
/* Get sender and recipient. */
sender = mb.getFrom();
recipient = mb.getTo();
recipientInCc=mb.getCc();
recipientInBcc=mb.getBcc();
/* Get message. We must escape the message to make sure that
there are no single periods on a line. This would mess up
sending the mail. */
message = escapeMessage(mb);
/* Take the name of the local mailserver and map it into an
* InetAddress */
destHost = localServer;
try {
destAddr = InetAddress.getByName(destHost);
} catch (UnknownHostException e) {
System.out.println("Unknown host: " + destHost);
System.out.println(e);
throw e;
}
return;
}
/* Escape the message by doubling all periods at the beginning of
a line. */
private MessageBuilder escapeMessage(MessageBuilder message) {
String escapedBody = "";
String token;
StringTokenizer parser = new StringTokenizer(message.Body, "\n", true);
while(parser.hasMoreTokens()) {
token = parser.nextToken();
if(token.startsWith(".")) {
token = "." + token;
}
escapedBody += token;
}
message.Body = escapedBody;
return message;
}
/* For printing the envelope. Only for debug. */
public String toString() {
String res = "Sender: " + sender + '\n';
res += "Recipient: " + recipient + '\n';
res += "Recipient in Cc: " + recipientInCc + '\n';
res += "Recipient in Bcc: " + recipientInBcc + '\n';
res += "MX-host: " + destHost + ", address: " + destAddr + '\n';
res += "Message:" + '\n';
res += message.toString();
return res;
}
}