//最近项目赶工,但还是抽时间写下内容,让自己对这部分的内容更加的清晰,
//内容经供参考,以下代码是运行成功后的结果.切莫copy,拒绝只做理论帝,努力成为手工王,高手请留言,让大家一起学习进步,壮大我大中华程序员...
package com.actionsoft.Util;
import java.io.*;
import java.util.*;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
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 MessageS extends Authenticator {
public void doPost(String recipients,String subject,String Text){
try{
Properties props = new Properties();//设置连接参数
props.setProperty("mail.host", "mail.163.com");//邮箱地址
props.setProperty("mail.transport.protocol", "smtp");//设置验证类型smtp
props.setProperty("mail.smtp.auth", "true");//验证开启
//获取session
System.out.println("执行了props");
Session session = Session.getDefaultInstance(props);
System.out.println("执行了session");
MimeMessage rss = Inserlv(session,recipients, subject, Text);
System.out.println("执行了rss");
Transport transport = session.getTransport();//连接邮件smtp服务器,参数分别为服务器地址,用户名和密码
System.out.println("执行了Transport");
transport.connect("smtp.163.com","你自己的163账户","密码");//发送消息 //前面的是邮箱服务器地址
System.out.println("transport");
transport.sendMessage(rss, rss.getAllRecipients());//关闭连接
transport.close();
System.out.println("发送成功!");
}catch(Exception e){
e.printStackTrace();
System.out.println("发送失败!");
}
}
public static MimeMessage Inserlv(Session session,String recipients,String subject,String context) throws AddressException, MessagingException{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("你自己的163账户"));
System.out.println(recipients);
message.setRecipient(RecipientType.TO, new InternetAddress(recipients));
message.setSubject(subject);message.setSentDate(new Date());
message.setContent(context, "text/html;charset=utf-8");
return (MimeMessage) message;
}
public static MessageS execute(){
return new MessageS();
}
public static void main(String[] args){
MessageS.execute().doPost("收件人的邮箱", "标题", "内容可以写html代码组成具体的美观看你个人能力了");//这个是调用方法.这个类可以导出成jar包,调用方法直接用MessageS.execute().doPost("","","")这个就可以了;
System.out.println("12");
}
}