javamail 收发 文本邮件

  1. /**
  2.  * 
  3.  */
  4. package com.tsts.oa.mail;
  5. import java.util.Properties;
  6. import javax.mail.Authenticator;
  7. import javax.mail.Message;
  8. import javax.mail.PasswordAuthentication;
  9. import javax.mail.Session;
  10. import javax.mail.Transport;
  11. import javax.mail.internet.InternetAddress;
  12. import javax.mail.internet.MimeMessage;
  13. /**
  14.  * @author sunyanan
  15.  * 
  16.  */
  17. public class SendHelper {
  18.  public static void send() throws Exception {
  19.   Properties prop = new Properties();
  20.   prop.put("mail.smtp.host""smtp.126.com");
  21.   prop.put("mail.smtp.auth""true");
  22.   
  23.   Session session =  Session.getDefaultInstance(prop, (new SendHelper()).new AT());
  24.   
  25.   session.setDebug(true);
  26.   MimeMessage message = new MimeMessage(session);
  27.   
  28.   message.setFrom(new InternetAddress("sunyanan8@126.com"));
  29.   message.setRecipient(Message.RecipientType.TO, new InternetAddress("sunyanan8@126.com"));
  30.   message.setSubject("主题");
  31.   message.setText("内容");
  32.   Transport.send(message);
  33.   
  34.  }
  35.  /**
  36.   * @param args
  37.   */
  38.  public static void main(String[] args) throws Exception {
  39.   send();
  40.  }
  41.  class AT extends Authenticator {
  42.   @Override
  43.   protected PasswordAuthentication getPasswordAuthentication() {
  44.    // TODO Auto-generated method stub
  45.    return new PasswordAuthentication("sunyanan8@126.com""xxxxxxxx");
  46.   }
  47.  }
  48. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值