/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sendmail;
import java.util.Date;
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;
/**
*
* @author hgh
*/
public class sdm {
public static void main(String[] args) {
System.out.println("启动成功");
try {
int port = 25;
String server = "smtp.163.com";//邮件服务器mail.cpip.net.cn
// String from = "[email protected]";//发送者,显示的发件人名字
String from = "[email protected]";//发送者,显示的发件人名字
// String user = "[email protected]";//发送者邮箱地址
String user = "[email protected]";//发送者邮箱地址
String password = "NMFsqa6HnV1t91Iu";//密码
//建立会话
Properties props = new Properties();
props.put("mail.smtp.host", server);
props.put("mail.smtp.port", String.valueOf(port));
props.put("mail.smtp.auth", "true");
Transport transport = null;
Session session = Session.getInstance(props);
//建立信息
Message msg = new MimeMessage(session);
//发件人
msg.setFrom(new InternetAddress(from));
String toList = "[email protected]";
InternetAddress[] iaToList = new InternetAddress().parse(toList);
//收件人
msg.setRecipients(Message.RecipientType.TO, iaToList);
// 发送日期
msg.setSentDate(new Date());
// 主题
msg.setSubject("测试标题");
msg.setContent("测试内容", "text/html;charset=utf8"); //内容
// 邮件服务器进行验证
transport = session.getTransport("smtp");
transport.connect(server, user, password);
// bluebit_cn是用户名,xiaohao是密码
transport.sendMessage(msg, msg.getAllRecipients()); // 发送
System.out.println("邮件发送成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}