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;
} }
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;
} }
解决方案 »
- struts2添加过滤器后乱码
- 拥有enctype="multipart/form-data"的表单无法获取表单中除了type=file以外的其他参数不能被传递的解决方法
- intRange的验证问题
- Struts资源文件问题,放了半年了,突然拿起来,连配置都不知道了,帮帮我!!!
- jsp服务问题
- 能提供一下新闻点击数统计实现想法吗?根据会话统计
- 一个小问题!但很急!请帮帮忙!
- extjs中的form.element.length为空或不是对象
- 存取文件的路径问题
- 不知sql语句能否实现???
- 【求教】使用java的timer做定时删除,为什么不能一次性删除完所有数据
- jsp重命名上传的文件提示文件无法保存(File can't be saved (1120).)
小弟分少,只能这么些了!谢谢了先!
定义为html的话换行使用我是第一行<br/>我是第二行
http://blog.csdn.net/sunyujia/archive/2008/06/10/2528696.aspx