读书人

Email 有关问题?

发布时间: 2012-06-14 16:00:31 作者: rapoo

Email 问题???
java.lang.NullPointerException
at com.shenfeinfo.protocol.smtp.SmtpConnectionPoolHandler.handleConnection(SmtpConnectionPoolHandler.java:249)
at com.shenfeinfo.protocol.smtp.SmtpConnectionPoolHandler.run(SmtpConnectionPoolHandler.java:132)
at java.lang.Thread.run(Thread.java:662)
2012-06-08 11:00:08.008 | ERROR | com.shenfeinfo.protocol.smtp.SmtpConnectionPoolHandler | ###Sned email failure######java.lang.NullPointerException

[解决办法]
public class TestSMTP extends Thread{

Multipart mp = null;



String host = "192.168.1.109";
int port = 125;
String userName = "tiger@extmail.org";
String password = "shenfeinfo";

String from = "tiger@extmail.org";
String to = "shenfeinfo@extmail.org";
private static int total = 1;
private static long TIME;

public TestSMTP(Multipart _mp){
this.mp = _mp;
}

public void run(){
testSmtp(mp);
}

public void testSmtp(Multipart mp){
long start = System.currentTimeMillis();
try {
Properties props = System.getProperties();
props.put("mail.smtp.host",host);
// props.put("mail.smtp.port",26);
props.put("mail.smtp.localhost","150");
props.put("mail.smtp.auth","true");
Session session = Session.getInstance(props, null);
session.setDebug(false);
Message msg = new MimeMessage(session);
//From
msg.setFrom(new InternetAddress(from));
//To
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));

msg.setSubject("Heisann "+System.currentTimeMillis());
msg.setText("Med vennlig hilsennTov Are Jacobsen");
msg.setHeader("X-Mailer", "Tov Are's program");
msg.setSentDate(new Date());
SMTPTransport t =(SMTPTransport)session.getTransport("smtp");
t.connect(host,port, userName, password);
msg.setContent(mp);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
} catch (AddressException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (SendFailedException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("Time:"+(end-start)/1000);
}

public static Multipart getFileData(String filePath){
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
String fileNames[] = new String[]{filePath};
String filename = null;
Vector file = new Vector();
try {
mbp.setContent("Med vennlig hilsennTov Are Jacobsen", "text/html;charset=utf-8");
mp.addBodyPart(mbp);
if(fileNames != null && fileNames.length > 0){//有附件
file.addAll(Arrays.asList(fileNames));
Enumeration efile=file.elements();
while(efile.hasMoreElements()){
mbp=new MimeBodyPart();
filename=efile.nextElement().toString(); //选择出每一个附件名
FileDataSource fds=new FileDataSource(filename); //得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
String name = fds.getName();
name = name.substring(name.indexOf("_") + 1,name.length());
mbp.setFileName(name); //得到文件名同样至入BodyPart
mp.addBodyPart(mbp);
}
file.removeAllElements();


}
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mp;
}

}
[解决办法]
package test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class popServer {
private Selector selector;
private ByteBuffer readBuffer = ByteBuffer.allocate(1024*1024);// 调整缓存的大小可以看到打印输出的变化
private Map<SocketChannel, byte[]> clientMessage = new java.util.concurrent.ConcurrentHashMap<SocketChannel, byte[]>();
private Map<SocketChannel, Boolean> clientStatus = new java.util.concurrent.ConcurrentHashMap<SocketChannel, Boolean>();

private int clientCount = 0;

private String host = "localhost";
private int port = 25;

private boolean startData = false;
private boolean endData = false;
private long uid = 255;
public popServer(String host, int port) {
this.host = host;
this.port = port;
}

public void start() throws IOException {

ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ssc.socket().bind(new InetSocketAddress(host, port));
selector = Selector.open();
ssc.register(selector, SelectionKey.OP_ACCEPT);
while (!Thread.currentThread().isInterrupted()) {
selector.select();
Set<SelectionKey> keys = selector.selectedKeys();

Iterator<SelectionKey> keyIterator = keys.iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (!key.isValid()) {
continue;
}
if (key.isAcceptable()) {
accept(key);
} else if (key.isReadable()) {
read(key);
} else if (key.isWritable()) {
write(key);
}
keyIterator.remove();
}
}
}

private void read(SelectionKey key) {
SocketChannel socketChannel = (SocketChannel) key.channel();
this.readBuffer.clear();
int numRead;
try {
numRead = socketChannel.read(this.readBuffer);
/**
* numRead - 读取缓存区大?? 大于0表示读取成功,否则读取失败.
*/
if (numRead > 0) {

byte[] newBytes = new byte[numRead];
System.arraycopy(readBuffer.array(), 0, newBytes, 0, numRead);
if (newBytes[0] == 68 && newBytes[1] == 65 && newBytes[2] == 84
&& newBytes[3] == 65) {// DATA 68,65,84,65
clientMessage.put(socketChannel, newBytes);
socketChannel.register(selector, SelectionKey.OP_WRITE);
clientStatus.put(socketChannel, true);
} else if (clientStatus.get(socketChannel)!=null && clientStatus.get(socketChannel)==true) {
if (newBytes[numRead - 5] == 13
&& newBytes[numRead - 4] == 10
&& newBytes[numRead - 3] == 46
&& newBytes[numRead - 2] == 13
&& newBytes[numRead - 1] == 10) {
clientStatus.put(socketChannel, false);
byte[] endBytes = new byte[]{13,10,46,13,10};
clientMessage.put(socketChannel, endBytes);
socketChannel.register(selector, SelectionKey.OP_WRITE);
}
}else{
clientMessage.put(socketChannel, newBytes);
socketChannel.register(selector, SelectionKey.OP_WRITE);
}

} else {
key.cancel();
socketChannel.close();
clientMessage.remove(socketChannel);
return;
}
} catch (IOException e) {


key.cancel();
try {
socketChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
clientMessage.remove(socketChannel);
return;
}

}

private void write(SelectionKey key) {
boolean falg = false;
SocketChannel socketChannel = (SocketChannel) key.channel();
byte[] newBytes = clientMessage.get(socketChannel);
if (newBytes != null && newBytes.length > 0) {

String command = new String(newBytes);
String response = "";
if(command.toUpperCase().startsWith("CAPA")){
response = "-ERR Unknown command\r\n";
}else if(command.toUpperCase().startsWith("USER")){
response = "+OK \r\n";
}else if(command.toUpperCase().startsWith("PASS")){
response = "+OK \r\n";
}else if(command.toUpperCase().startsWith("UIDL")){
uid +=1;
response = "+OK \r\n"+
"1 UID22417-"+uid+"\r\n"+
".\r\n";
}else if(command.toUpperCase().startsWith("STAT")){
response = "+OK 1 320396 \r\n";
}else if(command.toUpperCase().startsWith("NOOP")){
response = "+OK Yup.\r\n";
}else if(command.toUpperCase().startsWith("LIST")){
response = "+OK POP3 clients that break here, they violate STD53.\r\n"+
"1 320396\r\n"+
".\r\n";
}else if(command.toUpperCase().startsWith("UIDL")){
response = "+OK \r\n";
}else if(command.toUpperCase().startsWith("TOP") || command.toUpperCase().startsWith("RETR 1")){
falg = true;
}
else if(command.toUpperCase().startsWith("QUIT")){
response = "+OK Bye-bye.\r\n";
}else{
response = "+OK Bye-bye.\r\n";
}
byte[] b = response.getBytes();
try {
if(falg){
for(String str : startPop.list){
ByteBuffer writeBuffer = ByteBuffer.allocate(1024*310);
writeBuffer = ByteBuffer.wrap(str.getBytes(),0,str.length());
socketChannel.write(writeBuffer);
}
}else{
ByteBuffer writeBuffer = ByteBuffer.allocate(1024*310);
writeBuffer = ByteBuffer.wrap(b, 0,b.length);
socketChannel.write(writeBuffer);
}

socketChannel.register(selector, SelectionKey.OP_READ);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
clientMessage.put(socketChannel, new byte[0]);
}

}

private void write(SocketChannel socketChannel,String response){
ByteBuffer writeBuffer = ByteBuffer.wrap(response.getBytes());
try {
socketChannel.write(writeBuffer);
socketChannel.register(selector, SelectionKey.OP_READ);
} catch (IOException e) {
e.printStackTrace();
}
clientMessage.put(socketChannel, new byte[0]);
}

private void accept(SelectionKey key) throws IOException {
clientCount++;
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel clientChannel = ssc.accept();
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_WRITE);
ByteBuffer writeBuffer = ByteBuffer.wrap("+OK Email Server Ready.\r\n"
.getBytes());
clientChannel.write(writeBuffer);
clientChannel.register(selector, SelectionKey.OP_READ);
}
}

读书人网 >Java Web开发

热点推荐