日期:2014-05-17 浏览次数:20915 次
package test; import java.io.IOException; import java.util.Properties; import javax.activation.DataHandler; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.util.ByteArrayDataSource; public class SeUglyBO { private String host = "smtp.qq.com"; // 定义发送用户帐号密码 private String from = "593456521@qq.com"; private String user = "593456521@qq.com"; private String password = "13206511521";/** * 大部分 * @param name * @param to * @param title * @param content * @param lie * @return * @throws IOException */ public boolean send(String to, String title, String content) throws IOException { //System.out.println(to); boolean ioke = true; try { System.out.println(to+"======"+title+"======"+content); // 架设smtp Properties pro = new Properties(); pro.put("smtp.qq.com", host); pro.put("smtp.qq.com", "true");// ******8 Session session = Session.getDefaultInstance(pro); session.setDebug(true); // 是否在控制台打出语句 MimeMessage message = new MimeMessage(session); // 定义重哪个邮箱到哪个邮箱的地址和内容 message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(title); collect(content, message); message.saveChanges(); Transport tran = session.getTransport("smtp"); // 通过SMTP效验用户,密码等进行连接 tran.connect(host, user, password); tran.sendMessage(message, message.getAllRecipients()); tran.close(); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); ioke = false; } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); ioke = false; } return ioke; }
package test; import java.io.IOException; import action.SendEmailAction; public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SeUglyBO se=new SeUglyBO(); try { se.send("123456@qq.com","撒的发生的发生","撒旦法。"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }