/
Send.java
99 lines (91 loc) · 2.16 KB
/
Send.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
package mailClient;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Send extends Connection
{
private InternetAddress destination;
private String text;
private String subject;
private AskUser asker = new AskUser();
public Send()
{
super();
}
/**
* Sends the message. Uses superclass username, pass, host, etc.
*/
public void sendMessage()
{
if (text == null)
{
text = asker.askOpenEnded("Please enter a message to send: ");
}
if (destination == null)
{
while (destination == null)
{
try
{
destination = new InternetAddress(asker.askOpenEnded("Please enter a message recipient: "));
} catch (AddressException e)
{
System.out.println("Sorry, you entered an invalid email address.");
}
}
}
if (subject == null)
{
subject = asker.askOpenEnded("Please enter a subjectJ: ");
}
try
{
MimeMessage msg = new MimeMessage(super.session);
msg.setSubject(this.subject);
msg.setText(this.text);
msg.setFrom(new InternetAddress(super.username));
msg.addRecipient(Message.RecipientType.TO, this.destination);
Transport t = super.session.getTransport("smtps");
t.connect(super.host, super.username, super.password);
t.sendMessage(msg, msg.getAllRecipients());
} catch (MessagingException mex)
{
throw new MailException(mex.getMessage());
}
super.addMessagesToDB();
System.out.println("Successfully sent message.");
}
/**
* Sets the text of the message.
*
* @param text - String of what to send. Must not be null;
*/
public void setText(String text)
{
this.text = text;
}
public void setRecipient(String destination)
{
try
{
this.destination = new InternetAddress(destination);
} catch (AddressException e)
{
throw new MailException("The recipient " + destination
+ " isn't a valid address.");
}
}
public void setSubject(String subject)
{
this.subject = subject;
}
public void clearInfo()
{
this.destination = null;
this.subject = null;
this.text = null;
}
}