package doudizhu.scy.mail;
import java.util.Date;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EMail {
public void sendSimpleEmail(String subject,String content)
{
try
{
//加载资源文件
ResourceBundle rb=ResourceBundle.getBundle("douniu.scy.properties.mail");
//获取收信人地址
String fromMail=rb.getString("fromMail");
//获取收信人地址
String toMail=rb.getString("toMail");
//获取用户名
String username=rb.getString("username");
//获取用户密码
String password=rb.getString("password");
//获取发邮件服务器
String mailServer=rb.getString("mailServer");
//创建属性对象
Properties pros=new Properties();
//设置邮件传输协议为:smtp
pros.put("mial.transpost.protocol", "smtp");
//设置邮件服务器地址
pros.put("mail.smtp.host", mailServer);
//设置邮件验证为真
pros.put("mail.smtp.auth", "true");
//设置邮件服务器端口
pros.put("mail.smtp.port", "25");
//调用验证类进行验证,需要参数发件人用户名和密码
CheckSendEmail auth=new CheckSendEmail(username,password);
//创建session对象
Session sendMailSession=Session.getDefaultInstance(pros, auth);
//设置输出调试信息
sendMailSession.setDebug(true);
//创建信息对象
Message newMessage=new MimeMessage(sendMailSession);
//设置发信人地址
newMessage.setFrom(new InternetAddress(fromMail));
//设置收信人地址
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
//设置信件文本格式
newMessage.setContent("SendEmail", "text/html");
//设置信件主题
newMessage.setSubject(subject);
//设置信件发送日期
newMessage.setSentDate(new Date());
//设置信件正文
newMessage.setText(content);
//创建对象
// Transport transport=sendMailSession.getTransport("smtp");
Transport.send(newMessage);
System.out.println("发送成功");
} catch (Exception e)
{
System.out.println("发送失败");
e.printStackTrace();
}
}
public static void main(String[] args) {
EMail e=new EMail();
e.sendSimpleEmail("bingdu", "su jin ge zhu tou");
}
}