1 22 23 package gnu.mail.providers.nntp; 24 25 import java.io.IOException ; 26 import javax.mail.Address ; 27 import javax.mail.AuthenticationFailedException ; 28 import javax.mail.Message ; 29 import javax.mail.MessagingException ; 30 import javax.mail.Session ; 31 import javax.mail.Transport ; 32 import javax.mail.URLName ; 33 import javax.mail.internet.MimeMessage ; 34 import javax.mail.internet.NewsAddress ; 35 36 import gnu.inet.nntp.NNTPConnection; 37 import gnu.inet.nntp.PostStream; 38 39 47 public class NNTPTransport extends Transport 48 { 49 50 NNTPConnection connection; 51 52 57 public NNTPTransport(Session session, URLName url) 58 { 59 super(session, url); 60 } 61 62 66 protected boolean protocolConnect(String host, int port, String username, 67 String password) 68 throws MessagingException 69 { 70 try 71 { 72 if (port<0) 73 port = NNTPConnection.DEFAULT_PORT; 74 connection = new NNTPConnection(host, port, username, password, 75 debug); 76 if (username!=null && password!=null) 77 { 78 return connection.authinfo(username, password); 81 } 82 else 83 return true; 84 } 85 catch (IOException e) 86 { 87 throw new MessagingException (e.getMessage(), e); 88 } 89 catch (SecurityException e) 90 { 91 if (username!=null && password!=null) 92 throw new AuthenticationFailedException (e.getMessage()); 93 else 94 return false; 95 } 96 } 97 98 102 public void close() 103 throws MessagingException 104 { 105 try 106 { 107 synchronized (connection) 108 { 109 connection.quit(); 110 } 111 } 112 catch (IOException e) 113 { 114 throw new MessagingException (e.getMessage(), e); 115 } 116 super.close(); 117 } 118 119 124 public void sendMessage(Message message, Address [] addresses) 125 throws MessagingException 126 { 127 addresses = message.getRecipients(MimeMessage.RecipientType.NEWSGROUPS); 130 boolean ok = (addresses.length>0); 131 if (!ok) 132 throw new MessagingException ("No recipients specified"); 133 for (int i=0; i<addresses.length; i++) 134 { 135 if (!(addresses[i] instanceof NewsAddress )) 136 { 137 ok = false; 138 break; 139 } 140 } 141 if (!ok) 142 throw new MessagingException ("Newsgroup recipients must be specified "+ 143 "as type NewsAddress"); 144 145 try 146 { 147 synchronized (connection) 148 { 149 PostStream out = connection.post(); 150 message.writeTo(out); 151 out.close(); 152 } 153 } 154 catch (IOException e) 155 { 156 throw new MessagingException (e.getMessage(), e); 157 } 158 } 159 160 public static void main(String [] args) 162 { 163 try 164 { 165 Session session = Session.getInstance(System.getProperties(), null); 167 168 javax.mail.internet.MimeMessage message = 170 new javax.mail.internet.MimeMessage (session); 171 message.setFrom(new javax.mail.internet.InternetAddress ("dog@gnu.org")); 172 Address [] recipients = { new NewsAddress ("alt.test") }; 173 message.setRecipients(MimeMessage.RecipientType.NEWSGROUPS, 174 recipients); 175 message.setSubject("Test"); 176 message.setText("This is a test.", "iso-8859-1"); 177 178 URLName url = new URLName ("nntp-post://localhost"); 180 Transport transport = session.getTransport(url); 181 transport.connect(); 182 transport.sendMessage(message, message.getAllRecipients()); 183 transport.close(); 184 } 185 catch (MessagingException e) 186 { 187 e.printStackTrace(); 188 Exception e2 = e.getNextException(); 189 if (e2!=null) 190 { 191 System.out.println("Next exception:"); 192 e2.printStackTrace(); 193 } 194 } 195 } 196 197 } 198 | Popular Tags |