Java Mail example.
package com.statestr.ssc;import java.util.Date;import java.util.Properties;import javax.mail.AuthenticationFailedException;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;/** * * @author AjunBoys * */public class Email {private String host;private String username;private String password;private String from;private String to;private String subject;private String content;public String getHost() {return host;}public void setHost(String host) {this.host = host;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getFrom() {return from;}public void setFrom(String from) {this.from = from;}public String getTo() {return to;}public void setTo(String to) {this.to = to;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public Email(){this.host = "smtp.126.com";this.username = "wlq0215";this.password = "******";this.from = "wlq0215@126.com";}public static void main(String[] args) { Email email = new Email(); email.setContent("hello"); email.sendMail("wlq0215@126.com", "****");}//发送邮件public boolean sendMail(String from,String to){try{Properties props = new Properties();//存储连接参数props.put("mail.smtp.host", host); //smtp主机props.put("mail.smtp.auth", "true"); //需要身份验证Session session = Session.getInstance(props);//获取一个邮件sessionsession.setDebug(true);MimeMessage message = new MimeMessage(session); //邮件会话新建一个邮件信息//检查邮件地址是否合法if(from == null || from ==""){throw new Exception("Error Email Address in From.");}if(to == null || to ==""){throw new Exception("Error Email Address in To.");}//设置源地址//message.setFrom(new InternetAddress(from)); //等价于下面两条代码InternetAddress addressFrom = new InternetAddress(from);message.setFrom(addressFrom);//设置目的地址InternetAddress addressTo = new InternetAddress(to);message.setRecipient(Message.RecipientType.TO, addressTo);message.setSubject(subject);//message.setText(content); //邮件正文Multipart mp = new MimeMultipart();//邮件内容MimeBodyPart mbpContent = new MimeBodyPart();mbpContent.setContent(content,"text/html"); //邮件格式//向MimeMessage添加代表正文(Multipart) mp.addBodyPart(mbpContent); message.setContent(mp); message.setSentDate(new Date()); //设置发送的时间 Transport transport = session.getTransport("smtp"); //设置邮件传输 transport.connect((String)props.get("mail.smtp.host"), username, password); //连接主机 transport.sendMessage(message, message.getRecipients(MimeMessage.RecipientType.TO)); //发送邮件 System.out.println("Send Success !"); transport.close(); //关闭传输 return true; }catch(AuthenticationFailedException e){e.printStackTrace();return false;} catch(MessagingException e){e.printStackTrace();return false;}catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}}}