标签:rgs efault out address throws 收件人 hello oid getpass
package com.rupeng.javaMail;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class Test {
public static void main(String[] args) throws AddressException, MessagingException {
Properties props=new Properties();
props.setProperty("mail.smtp.host","smtp.sina.com");
props.setProperty("mail.smtp.auth", "true");
//1.创建Session对象
Session session=Session.getDefaultInstance(props,new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("rupengfrom","rupengfrom");
}
});
//创建和编写邮件
Message message=new MimeMessage(session);
//设置发件人
message.setFrom(new InternetAddress("rupengfrom@sina.com"));
//设置收件人
message.setRecipient(RecipientType.TO,new InternetAddress("rupengto@sina.com"));
//设置标题
message.setSubject("测试邮件");
//设置邮件的内容
message.setText("hello JavaMail1564856456456!");
//3.发送邮件
Transport.send(message);
System.out.println("111");
}
}
标签:rgs efault out address throws 收件人 hello oid getpass
原文地址:https://www.cnblogs.com/traveller-hzq/p/9087217.html