1 16 package org.apache.commons.net.smtp; 17 18 import java.io.IOException ; 19 import java.io.Writer ; 20 import java.net.InetAddress ; 21 import org.apache.commons.net.io.DotTerminatedMessageWriter; 22 23 122 123 public class SMTPClient extends SMTP 124 { 125 126 129 131 132 164 public boolean completePendingCommand() throws IOException 165 { 166 return SMTPReply.isPositiveCompletion(getReply()); 167 } 168 169 170 185 public boolean login(String hostname) throws IOException 186 { 187 return SMTPReply.isPositiveCompletion(helo(hostname)); 188 } 189 190 191 205 public boolean login() throws IOException 206 { 207 String name; 208 InetAddress host; 209 210 host = getLocalAddress(); 211 name = host.getHostName(); 212 213 if (name == null) 214 return false; 215 216 return SMTPReply.isPositiveCompletion(helo(name)); 217 } 218 219 220 236 public boolean setSender(RelayPath path) throws IOException 237 { 238 return SMTPReply.isPositiveCompletion(mail(path.toString())); 239 } 240 241 242 258 public boolean setSender(String address) throws IOException 259 { 260 return SMTPReply.isPositiveCompletion(mail("<" + address + ">")); 261 } 262 263 264 280 public boolean addRecipient(RelayPath path) throws IOException 281 { 282 return SMTPReply.isPositiveCompletion(rcpt(path.toString())); 283 } 284 285 286 302 public boolean addRecipient(String address) throws IOException 303 { 304 return SMTPReply.isPositiveCompletion(rcpt("<" + address + ">")); 305 } 306 307 308 309 348 public Writer sendMessageData() throws IOException 349 { 350 if (!SMTPReply.isPositiveIntermediate(data())) 351 return null; 352 353 return new DotTerminatedMessageWriter(_writer); 354 } 355 356 357 375 public boolean sendShortMessageData(String message) throws IOException 376 { 377 Writer writer; 378 379 writer = sendMessageData(); 380 381 if (writer == null) 382 return false; 383 384 writer.write(message); 385 writer.close(); 386 387 return completePendingCommand(); 388 } 389 390 391 411 public boolean sendSimpleMessage(String sender, String recipient, 412 String message) 413 throws IOException 414 { 415 if (!setSender(sender)) 416 return false; 417 418 if (!addRecipient(recipient)) 419 return false; 420 421 return sendShortMessageData(message); 422 } 423 424 425 426 446 public boolean sendSimpleMessage(String sender, String [] recipients, 447 String message) 448 throws IOException 449 { 450 boolean oneSuccess = false; 451 int count; 452 453 if (!setSender(sender)) 454 return false; 455 456 for (count = 0; count < recipients.length; count++) 457 { 458 if (addRecipient(recipients[count])) 459 oneSuccess = true; 460 } 461 462 if (!oneSuccess) 463 return false; 464 465 return sendShortMessageData(message); 466 } 467 468 469 481 public boolean logout() throws IOException 482 { 483 return SMTPReply.isPositiveCompletion(quit()); 484 } 485 486 487 488 501 public boolean reset() throws IOException 502 { 503 return SMTPReply.isPositiveCompletion(rset()); 504 } 505 506 507 521 public boolean verify(String username) throws IOException 522 { 523 int result; 524 525 result = vrfy(username); 526 527 return (result == SMTPReply.ACTION_OK || 528 result == SMTPReply.USER_NOT_LOCAL_WILL_FORWARD); 529 } 530 531 532 546 public String listHelp() throws IOException 547 { 548 if (SMTPReply.isPositiveCompletion(help())) 549 return getReplyString(); 550 return null; 551 } 552 553 554 569 public String listHelp(String command) throws IOException 570 { 571 if (SMTPReply.isPositiveCompletion(help(command))) 572 return getReplyString(); 573 return null; 574 } 575 576 577 590 public boolean sendNoOp() throws IOException 591 { 592 return SMTPReply.isPositiveCompletion(noop()); 593 } 594 595 } 596 | Popular Tags |