Javamail使用过程中常见问题解决方案-创新互联
今天在研究javamail发信的过程中,出现了一些小问题,现总结如下,以免后来者走些不必要的弯路,先把完整的能够正常运行的代码示例粘贴如下:

发邮件源代码:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailExample {
public static void main (String args[]) throws Exception {
String host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "你自己的电子信箱"; //发邮件的出发地(发件人的信箱)
String to = "收件人信箱"; //发邮件的目的地(收件人信箱)
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
props.put("mail.smtp.auth", "true"); //这样才能通过验证
MyAuthenticator myauth = new MyAuthenticator("你自己的电子信箱", "你自己的信箱密码");
Session session = Session.getDefaultInstance(props, myauth);
//session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set the subject
message.setSubject("测试程序!");
// Set the content
message.setText("这是用java写的发送电子邮件的测试程序!");
message.saveChanges();
Transport.send(message);
}
} 网页名称:Javamail使用过程中常见问题解决方案-创新互联
链接URL:http://www.jxjierui.cn/article/djhhsh.html


咨询
建站咨询
