-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageBuilder.java
130 lines (110 loc) · 3.16 KB
/
MessageBuilder.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
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
124
125
126
127
128
129
130
package edu.ucm.cs.lab2;
import java.util.*;
import java.text.*;
/* $Id: Message.java,v 1.5 1999/07/22 12:10:57 kangasha Exp $ */
/**
* Mail message.
*
* @author Jussi Kangasharju
*/
public class MessageBuilder {
/* The headers and the body of the message. */
public String Headers;
public String Body;
/* Sender and recipient. With these, we don't need to extract them
from the headers. */
private String from;
private String to;
private String cc;
private String bcc;
/* To make it look nicer */
private static final String CRLF = "\r\n";
/* Create the message object by inserting the required headers from
RFC 822 (From, To, Date). */
public MessageBuilder(String fromEmail, String toEmails,String ccEmails,String bccEmails,String subject, String text) {
from = fromEmail.trim();
to = toEmails;
cc = ccEmails;
bcc = bccEmails;
Headers = "From: " + from + CRLF;
Headers += "To: " + to + CRLF;
Headers += "Subject: " + subject.trim() + CRLF;
/* A close approximation of the required format. Unfortunately
only GMT. */
SimpleDateFormat format =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
String dateString = format.format(new Date());
Headers += "Date: " + dateString + CRLF;
Body = text;
}
/* Check whether the message is valid. In other words, check that
both sender and recipient contain only one @-sign. */
public boolean isValid() {
int fromat = from.indexOf('@');
// Validate From emails
if(fromat < 1 || (from.length() - fromat) <= 1) {
System.out.println("Sender address is invalid");
return false;
}
if(fromat != from.lastIndexOf('@')) {
System.out.println("Sender address is invalid");
return false;
}
// Validate To Emails
int toat=to.trim().indexOf('@');
if(toat < 1 || (to.length() - toat) <= 1) {
System.out.println("Recipient address is invalid");
return false;
}
if(toat != to.lastIndexOf('@')) {
System.out.println("Recipient address is invalid");
return false;
}
// Validate CC Field Emails
int ccat=cc.trim().indexOf('@');
if(ccat < 1 || (cc.length() - ccat) <= 1) {
System.out.println("Recipient address in CC field is invalid");
return false;
}
if(ccat != cc.lastIndexOf('@')) {
System.out.println("Recipient address in CC field is invalid");
return false;
}
// Validate Bcc Field Emails
int bccat=bcc.trim().indexOf('@');
if(bccat < 1 || (bcc.length() - bccat) <= 1) {
System.out.println("Recipient address in BCC field is invalid");
return false;
}
if(bccat != bcc.lastIndexOf('@')) {
System.out.println("Recipient address in BCC field is invalid");
return false;
}
return true;
}
public String getFrom() {
return from;
}
public String getTo() {
return to;
}
/**
* @return the cc
*/
public String getCc() {
return cc;
}
/* For printing the message. */
public String toString() {
String res;
res = Headers + CRLF;
res += Body;
return res;
}
/**
* @return the bcc
*/
public String getBcc() {
return bcc;
}
}