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

📄 mail.java

📁 一个java实现的有界面的email发送程序。可以从网络上抓取email。也可以从文件中读取email
💻 JAVA
字号:
package com.code10.core;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.AuthenticationFailedException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;

import com.code10.basecomponent.UserInfo;

public class Mail {
	private String displayName;   
    private String to;   
    private String cc;  
    private String bcc;                  //暗送 
    private UserInfo userInfo;
    private String subject;   
    private String content;   
    private String filename="";   
    private Vector file = new Vector(); //用于保存发送附件的文件名的集合   
	public String getBcc() {
		return bcc;
	}
	public void setBcc(String bcc) {
		this.bcc = bcc;
	}
	public String getCc() {
		return cc;
	}
	public void setCc(String cc) {
		this.cc = cc;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getDisplayName() {
		return displayName;
	}
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
	public Vector getFile() {
		return file;
	}
	public void setFile(Vector file) {
		this.file = file;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
    
	 public Mail(UserInfo userInfo, String to,String cc,String bcc,String subject,String content){   
	        initMail(userInfo,  to, cc, bcc, subject, content);
	 } 
	 
    /**  
     * 初始化SMTP服务器地址、发送者E-mail地址、用户名、密码、接收者、主题、内容  
     */   
    public void initMail(UserInfo userinfo,String to,String cc,String bcc,String subject,String content){   
        this.userInfo=userinfo;   
        this.displayName=userinfo.getStrUserName();   
        if(to!=null)
        this.to=to.replace(";", ",");  
        if(cc!=null)
        this.cc=cc.replace(";", ",");
        if(bcc!=null)
        this.bcc=bcc.replace(";", ",");
        this.subject=subject;   
        this.content=content;   
    }   
    /**  
     * 发送邮件  
     */   
	public HashMap send(){   
        HashMap map=new HashMap();   
        map.put("state", "success");
		String message="邮件发送成功!";   
        Session session=null;   
        Properties props = System.getProperties();   
        props.put("mail.smtp.host", userInfo.getStrSmtpServer());   
      
        if(userInfo.getStrIsmtpAuth()){ //服务器需要身份认证   
            props.put("mail.smtp.auth","true");      
    		SmtpAuth smtpAuth=new SmtpAuth(userInfo.getStrUserName(),userInfo.getStrPassword());   
            session=Session.getInstance(props, smtpAuth);    
        }else{   
            props.put("mail.smtp.auth","false");   
            session=Session.getInstance(props, null);   
        }   
        session.setDebug(true);   
        Transport trans = null;     
        try {   
            Message msg = new MimeMessage(session);    
            try{   
                Address from_address = new InternetAddress(userInfo.getStrSmtpFrom(), displayName);   
                msg.setFrom(from_address);   
            }catch(java.io.UnsupportedEncodingException e){   
                e.printStackTrace();   
            }   
//            设置收信人
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
			//设置抄送人
			if(cc!=null)
				msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
//			设置暗送人
			if(bcc!=null)
				msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
            msg.setSubject(subject);   
            Multipart mp = new MimeMultipart();   
            MimeBodyPart mbp = new MimeBodyPart();   
            mbp.setContent(content.toString(), "text/html;charset=gb2312");   
            mp.addBodyPart(mbp);     
            if(!file.isEmpty()){//有附件   
                Enumeration efile=file.elements();   
                while(efile.hasMoreElements()){    
                    mbp=new MimeBodyPart();   
                    filename=efile.nextElement().toString(); //选择出每一个附件名   
                    FileDataSource fds=new FileDataSource(filename); //得到数据源   
                    mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart   
                    mbp.setFileName(fds.getName());  //得到文件名同样至入BodyPart   
                    mp.addBodyPart(mbp);   
                }     
                file.removeAllElements();       
            }    
            msg.setContent(mp); //Multipart加入到信件   
            msg.setSentDate(new Date());     //设置信件头的发送日期   
            //发送信件   
            msg.saveChanges();    
            trans = session.getTransport("smtp");   
            trans.connect(userInfo.getStrSmtpServer(), userInfo.getStrUserName(), userInfo.getStrPassword());   
            trans.sendMessage(msg, msg.getAllRecipients());   
            trans.close();   
              
        }catch(AuthenticationFailedException e){      
             map.put("state", "failed");   
             message="邮件发送失败!错误原因:\n"+"身份验证错误!";   
             e.printStackTrace();    
        }catch (MessagingException e) {   
             message="邮件发送失败!错误原因:\n"+e.getMessage();   
             map.put("state", "failed");   
             e.printStackTrace();   
             Exception ex = null;   
             if ((ex = e.getNextException()) != null) {   
                 System.out.println(ex.toString());   
                 ex.printStackTrace();   
             }    
        }
        //System.out.println("\n提示信息:"+message);   
        map.put("message", message);   
        return map;   
    }   
    public class SmtpAuth extends javax.mail.Authenticator {    
        private String username,password;    
       
        public SmtpAuth(String username,String password){    
            this.username = username;     
            this.password = password;     
        }    
        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {    
            return new javax.mail.PasswordAuthentication(username,password);    
        }    
    }
	public UserInfo getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	} 
}

⌨️ 快捷键说明

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