title image


Smiley Habe ich mal irgendwann gefunden...
Hallo,teste mal diese Klasse:import java.io.*;import java.net.*;import java.util.*;/**A JavaBeans component that implements the SMTP and POP3 protocols. These protocolsallow a client to send and retrieve email. @author Trevor Harmon@version 1.00*/public class MailClient implements java.io.Serializable{ private static final int SMTP_PORT = 25; // the socket port for sending mail private static final int POP3_PORT = 110; // the socket port for retrieving mail private static final String POP3_OK_MSG = "+OK"; private static final String POP3_ERROR_MSG = "-ERR"; private static final String SMTP_OK_MSG = "2"; private static final String SMTP_CONTINUE_MSG = "3"; private String user; // the user name for authorization private String password; // the user's password private String host; // the host server to finger private boolean loggedOnForRetrieval; // true means only allow message retrieval private Socket mailServer; // socket connection to mail server private BufferedReader inputStream; // input stream for mail socket private PrintWriter outputStream; // output stream for mail socket public MailClient() { try { host = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { host = "127.0.0.1"; } user = "anonymous"; password = user + "@" + host; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } /** Opens a connection to the mail server host and prepares for message retrieval. The host must support the POP3 protocol. */ public void logonForRetrieval() throws IOException { if (mailServer != null) { throw new IllegalStateException("Already connected to mail server"); } try { mailServer = new Socket(host, POP3_PORT); inputStream = new BufferedReader(new InputStreamReader(mailServer.getInputStream())); outputStream = new PrintWriter(mailServer.getOutputStream(), true); String response; response = inputStream.readLine(); if (response == null || !response.startsWith(POP3_OK_MSG)) { throw new IOException("No response from server"); } outputStream.println("USER " + user); response = inputStream.readLine(); if (response == null || !response.startsWith(POP3_OK_MSG)) { throw new IOException("Invalid user name " + user); } outputStream.println("PASS " + password); response = inputStream.readLine(); if (response == null || !response.startsWith(POP3_OK_MSG)) { throw new IOException("Invalid password " + password); } loggedOnForRetrieval = true; } catch (UnknownHostException e) { logoff(); throw new IOException("Unknown host"); } catch (IOException e) { logoff(); throw new IOException("Could not log on to " + host + ". Reason: " + e.getMessage()); } } /** Opens a connection to the mail server host and prepares for sending messages. The host must support the SMTP protocol. */ public void logonForSend() throws IOException { if (mailServer != null) { throw new IllegalStateException("Already connected to mail server"); } try { mailServer = new Socket(host, SMTP_PORT); inputStream = new BufferedReader(new InputStreamReader(mailServer.getInputStream())); outputStream = new PrintWriter(mailServer.getOutputStream(), true); String response; response = inputStream.readLine(); if (response == null || !response.startsWith(SMTP_OK_MSG)) { throw new IOException("No response from server"); } outputStream.println("HELO " + host); response = inputStream.readLine(); if (response == null || !response.startsWith(SMTP_OK_MSG)) { throw new IOException("Mail server rejected hello message"); } loggedOnForRetrieval = false; } catch (UnknownHostException e) { logoff(); throw new IOException("Unknown host"); } catch (IOException e) { logoff(); throw new IOException("Could not log on to " + host + ". Reason: " + e.getMessage()); } } /** Closes the connection to the mail server host. */ public void logoff() throws IOException { if (inputStream != null) { inputStream.close(); inputStream = null; } if (outputStream != null) { outputStream.println("QUIT"); outputStream.close(); outputStream = null; } if (mailServer != null) { mailServer.close(); mailServer = null; } } /** Returns the number of messages waiting in the user's mailbox. This function is valid only for message retrieval. */ public int getMessageCount() throws IOException { if (mailServer == null || !loggedOnForRetrieval) { throw new IllegalStateException("Not logged on for retrieval of messages"); } try { outputStream.println("STAT"); String response = inputStream.readLine(); if (response == null || !response.startsWith(POP3_OK_MSG)) { throw new NumberFormatException(); } int i = response.indexOf(' '); return Integer.parseInt(response.substring(i + 1, response.indexOf(' ', i + 1))); } catch (IOException e) { throw new IOException("Could not get message count. Reason: " + e.getMessage()); } catch (NumberFormatException e) { throw new IOException("Could not get message count. Reason: Invalid response from server"); } catch (StringIndexOutOfBoundsException e) { throw new IOException("Could not get message count. Reason: Invalid response from server"); } } /** Returns the given message number as a string. This function is only valid for message retrieval. @see #parseMessage */ public String getMessage(int messageNumber) throws IOException { if (mailServer == null || !loggedOnForRetrieval) { throw new IllegalStateException("Not logged on for retrieval of messages"); } try { outputStream.println("RETR " + messageNumber); String response = POP3_OK_MSG; while (response.startsWith(POP3_OK_MSG)) { response = inputStream.readLine(); if (response == null) { throw new IOException("Premature end of input data"); } else if (response.startsWith(POP3_ERROR_MSG)) { throw new IOException("Server returned error code"); } } String message = response + "\n"; response = ""; while ( !response.equals(".") ) { response = inputStream.readLine(); if (response == null) { throw new IOException("Premature end of input data"); } if ( !response.equals(".") ) { if ( response.startsWith(".") ) message = message + response.substring(1) + "\n"; else message = message + response + "\n"; } } return message; } catch (IOException e) { throw new IOException("Could not get message number " + messageNumber + ". Reason: " + e.getMessage()); } } /** Permanently deletes the given message number from the user's mailbox. This function is only valid for message retrieval. */ public void deleteMessage(int messageNumber) throws IOException { if (mailServer == null || !loggedOnForRetrieval) { throw new IllegalStateException("Not logged on for retrieval of messages"); } try { outputStream.println("DELE " + messageNumber); String response = inputStream.readLine(); if (response == null || !response.startsWith(POP3_OK_MSG)) { throw new IOException("Invalid response from server or invalid message number"); } } catch (IOException e) { throw new IOException("Could not delete message number " + messageNumber + ". Reason: " + e.getMessage()); } } /** Translates the given message string into a hashtable, where each key of the hashtable is a field of the message header. One extra key called "Body" is added to hold the message body. */ public Hashtable parseMessage(String message) { if (message == null || message.length() == 0) return null; Hashtable messageFields = new Hashtable(16); int i = 0; while ( message.charAt(i) != '\n' ) { String header, data = ""; int start; start = i; i = message.indexOf(':', i); header = message.substring(start, i++).trim(); while ( message.charAt(i) == ' ' || message.charAt(i) == '\t' ) { start = i; i = message.indexOf('\n', i); data += message.substring(start, i++).trim() + " "; } messageFields.put(header, data.trim()); } messageFields.put("Body", message.substring(i + 1)); return messageFields; } /** Sends the given message, where "from" is the return address, "to" is the destination address, "subject" is the message subject, and "body" is the text of the message. This function is only valid when logged on for sending messages. */ public void sendMessage(String from, String to, String subject, String body) throws IOException { Hashtable messageFields = new Hashtable(4); messageFields.put("From", from); messageFields.put("To", to); messageFields.put("Subject", subject); messageFields.put("Body", body); sendMessage(messageFields); } /** Sends the given message using the information stored in the given hashtable, where each key of the hashtable is a field of the message. At a minimum, the "To", "From", and "Body" fields must be specified. If "cc" or "bcc" are specified, they must be in lower case. The "To", "From", "cc", and "bcc" fields may contain multiple addresses, but each address must be enclosed by "". */ public void sendMessage(Hashtable messageFields) throws IOException { if (mailServer == null || loggedOnForRetrieval) { throw new IllegalStateException("Not logged on for sending messages"); } String from = (String) messageFields.get("From"); from = from.substring(from.indexOf('')); Vector recipients = new Vector(); parseAddressField((String)messageFields.get("To"), recipients); if (messageFields.get("cc") != null) parseAddressField((String)messageFields.get("cc"), recipients); if (messageFields.get("bcc") != null) parseAddressField((String)messageFields.get("bcc"), recipients); try { String response; outputStream.println("MAIL From:"); response = inputStream.readLine(); if (response == null || !response.startsWith(SMTP_OK_MSG)) { throw new IOException("Could not begin mail message"); } for (Enumeration e = recipients.elements(); e.hasMoreElements(); ) { outputStream.println("RCPT To:"); response = inputStream.readLine(); if (response == null || !response.startsWith(SMTP_OK_MSG)) { throw new IOException("Could not set destination address of message"); } } outputStream.println("DATA"); response = inputStream.readLine(); if (response == null || !response.startsWith(SMTP_CONTINUE_MSG)) { throw new IOException("Could not begin sending message body"); } for (Enumeration e = messageFields.keys(); e.hasMoreElements(); ) { String fieldName = (String) e.nextElement(); if ( !fieldName.equals("bcc") && !fieldName.equals("Body") ) { outputStream.print(fieldName + ": " + (String)messageFields.get(fieldName) + "\r\n"); } } outputStream.print("\r\n"); StringTokenizer tokenizer = new StringTokenizer((String)messageFields.get("Body"), "\r\n"); while (tokenizer.hasMoreTokens()) { String line = tokenizer.nextToken(); if (line.charAt(0) == '.') line = "." + line; outputStream.print(line + "\r\n"); } outputStream.println("\r\n.\r\n"); response = inputStream.readLine(); if (response == null || !response.startsWith(SMTP_OK_MSG)) { throw new IOException("Could not send message body"); } } catch (IOException e) { throw new IOException("Could not send message. Reason: " + e.getMessage()); } } /** Helper function to translate an address field into a list of addresses. */ private void parseAddressField(String addressField, Vector recipients) { int i = 0; while (true) { int start = addressField.indexOf(' int end = addressField.indexOf('>', i); if (start == -1 || end == -1) break; recipients.addElement(addressField.substring(start + 1, end)); i = end + 1; } } /** Override to ensure that the connection to the mail server is disconnected when this instance dies. */ protected void finalize() throws IOException { if (mailServer != null) logoff(); }}Frank

geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: