50,523
社区成员
发帖
与我相关
我的任务
分享
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaMailFor163 {
public static void main(String[] args) throws AddressException,
MessagingException {
String to = "***@163.com";
String from = "***@163.com";
Properties props = new Properties();
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", "smtp.163.com"); // 这里填写你发信者的SMTP主机,如:smtp.sohu.com
// props.put("mail.smtp.user", usr);
// props.put("mail.smtp.password", pwd);
props.put("mail.smtp.auth", "true");
sendMailSession = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("邮箱地址","邮箱密码");
}// 这里填写你发信者的邮箱地址和密码
});// 如果你的邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的,我的邮箱要验证,所以得这么写。
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
to));
newMessage.setSubject("Stamp");
newMessage.setSentDate(new Date());
newMessage.setText("Stamp");
transport = sendMailSession.getTransport("smtp");
Transport.send(newMessage);
}
}