用javamail 发邮件

jar包 主要是用到两个。http://dl2.csdn.net/down4/20070803/03175237322.jar

http://dl2.csdn.net/down4/20070803/03175338758.jar

 import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

 

/**
 * 使用Gmail发送邮件
 *
 * @author Rain Chen
 */
public class GmailSender {

 private static final Log logger = LogFactory.getLog(GmailSender.class);

 private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

 private static final String USERNAME = "feifniao";// 设定邮箱的用户名

 private static final String PASSWORD = "××××××";// 设定邮箱的密码×××就是你的信箱的密码

 private static final String TO_EMAIL = "feifniao@163.com";// 设定收件人的信箱

 private static final String SUBJECT = "你有新订单111!";// 设定邮件标题

 private static final String FROM = "feifniao@gmail.com";// 设定发件件的人

 private static final String SMTP = "smtp.gmail.com";

 public static void sender(String subject, String content) throws Exception {

  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  // Get a Properties object
  Properties props = System.getProperties();
  props.setProperty("mail.smtp.host", SMTP);
  props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
  props.setProperty("mail.smtp.socketFactory.fallback", "false");
  props.setProperty("mail.smtp.port", "465");
  props.setProperty("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.quitwait", "false");

  Session session = Session.getDefaultInstance(props,
    new Authenticator() {
     protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(USERNAME, PASSWORD);
     }
    });

  Message msg = new MimeMessage(session);
  InternetAddress[] address = null;
  // 设定发邮件的人
  msg.setFrom(new InternetAddress(FROM));

  // 设定收信人的信箱
  address = InternetAddress.parse(TO_EMAIL, false);
  msg.setRecipients(Message.RecipientType.TO, address);

  // 设定信中的主题
  msg.setSubject(subject);

  // 设定送信的时间
  msg.setSentDate(new Date());

  Multipart mp = new MimeMultipart();
  MimeBodyPart mbp = new MimeBodyPart();

  // 设定邮件内容的类型为 text/plain 或 text/html
  mbp.setContent(content, "text/html;charset=GB2312");
  mp.addBodyPart(mbp);
  msg.setContent(mp);

  Transport transport = session.getTransport("smtp");
  transport.connect(SMTP, USERNAME, PASSWORD);
  transport.sendMessage(msg, msg.getAllRecipients());
  transport.close();
 }

 public static void main(String[] args) throws Exception {   
  
        sender(SUBJECT,     " 恭喜你!收到邮件了!www.diker.cn ");   
        System.out.println("Message sent.");   
    }}
 

 
上一篇:netlogo扩散模型


下一篇:CentOS7--使用yum安装和管理软件