- 论坛徽章:
- 0
|
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
*
* @author JRNT
*
*/
public class SendMail extends Thread {
private static String HOSTNAME = "";
private static String USERNAME = ""; // smtp认证用户名
private static String USERPASSWORD = "";// smtp认证密码
private static String FORM = "";
private MimeMessage mimeMsg; // MIME邮件对象
private Session session; // 邮件会话对象
private Properties props; // 系统属性
private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
private Vector fileList;// 发送附件列表
private String subjec;
private String msgInfo;
private String sendTo;
public SendMail() {
props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", HOSTNAME);
props.put("mail.smtp.auth", "true");
}
public void run() {
this.createMimeMessage();
this.setFileList(this.getFileList());
if (this.setSubject(subjec) == false) {
return;
}
if (this.setBody(msgInfo) == false) {
return;
}
if (this.setTo(sendTo) == false) {
return;
}
if (this.setFrom() == false) {
return;
}
if (this.addFileAffix() == false) {
return;
}
if (this.sendout() == false) {
return;
}
}
public boolean createMimeMessage()
{
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
mp = new MimeMultipart();
return true;
}
catch (Exception e) {
System.out.println("创建MimeMessage失败!" + e.getMessage());
return false;
}
}
public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题:" + mailSubject);
try {
mimeMsg.setSubject(mailSubject);
return true;
}
catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
}
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent("" + mailBody, "text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
}
catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
}
private boolean addFileAffix() {
if (this.fileList == null || this.fileList.size() < 1) {
System.out.println("没有要发送的邮件附件......");
return true;
}
try {
while (!this.fileList.isEmpty()) {
BodyPart bp = new MimeBodyPart();
String filename = (String) this.fileList.get(0);
System.out.println("增加邮件附件:" + this.fileList.get(0) + "!");
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
this.fileList.remove(0);
}
return true;
}
catch (Exception e) {
System.err.println("增加邮件附件:" + this.fileList.get(0) + "发生错误!" + e);
return false;
}
}
// 设置发信人
public boolean setFrom() {
try {
mimeMsg.setFrom(new InternetAddress(FORM));
return true;
}
catch (Exception e)
{
return false;
}
}
// 设置收信人
public boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
}
catch (Exception e)
{
return false;
}
}
private boolean sendout()
{
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), USERNAME,
USERPASSWORD);
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功!");
props.clear();
transport.close();
return true;
}
catch (Exception e)
{
System.out.println("邮件发送失败!" + e);
return false;
}
}
public Vector getFileList() {
return fileList;
}
public void setFileList(Vector fileList) {
this.fileList = fileList;
}
public static void main (String args[]) {
this.subjec = "test";
this.msgInfo = "test test test ";
this.fileList = null;
this.sendTo = "fdqzq@sina.com";
return true;
}
}
邮件为什么只能发一次? |
|