62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.Date;
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;
public class SimpleMailSendMessage {
public static void main(String[] args) {
String host = "smtp.163.com";
String to = "yourmail@163.com";
String from = "yourmail@163.com";
String subject = "javaMailTest";
String messageText = "Hello ,java mail";
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.smtp.port", 25);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("yourmail@163.com","yourpassword");
}
});
session.setDebug(sessionDebug);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(javax.mail.Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}