- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.MimeMessage;
- public class MailTest2 {
- static Authenticator auth = new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("tt@qq.com", "fuck");
- }
- };
- public static void main(String[] args) {
- Properties props = new Properties();
- props.put("mail.smtp.host", "smtp.qq.com");
- props.put("mail.smtp.auth", "true");
- props.put("mail.from", "tt@qq.com");
- Session session = Session.getInstance(props, auth);
- try {
- MimeMessage msg = new MimeMessage(session);
- msg.setFrom();
- msg.setRecipients(Message.RecipientType.TO, "tt@163.com");
- msg.setSubject("JavaMail hello world example");
- msg.setSentDate(new Date());
- msg.setText("Hello, world!\\n");
- Transport.send(msg);
- } catch (MessagingException mex) {
- System.out.println("send failed, exception: " + mex);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2907201513252.html
来源: http://www.codesnippet.cn/detail/2907201513252.html