public boolean sendMail(String ttitle,String tcontent,String[] ttos,String[] attachFile){
Properties props = new Properties();
props.put( "mail.smtp.host ", "xx.xx.cn ");
props.put( "mail.smtp.auth ", "true ");
Session s = Session.getInstance(props);
s.setDebug(true);
MimeMessage message = new MimeMessage(s);
try {
InternetAddress from = new InternetAddress( "amyfe@163.com.cn ");
message.setFrom(from);
InternetAddress[] address = new InternetAddress[ttos.length];
for(int i=0;i <address.length;i++){
address[i] = new InternetAddress(ttos[i]);
}
message.setRecipients(Message.RecipientType.TO,address);
message.setSubject(ttitle);
message.setSentDate(new Date());
Multipart mm = new MimeMultipart();
// 给消息对象设置内容
BodyPart mdp = new MimeBodyPart();
System.out.println( "tcontent: "+tcontent);
mdp.setContent(tcontent, "text/html;charset=gb2312 ");
mm.addBodyPart(mdp);
//附件相关
//String[] attachFile = { "C:\\MSINET.OCX ", "C:\\RICHTX32.OCX "};
if(attachFile!=null&&attachFile.length> 0){
FileDataSource fds = null;
BodyPart attachbodypart = null;
for(int i=0;i <attachFile.length&&!attachFile[i].equals( " ");i++){
attachbodypart = new MimeBodyPart();
fds =new FileDataSource(attachFile[i]);
attachbodypart.setDataHandler(new DataHandler(fds)); //设置数据源
attachbodypart.setFileName(MimeUtility.encodeWord(fds.getName(), "UTF-8 ", "Q "));
//new String(fds.getName().getBytes( "gb2312 "), "iso8859 ")
mm.addBodyPart(attachbodypart);
}
message.setContent(mm);
message.saveChanges();
}
Transport transport = s.getTransport( "smtp ");
transport.connect( "xx.xx.cn ", "amyfe ", "123456 ");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println( "发送成功! ");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
------解决方案--------------------------------------------------------
在发出之前用replaceAll()方法把\n换成<br>