当前位置: 代码迷 >> Java Web开发 >> Email 有关问题?
  详细解决方案

Email 有关问题?

热度:6666   发布时间:2013-02-25 21:18:06.0
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();
  相关解决方案