- /**
- *
- */
- package com.tsts.oa.mail;
- 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.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- /**
- * @author sunyanan
- *
- */
- public class SendHelper {
- public static void send() throws Exception {
- Properties prop = new Properties();
- prop.put("mail.smtp.host", "smtp.126.com");
- prop.put("mail.smtp.auth", "true");
- Session session = Session.getDefaultInstance(prop, (new SendHelper()).new AT());
- session.setDebug(true);
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress("sunyanan8@126.com"));
- message.setRecipient(Message.RecipientType.TO, new InternetAddress("sunyanan8@126.com"));
- message.setSubject("主题");
- message.setText("内容");
- Transport.send(message);
- }
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- send();
- }
- class AT extends Authenticator {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- // TODO Auto-generated method stub
- return new PasswordAuthentication("sunyanan8@126.com", "xxxxxxxx");
- }
- }
- }
javamail 收发 文本邮件
最新推荐文章于 2023-07-08 23:13:21 发布