- 论坛徽章:
- 0
|
1.初始化Session环境
Properties prop = new Properties();
//邮件发送协议,邮件服务器,端口.
prop.put("mail.transport.protocol","smtp");
prop.put("mail.smtp.host","yourmailserverhost");
prop.put("mail.smtp.port",25);
Session session = Session.getDefaultInstance(prop,null);
2.构造邮件
Message msg = new MimeMessage(session);
String from =
start@sohu.com
;
String to =
end@sina.com
;
msg.setFrom(new InternetAddress(from));
msg.setTo(Message.RecipientType.TO,InternetAddress.parse(to));
//发送日期.
msg.setSentDate(new Date());
//主题64编码,防止乱码.
/**有些邮件服务器使用的字符集是7bit传送的,这时候对二进制和中文会出现乱码,就要进行编码处理.*/
BASE64Encoder encoder = new BASE64Encoder();
String sub = "思潮起伏";
msg.setSubject("=?gbk?B?"+encoder.encode(sub.getBytes())+"?=");
//内容,纯文本.
msg.setContent("this","text/plain;charset=gbk");
msg.setText("我的确中国共产党");
//把msg保存成为邮件.
msg.saveChanges();
3.连接服务器
transport.connect("localhost","userName","password");
4.送邮件
transport.send(msg);
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45398/showart_358141.html |
|