发送邮件 java实现


import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailDemo {
 public static void main(String args[]) throws Exception {
  Properties props = new Properties();
  props.setProperty("mail.smtp.auth", "true");
  props.setProperty("mail.transport.protocol", "smtp");
  props.setProperty("mail.host", "smtp.sina.com");
  props.setProperty("mail.port", "25");
 
  Session session = Session.getInstance(props, new Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("test", "123456");
   }
  });
  session.setDebug(true);
  Message msg = new MimeMessage(session);
  msg.setFrom(new InternetAddress("test@sina.com"));
  msg.setSubject("你好吗?");
  msg.setSentDate(new Date());
  msg.setContent("<a href='http://sina.com'><span style='color:red'>我很好,你呢?</span></a>", "text/html;charset=gbk");
  msg.setRecipients(RecipientType.TO, InternetAddress.parse("test@sina.com,test@sohu.com"));
  Transport.send(msg);
 }
}

相关内容