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("[email protected]");
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;
} }

解决方案 »

  1.   

    jmail 发送的邮件 怎么换行? 我的源代码如上贴,发送邮件已经没问题,但是收到的邮件内容和我在页面输入后传过来的不一样,输入使用了回车键换了行,可是收到的邮件没换行。为什么???请高手指教!!!  
    小弟分少,只能这么些了!谢谢了先!
      

  2.   

    mdp.setContent(tcontent,   "text/html;charset=gb2312 "); 没看到你的内容在哪里
    定义为html的话换行使用我是第一行<br/>我是第二行
    http://blog.csdn.net/sunyujia/archive/2008/06/10/2528696.aspx
      

  3.   

    在发出之前用replaceAll()方法把\n换成<br>
      

  4.   

    我的内容是在jsp页面上获得的。是一个text!在text里使用回车键换了行,到邮箱的时候却不是那样了!我想收到的邮件和在text里输入的一样!  请教!