java做老师批假界面_一分钟学会JavaMail(假)__手动滑稽

importcom.sun.mail.util.MailSSLSocketFactory;import java.io.*;importjava.security.GeneralSecurityException;import java.util.*;import javax.activation.*;import javax.mail.*;import javax.mail.internet.*;public classSendMail {private String username = null;private String password = null;private Authenticator auth = null;private MimeMessage mimeMessage =null;private Properties pros = null;private Multipart multipart = null;private BodyPart bodypart= null;/*** 初始化账号密码并验证

* 创建MimeMessage对象

* 发送邮件必须的步骤:1

*@paramusername

*@parampassword*/

publicSendMail(String username,String password){this.username =username;this.password =password;

}/*** 设置email系统参数

* 接收一个map集合key为string类型,值为String

* 发送邮件必须的步骤:2

*@parammap*/

public void setPros(Mapmap){

pros= newProperties();for(Map.Entryentry:map.entrySet()){

pros.setProperty(entry.getKey(), entry.getValue());

}

}/*** 初始化MimeMessage对象

* 发送邮件必须的步骤:3*/

public voidinitMessage(){this.auth = newEmail_Autherticator();

Session session=Session.getDefaultInstance(pros,auth);

mimeMessage= newMimeMessage(session);

}/*** 验证账号密码

* 发送邮件必须的步骤

*@authorAdministrator

**/

public class Email_Autherticator extendsAuthenticator

{publicPasswordAuthentication getPasswordAuthentication()

{return newPasswordAuthentication(username, password);

}

}/*** 设置发送邮件的基本参数(去除繁琐的邮件设置)

*@paramsub 设置邮件主题

*@paramtext 设置邮件文本内容

*@paramrec 设置邮件接收人

*@throwsMessagingException

*@throwsUnsupportedEncodingException*/

public void setDefaultMessagePros(String sub,String text,String rec) throwsMessagingException, UnsupportedEncodingException{

mimeMessage.setSubject(sub);

mimeMessage.setText(text);

mimeMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(rec));

mimeMessage.setSentDate(newDate());

mimeMessage.setFrom(newInternetAddress(username,username));

}/*** 设置主题

*@paramsubject

*@throwsMessagingException*/

public void setSubject(String subject) throwsMessagingException{

mimeMessage.setSubject(subject);

}/*** 设置日期

*@paramdate

*@throwsMessagingException*/

public void setDate(Date date) throwsMessagingException{

mimeMessage.setSentDate(newDate());

}/*** 设置邮件文本内容

*@paramtext

*@throwsMessagingException*/

public void setText(String text) throwsMessagingException{

mimeMessage.setText(text);

}/*** 设置邮件头部

*@paramarg0

*@paramarg1

*@throwsMessagingException*/

public void setHeader(String arg0,String arg1) throwsMessagingException{

mimeMessage.setHeader(arg0, arg1);

}/*** 设置邮件接收人地址

*@paramrecipient

*@throwsMessagingException*/

public void setRecipient(String recipient) throwsMessagingException{

mimeMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(recipient));

}/*** 设置邮件接收人地址

*@paramlist

*@throwsMessagingException

*@throwsAddressException*/

public String setRecipients(List recs) throwsAddressException, MessagingException{if(recs.isEmpty()){return "接收人地址为空!";

}for(String str:recs){

mimeMessage.addRecipient(Message.RecipientType.TO,newInternetAddress(str));

}return "加入接收人地址成功!";

}/*** 设置邮件接收人地址

*@paramStringBuffer

*@throwsMessagingException

*@throwsAddressException*/@SuppressWarnings("static-access")public String setRecipients(StringBuffer sb) throwsAddressException, MessagingException{if(sb==null||"".equals(sb)){return "字符串数据为空!";

}

Address []address= newInternetAddress().parse(sb.toString());

mimeMessage.addRecipients(Message.RecipientType.TO, address);return "收件人加入成功";

}/*** 设置邮件发送人的名字

*@paramfrom

*@throwsUnsupportedEncodingException

*@throwsMessagingException*/

public void setFrom(String from) throwsUnsupportedEncodingException, MessagingException{

mimeMessage.setFrom(newInternetAddress(username,from));

}/*** 发送邮件

* return 是否发送成功

*@throwsMessagingException*/

public String sendMessage() throwsMessagingException{

Transport.send(mimeMessage);return "success";

}/*** 设置附件

*@paramfile 发送文件的路径*/

public void setMultipart(String file) throwsMessagingException, IOException{if(multipart==null){

multipart= newMimeMultipart();

}

multipart.addBodyPart(writeFiles(file));

mimeMessage.setContent(multipart);

}/*** 设置附件

*@paramfileList

*@throwsMessagingException

*@throwsIOException*/

public void setMultiparts(List fileList) throwsMessagingException, IOException{if(multipart==null){

multipart= newMimeMultipart();

}for(String s:fileList){

multipart.addBodyPart(writeFiles(s));

}

mimeMessage.setContent(multipart);

}/*** 发送文本内容,设置编码方式

*

*

*@params

*@paramtype

*@throwsMessagingException*/

public void setContent(String s,String type) throwsMessagingException{if(multipart==null){

multipart= newMimeMultipart();

}

bodypart= newMimeBodyPart();

bodypart.setContent(s, type);

multipart.addBodyPart(bodypart);

mimeMessage.setContent(multipart);

mimeMessage.saveChanges();

}/*** 读取附件

*@paramfilePath

*@return*@throwsIOException

*@throwsMessagingException*/

public BodyPart writeFiles(String filePath)throwsIOException, MessagingException{

File file= newFile(filePath);if(!file.exists()){throw new IOException("文件不存在!请确定文件路径是否正确");

}

bodypart= newMimeBodyPart();

DataSource dataSource= newFileDataSource(file);

bodypart.setDataHandler(newDataHandler(dataSource));//文件名要加入编码,不然出现乱码

bodypart.setFileName(MimeUtility.encodeText(file.getName()));returnbodypart;

}/*** 使用SSL登录

*@throwsGeneralSecurityException*/

public void openSSL() throwsGeneralSecurityException {

MailSSLSocketFactory sf=newMailSSLSocketFactory();

sf.setTrustAllHosts(true);

pros.put("mail.smtp.ssl.enable", "true");

pros.put("mail.smtp.ssl.socketFactory", sf);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值