//发送带一个附件的邮件的代码
public static String sendmail(String from,String to,String reply_to,String host,String subject,String body,String filename)
{
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.setProperty("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props, new myAuth());
try {
// create a message
if(from.trim().length()<1)
{if(reply_to.trim().length()<1) {from=to;reply_to=to;}
else from=reply_to;
}
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address1 = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address1);
InternetAddress[] address2 = {new InternetAddress(reply_to)};
msg.setReplyTo(address2);
msg.setSubject(subject); // create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body); // create the second message part
MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(new String(fds.getName().getBytes("GBK"),"iso8859-1")); // create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); // add the Multipart to the message
msg.setContent(mp); // set the Date: header
msg.setSentDate(new java.util.Date());
// send the message
Transport.send(msg);
}
catch (Exception e) {e.printStackTrace();return "发送邮件失败:"+e.toString()+"!";}
return "邮件发送成功!";
}
//myAuth.java
public class myAuth extends javax.mail.Authenticator
{
public myAuth () {}
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication(用户名,密码);
}
}
public static String sendmail(String from,String to,String reply_to,String host,String subject,String body,String filename)
{
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.setProperty("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props, new myAuth());
try {
// create a message
if(from.trim().length()<1)
{if(reply_to.trim().length()<1) {from=to;reply_to=to;}
else from=reply_to;
}
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address1 = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address1);
InternetAddress[] address2 = {new InternetAddress(reply_to)};
msg.setReplyTo(address2);
msg.setSubject(subject); // create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body); // create the second message part
MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(new String(fds.getName().getBytes("GBK"),"iso8859-1")); // create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); // add the Multipart to the message
msg.setContent(mp); // set the Date: header
msg.setSentDate(new java.util.Date());
// send the message
Transport.send(msg);
}
catch (Exception e) {e.printStackTrace();return "发送邮件失败:"+e.toString()+"!";}
return "邮件发送成功!";
}
//myAuth.java
public class myAuth extends javax.mail.Authenticator
{
public myAuth () {}
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication(用户名,密码);
}
}
解决方案 »
- 公司要开发一个jsp网站系统,我一点不懂,应该怎么学起,请高手指点一下
- 请教一下tomcat6.0的优化配置
- hibernateTemplate多表外键存储
- jsp中javabean访问数据库,允许jsp是出错了,大虾帮忙啊!
- 抽象类Graphics2D没有子类 ,它是怎么实例化的?
- 我有一段下面的代码,我在javabean中如何调用这段代码,写入sql语句,使用system.out.print打印出来呢。
- 大家帮我看一下这个程序!!
- zk做个遮罩层,显示"处理中..."
- 请问怎么实现取出数据后只要一点页面上面的按什么什么排序就对表格中已取出的数据重新排序呢?(一定给分的!)
- 如何这样刷新
- 关于jsp读xml循环的问题,请帮忙,谢谢!!!
- 急,在线等待,解决即给分!!!
transport.sendMessage(newMessage, newMessage.getAllRecipients());试试,不过应该不是这个问题~~~