-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJmail.java
More file actions
141 lines (125 loc) · 3.27 KB
/
Jmail.java
File metadata and controls
141 lines (125 loc) · 3.27 KB
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
131
132
133
134
135
136
137
138
139
140
141
/*
Jmail : provides email functionality: static use.
written by Shafik Amin, 9-24-2003
*/
import java.net.*;
import java.util.*;
import java.io.*;
public class Jmail
{
private static Socket s;
private static PrintWriter out;
private static String hostName;
private static BufferedReader in;
/* command line use */
public static void main(String[] args)
{
if (args.length != 3)
{
System.out.println("\nJmail v1.0, by Shafik Amin");
System.out.println("USAGE : java Jmail <from> <to> <subject>");
System.out.println("\n(The body of the message is read from standard in)");
}
else
{
try
{
int byteCount = System.in.available();
byte[] buffer = new byte[byteCount];
System.in.read(buffer, 0, byteCount);
String body = new String(buffer);
System.out.println(body);
send(args[0], args[1], args[2], body);
}
catch(Exception e)
{
System.out.println("Error!\n " + e);
}
}
}
/* convenience version of send */
public static void send(String from, String to, String subject, String body)
{
Vector v = new Vector();
v.add(body);
send(from, to, subject, v);
}
/** Allows you to send an email anywhere from the program
@param fromWho the string that contains the emails "from who" field
@param toWho the string that the address of the destination
@param the databuffer to send
**/
public static void send(String fromWho,
String toWho,
String subject,
Vector dataBuffer)
{
try
{
s = new Socket("smtp.west.cox.net",25); //SMTP
hostName = InetAddress.getLocalHost().getHostName();
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader (s.getInputStream()));
}
catch (Exception e)
{
System.out.println(e + " Error getting your HostName! or the socket");
}
try
{
if (s == null)
s = new Socket("smtpgate.email.arizona.edu",25); //SMTP
}
catch (Exception e)
{
System.out.println(e + " Error creating the socket");
}
String theMessage = "\n" + makeStringFromBuffer(dataBuffer);
theMessage += "\r\n.";
try
{
out.println("HELO " + hostName );
out.flush();
out.println("MAIL FROM: " + "<" + fromWho + ">");
out.flush();
out.println("RCPT TO: " + "<" + toWho + ">" );
out.flush();
out.println("DATA");
out.flush();
out.println("From: " + "< " + fromWho + " >");
out.flush();
out.println("To: " + "< " + toWho +" >");
out.flush();
out.println("Subject: " + subject);
out.flush();
//System.out.println("Sending messeage:" + theMessage);
out.println(theMessage);
out.flush();
out.println("QUIT");
out.flush();
/* read the response off the server */
String l;
while ((l = in.readLine()) != null)
System.out.println(l);
}
catch(Exception e)
{
System.out.println("Error on BufferedReader:" + e);
}
try {s.close();}
catch(Exception e)
{
System.out.println(e + "socket closing error!" + e);
}
}
/* convert a buffer to a string */
private static String makeStringFromBuffer(Vector buffer)
{
String ret = "";
for (int i = 0; i < buffer.size() ; i++)
{
ret += (buffer.get(i)).toString();
}
return ret;
}
}