标签:style blog http io color ar os 使用 java
java发邮件的方法应该不止一种
我这里只说我会的一种
首先添加两个jar包mail.jar 和 activation.jar
然后自己写一个mail的类,类中包括发送等方法
1 public class Mail { 2 private String subject; 3 private String from; 4 private String[] recipients; 5 private String message; 6 7 private static final String SMTP_HOST_NAME = "";//你的smtp服务器地址 8 private static final String SMTP_AUTH_USER = "";//smtp用户名 9 private static final String SMTP_AUTH_PWD = ""; //密码; 10 11 public void send() throws MessagingException{ 12 boolean debug = false; 13 14 Properties props = new Properties(); 15 props.put("mail.smtp.host", SMTP_HOST_NAME); 16 props.put("mail.smtp.auth", "true"); 17 18 Session session = Session.getDefaultInstance(props, new SMTPAuthenticator()); 19 session.setDebug(debug); 20 21 // create a message 22 Message msg = new MimeMessage(session); 23 msg.setFrom(new InternetAddress(from)); 24 25 InternetAddress[] addressTo = new InternetAddress[recipients.length]; 26 for (int i = 0; i < recipients.length; i++) 27 { 28 addressTo[i] = new InternetAddress(recipients[i]); 29 } 30 msg.setRecipients(Message.RecipientType.TO, addressTo); 31 32 msg.setHeader("X-Priority", "3"); //1:紧急 3:普通 5:缓慢 33 msg.setSubject(subject); 34 msg.setContent(message, "text/plain; charset=UTF-8"); 35 Transport.send(msg); 36 } 37 38 //如需认证发件,可打开下面注释 39 private class SMTPAuthenticator extends javax.mail.Authenticator 40 { 41 42 public PasswordAuthentication getPasswordAuthentication() 43 { 44 String username = SMTP_AUTH_USER; 45 String password = SMTP_AUTH_PWD; 46 return new PasswordAuthentication(username, password); 47 } 48 } 49 50 public String getSubject() { 51 return subject; 52 } 53 54 55 public void setSubject(String subject) { 56 this.subject = subject; 57 } 58 59 60 public String getFrom() { 61 return from; 62 } 63 64 65 public void setFrom(String from) { 66 this.from = from; 67 } 68 69 70 public String[] getRecipients() { 71 return recipients; 72 } 73 74 75 public void setRecipients(String[] recipients) { 76 this.recipients = recipients; 77 } 78 79 80 public String getMessage() { 81 return message; 82 } 83 84 85 public void setMessage(String message) { 86 this.message = message; 87 } 88 89 90 91 }
在别的类中这样引用就可以了:
private static void sendRenew(String toMail,String message){ Mail mail = new Mail(); mail.setFrom("License Manager <huang@163.com>");//发送方 mail.setRecipients(new String[]{toMail});//接收方,可以多个 mail.setSubject("Renew Now!!"); mail.setMessage(message); try { mail.send();//发送 } catch (Exception e) { System.out.println("error report failure.\n"+e); } }
这样就行了,不过如果你使用的是MyEclipse有时候会出现这样的异常
java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
解决方法:
进入MyEclipse的安装目录下
D:\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\EE_5
(蓝色部分不同版本略有不同)
可以看到javaee.jar包,
使用压缩软件打开把这个包文件,然后进到javax文件夹中,删除mail目录和activation文件夹
(也就是说MyEclipse中也自带了这样的功能包,只是方法与JDK的javamail包不一致而导致报异常)
删掉之后,在运行代码,就不会出现错误了
参考:http://blog.sina.com.cn/s/blog_4550f3ca01019qpt.html,谢谢这位大哥
标签:style blog http io color ar os 使用 java
原文地址:http://www.cnblogs.com/tommy-huang/p/4076436.html