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

📄 sendmail.java

📁 利用JavaMail实现的发送及接收邮件的程序
💻 JAVA
字号:
package com.digipower.automail.mailsender;import java.util.Properties;import java.util.Date;import java.util.ArrayList;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import com.digipower.automail.AutoMailConstants;public abstract class SendMail{    protected BodyPart messageBodyPart = null;    protected Multipart multipart = null;    protected MimeMessage mailMessage = null;    protected Session mailSession = null;    protected Properties mailProperties = System.getProperties();    protected InternetAddress mailFromAddress = null;    protected InternetAddress mailToAddress = null;    protected MailAuthenticator authenticator = null;    protected String mailSubject ="";    protected Date mailSendDate = null;    protected String unSendMailPath="";//待发邮件目录    public String getHtmlFromFile(String uspath){return null;}//从文件里得到HTML信息    public SendMail(String smtpHost,String username,String password){        mailProperties.put("mail.smtp.host",smtpHost);        mailProperties.put("mail.smtp.auth","true"); //设置smtp认证,很关键的一句        authenticator = new MailAuthenticator(username,password);        mailSession = Session.getInstance(mailProperties,authenticator);        mailMessage = new MimeMessage(mailSession);        messageBodyPart = new MimeBodyPart();    }    //设置邮件主题    public void setSubject(String mailSubject)throws MessagingException{        this.mailSubject = mailSubject;        mailMessage.setSubject(mailSubject);    }    //所有子类都需要实现的抽象方法,为了支持不同的邮件类型    protected abstract void setMailContent(String mailContent)throws MessagingException;    //设置邮件发送日期    public void setSendDate(Date sendDate)throws MessagingException{        this.mailSendDate = sendDate;        mailMessage.setSentDate(sendDate);    }    //设置邮件发送附件    public void setAttachments(String attachmentName)throws MessagingException,Exception{System.out.println("attachmentNamestart:" + attachmentName);        attachmentName = new String(attachmentName.getBytes(),"GBK");System.out.println("attachmentNameend:" + attachmentName);      messageBodyPart = new MimeBodyPart();        DataSource source = new FileDataSource(attachmentName);        messageBodyPart.setDataHandler(new DataHandler(source));        int index = attachmentName.lastIndexOf('\\');        //防止在传送中文名副档的时候档名出现乱码        String attachmentRealName = MimeUtility.encodeText(attachmentName.substring(index+1));System.out.println("attachmentRealName:" + attachmentRealName);        messageBodyPart.setFileName(attachmentRealName);        multipart.addBodyPart(messageBodyPart);    }    //设置发件人地址    public void setMailFrom(String mailFrom)throws MessagingException{      try      {        mailFromAddress = new InternetAddress(mailFrom, AutoMailConstants.COMPANY);        mailMessage.setFrom(mailFromAddress);      }      catch(Exception e)      {        e.printStackTrace();      }    }    //设置收件人地址,收件人类型为to,cc,bcc(大小写不限)    public void setMailTo(String mailTo,String mailType)throws Exception{        //for(int i=0;i<mailTo.length;i++){            mailToAddress = new InternetAddress(mailTo);            if(mailType.equalsIgnoreCase("to")){                mailMessage.addRecipient(Message.RecipientType.TO,mailToAddress);            }            else if(mailType.equalsIgnoreCase("cc")){                mailMessage.addRecipient(Message.RecipientType.CC,mailToAddress);            }            else if(mailType.equalsIgnoreCase("bcc")){                mailMessage.addRecipient(Message.RecipientType.BCC,mailToAddress);            }            else{                throw new Exception("Unknown mailType: "+mailType+"!");            }        //}    }    //开始投递邮件    public void sendMail()throws MessagingException,SendFailedException{        if(mailToAddress == null)        {            System.out.println("请你必须你填写收件人地址!");            System.exit(1);        }        else        {            mailMessage.setContent(multipart);            System.out.println("正在发送邮件,请稍候.......");            Transport.send(mailMessage);            System.out.println("恭喜你,邮件已经成功发送!");        }    }    public String getUnSendMailPath() {      return unSendMailPath;    }    public void setUnSendMailPath(String unpath) {      this.unSendMailPath = unpath;    }    public void setHeader(String replyName ,String replyAddress, String subject, String folderName) throws Exception{      // mailMessage.setHeader("Return-Receipt-To","\"jack\" <sky.x.shao@mydigipower.com>");      //mailMessage.setHeader("Disposition-Notification-To","\"" + replyName + "\"<" + replyAddress + ">");      mailMessage.setHeader("Disposition-Notification-To", AutoMailConstants.REPLY_SEPARATOR + folderName + "<" + replyAddress + ">");    }    /**     *     * @param replyTo String     * @throws MessagingException     */    public void setReplyTo(String replyTo) throws MessagingException{          try          {            InternetAddress temp = new InternetAddress(replyTo);           Address [] addressArray = new InternetAddress [] {temp};            mailMessage.setReplyTo(addressArray);          }          catch(Exception e)          {            e.printStackTrace();          }        }    public void setDescription( String FolderName, String strMail) throws Exception{      mailMessage.setDescription(FolderName + strMail + AutoMailConstants.POSTMASTER_SEPARATOR);    }}

⌨️ 快捷键说明

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