⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mail.java复件

📁 一个专门用来快速开发网站的框架
💻 JAVA复件
字号:
package mail;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.*;

import auth.PopupAuthenticator;


public class Mail {
        private InternetAddress addressFrom=new InternetAddress();
        private DataHandler dataHandler;
        private String subject;
        private String content;
        private Properties props=System.getProperties();
        private Transport connect;
        private Session session;
        private Message message;
        private boolean isDebug=false;
        private Multipart multipart;
        public Mail() {
        }

        public Mail(String subject,String content) throws MessagingException {
                setSubject(subject);
                setContent(content);
        }

        public Mail(String addressTo, String subject,String content) throws MessagingException {
                addAddressTo(addressTo);
                setSubject(subject);
                setContent(content);
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Mail mail = null;
                try {
                        mail = new Mail();
                        mail.setSmtp("smtp.qq.com");
                        mail.setSmtpAuth(true);
                        mail.isDebug(true);
                        mail.connect();
                       // mail.addAddressTo("flybypower@qq.com");
                        mail.addAddressTo("flybypower@qq.com");
                        mail.setAddressFrom("7650018@qq.com");
                        mail.write("Mail 测试", " 测试一下");
                       //mail.addAttachmentPath("C://db.rar");
                        mail.addAttachmentPath("http://www.lifetv.com.cn/web/cartoon/music/20077209380498.mp3");
                        mail.send();
                        mail.disconnect();
                } catch (MessagingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        void write(String subject,String content) throws MessagingException{
                message.setSubject(subject);
                multipart = new MimeMultipart();
                MimeBodyPart bodyPart = new MimeBodyPart();
                bodyPart.setText(content);
                multipart.addBodyPart(bodyPart);
        }
        void send() throws MessagingException{
                message.setFrom(addressFrom);
                message.setContent(multipart);
                connect.sendMessage(message, message.getAllRecipients());
        }
/**
 * @return the addressTo
 * @throws MessagingException
 */
public String getAddressesTo() throws MessagingException {
        String s = "";
        for(int i=0;i<message.getAllRecipients().length;i++){
                s=s+((InternetAddress)message.getAllRecipients()[i]).getAddress()+"\n";
        }
        return s;
}

/**
 * @param addressTo the addressTo to set
 * @throws MessagingException
 */
public void addAddressTo(String addressTo) throws MessagingException {
        this.message.addRecipient(Message.RecipientType.TO, new InternetAddress(addressTo));
}

/**
 * 添加抄送地址
 * @param addressTo
 * @throws MessagingException
 */
public void addCopyAddressTo(String addressTo) throws MessagingException {
        this.message.addRecipient(Message.RecipientType.CC, new InternetAddress(addressTo));
}

/**
 * 添加密送地址
 * @param addressTo
 * @throws MessagingException
 */
public void addBlindCopyAddressTo(String addressTo) throws MessagingException {
        this.message.addRecipient(Message.RecipientType.BCC, new InternetAddress(addressTo));
}

/**
 * @return the subject
 */
public String getSubject() {
        return subject;
}

/**
 * @param subject the subject to set
 */
public void setSubject(String subject) {
        this.subject = subject;
}


public void addAttachmentPath(String path) throws MessagingException {
        URL urL = null;
        try {
                urL = new URL(path);
                dataHandler=new DataHandler(urL);
                MimeBodyPart bodyPart = new MimeBodyPart();
                bodyPart.setDataHandler(dataHandler);
                multipart.addBodyPart(bodyPart);
        } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
}
/**
 * @return the content
 */
public String getContent() {
        return content;
}
/**
 * @param content the content to set
 */
public void setContent(String content) {
        this.content = content;
}
/**
 * @return the smtp
 */
public String getSmtp() {
        return props.getProperty("mail.smtp.host");
}
/**
 * @param smtp the smtp to set
 */
public void setSmtp(String smtp) {
        props.put("mail.smtp.host", smtp);
}
/**
 * @return the smtpAuth
 */
public boolean isSmtpAuth() {
        if (props.getProperty("mail.smtp.auth").equalsIgnoreCase("true")) {
                return true;
        }else	return false;
}
/**
 * @param smtpAuth the smtpAuth to set
 */
public void setSmtpAuth(boolean smtpAuth) {
        if(smtpAuth){
                props.put("mail.smtp.auth", "true");
        }
}
/**
 * @return the addressFrom
 */
public String getAddressFrom() {
        return addressFrom.getAddress();
}
/**
 * @param addressFrom the addressFrom to set
 */
public void setAddressFrom(String addressFrom) {
        this.addressFrom.setAddress(addressFrom);
}

/**
 * @param connect the connect to set
 * @throws MessagingException
 */
public void connect() throws MessagingException {
        Authenticator auth = new PopupAuthenticator();
        session= Session.getDefaultInstance(props,auth);
        session.setDebug(isDebug);
        message=new MimeMessage(session);
        try {
                this.connect = session.getTransport("smtp");
                connect.connect();
        } catch (NoSuchProviderException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
}
public String disconnect(){
        try {
                connect.close();
                return "disconnected";
        } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return e.toString();
        }
}
public String isConnect(){
        if(connect.isConnected()){
                return "connected";
        }else{
                return "not connected";
        }
}
public boolean isDebug(boolean debug){
        isDebug=debug;
        return this.isDebug;
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -