免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1326 | 回复: 0
打印 上一主题 下一主题

邮件为什么只能发一次 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-23 21:33 |只看该作者 |倒序浏览
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;

        }

}
邮件为什么只能发一次?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP