67,515
社区成员
发帖
与我相关
我的任务
分享
import java.net.InetAddress;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
/**
* 发送邮件测试
*/
public static void main(String[] args) {
try {
// Get system properties
Properties props = System.getProperties();
// 设置smtp邮件服务器
props.put("mail.smtp.host", "192.168.0.61");
props.setProperty("mail.smtp.port", "25");
props.put("mail.smtp.auth", false);
// 取得连接
Session session = Session.getDefaultInstance(props, null);
Transport transport = session.getTransport("smtp");
transport.connect();
// 邮件定义
MimeMessage mail = new MimeMessage(session);
mail.addRecipient(Message.RecipientType.TO, new InternetAddress("To@test.com"));
mail.setSubject("邮件标题……");
mail.setText("邮件正文……");
mail.setFrom(new InternetAddress("from@test.com"));
transport.sendMessage(mail, mail.getAllRecipients());
Transport.send(message);
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}