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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| package pack;
import java.io.*;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
public class msgsend {
public static void main(String[] arg){
String[] argv={"-T","smtp","-H","127.0.0.1","-U","root","-P","root",
"-s","Nouveau sujet du mail","-o","eblonvia@wanadoo.fr","-M","127.0.0.1","-d"};
/*String[] argv={"-T","smtp","-H","smtp.capgemini.fr","-U","eblonvia","-P","ecom97",
"-s","le sujet du mail","-o","manu@localhost","-M","127.0.0.1","-d"};*/
new msgsend(argv);
}
public msgsend(String[] argv) {
String to, subject = null, from = null,
cc = null, bcc = null, url = null;
String mailhost = null;
String mailer = "msgsend";
String protocol = null, host = null, user = null, password = null;
String record = null; // name of folder in which to record mail
boolean debug = false;
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
int optind;
for (optind = 0; optind < argv.length; optind++) {
if (argv[optind].equals("-T")) {
protocol = argv[++optind];
System.out.println("protocol : "+protocol);
} else if (argv[optind].equals("-H")) {
host = argv[++optind];
System.out.println("host : "+host);
} else if (argv[optind].equals("-U")) {
user = argv[++optind];
System.out.println("user : "+user);
} else if (argv[optind].equals("-P")) {
password = argv[++optind];
System.out.println("password : "+password);
} else if (argv[optind].equals("-M")) {
mailhost = argv[++optind];
System.out.println("mailhost : "+mailhost);
} else if (argv[optind].equals("-f")) {
record = argv[++optind];
System.out.println("record : "+record);
} else if (argv[optind].equals("-s")) {
subject = argv[++optind];
System.out.println("subject : "+subject);
} else if (argv[optind].equals("-o")) { // originator
from = argv[++optind];
} else if (argv[optind].equals("-c")) {
cc = argv[++optind];
} else if (argv[optind].equals("-b")) {
bcc = argv[++optind];
} else if (argv[optind].equals("-L")) {
url = argv[++optind];
} else if (argv[optind].equals("-d")) {
debug = true;
} else if (argv[optind].equals("--")) {
optind++;
break;
} else if (argv[optind].startsWith("-")) {
System.out.println(
"Usage: msgsend [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
System.out.println(
"\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
System.out.println(
"\t[-f record-mailbox] [-M transport-host] [-d] [address]");
System.exit(1);
} else {
break;
}
}
try {
if (optind < argv.length) {
// XXX - concatenate all remaining arguments
to = argv[optind];
System.out.println("To: " + to);
} else {
System.out.print("To: ");
System.out.flush();
to = in.readLine();
}
if (subject == null) {
System.out.print("Subject: ");
System.out.flush();
subject = in.readLine();
} else {
System.out.println("Subject: " + subject);
}
Properties props = System.getProperties();
// XXX - could use Session.getTransport() and Transport.connect()
// XXX - assume we're using SMTP
if (mailhost != null)
props.put("127.0.0.1", mailhost);
// Get a Session object
Session session = Session.getDefaultInstance(props, null);
if (debug)
session.setDebug(true);
// construct the message
Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc, false));
msg.setSubject(subject);
//collect(in, msg);
msg.setText("Un troisieme essai avec *@mimi.fr: Message numero 3 devant etre renvoye "+System.getProperty("line.separator")+"a eblonvia@hotmail.fr "+System.getProperty("line.separator")+" AVEC un bon forwarding ");
System.out.println("le point final");
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());
// send the thing off
Transport.send(msg);
System.out.println("\nMail was sent successfully.");
// Keep a copy, if requested.
if (record != null) {
// Get a Store object
Store store = null;
if (url != null) {
URLName urln = new URLName(url);
store = session.getStore(urln);
store.connect();
} else {
if (protocol != null)
store = session.getStore(protocol);
else
store = session.getStore();
// Connect
if (host != null || user != null || password != null)
store.connect(host, user, password);
else
store.connect();
}
// Get record Folder. Create if it does not exist.
Folder folder = store.getFolder(record);
if (folder == null) {
System.err.println("Can't get record folder.");
System.exit(1);
}
if (!folder.exists())
folder.create(Folder.HOLDS_MESSAGES);
Message[] msgs = new Message[1];
msgs[0] = msg;
folder.appendMessages(msgs);
System.out.println("Mail was recorded successfully.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void collect(BufferedReader in, Message msg)
throws MessagingException, IOException {
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != "\n") {
sb.append(line);
sb.append("\n");
System.out.println("line: "+line);
}
// If the desired charset is known, you can use
// setText(text, charset)
msg.setText(sb.toString());
}
} |
Partager