emailthread.java

来自「javamail发送程序」· Java 代码 · 共 171 行

JAVA
171
字号
////////////////////////////////////////
//////邮件发送线程类
//////本类用来生成多个线程类对象
//////本类含有一个构造器
/////////////////////////
package myemail;
import java.io.*;
import java.awt.*;
import java.text.*; //字符串格式
import java.util.*; //包含时间函数
public class EmailThread extends Thread
{
  public EmailThread(String logpath,int iPort,String[] Email,String sSmtp,String sName,String sPassword,String sFrom,String sBject,String sText,String textformat,String[] sEmailis,String[] sSmtpis,String[] sNameis,String[] sPasswordis,String[] sProtis,int iCountFrom)
  {
    this.Email=Email;
    this.sSmtp=sSmtp;
    this.smtpBackup =this.sSmtp;
    this.sName=sName;
    this.sPassword=sPassword;
    this.sFrom=sFrom;
    this.sFromBackup=this.sFrom;
    this.sBject=sBject;
    this.sText=sText;
    this.textformat=textformat;
    this.iPort=iPort;
    this.logpath=logpath;
    this.MailPathLog=this.logpath;
    String templog[]=myjbemail.myinstr(this.sName,"@");
    this.logpath=this.logpath+"\\"+templog[0].toString();
    ///////
    this.sEmailis=sEmailis;
    this.sSmtpis=sSmtpis;
    this.sNameis=sNameis;
    this.sPasswordis=sPasswordis;
    this.sProtis=sProtis;
    this.iCountSmtp=iCountFrom;
  }
  //启动线程函数
  public void Send()
  {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date dateTime= new Date();
    String sDateTime=dateFormat.format(dateTime);
    FileWriteRead.FileGain(logpath+".log",sDateTime);
    FileWriteRead.WriteText("sOutput");
    //生成进度文件
    try
    {
        File filelog=new File(logpath+".sed");
        BufferedWriter out2=new BufferedWriter(new FileWriter(filelog.getPath(),false));
        String newline = System.getProperty("line.separator");
        out2.write(smtpBackup+"/"+sFromBackup+"/"+Email.length+"/"+"0"+"/");
        out2.close();
    }
    catch (IOException ex)
    {
        System.out.println(ex.toString());
    }
    //////////
    this.start();

  }
  public void run()
  {
    try
    {
      sendok=0;
      for(int i=0;i<Email.length;i++)
      {
         String EmailDi=Email[i].toString();
         this.sendemail(EmailDi);
         sleep(1000);
      }
      this.timersend();
    }
    catch (InterruptedException ex)
    {
        System.out.println(ex.toString());
    }
  }
  //邮件发送函数
  private void sendemail(String sTo)
  {
    if(iCountNum>=iCountSmtp)
      iCountNum=0;
    //进行类型转换
    String tempiPort=sProtis[iCountNum].toString();
    iPort=java.lang.Integer.parseInt(tempiPort);
    sPassword=sPasswordis[iCountNum].toString();
    sName=sNameis[iCountNum].toString();
    sFrom=sEmailis[iCountNum].toString();
    sSmtp=sSmtpis[iCountNum].toString();
    iCountNum=iCountNum+1;
    ///
    String retSend;//发送失败的邮箱地址
    retSend=myjbemail.sendmail(MailPathLog,iPort,sPassword,sName,sFrom,sTo,sBject,sText,sSmtp,textformat);
    try
    {
      this.sleep(5000);
    }
    catch(InterruptedException e)
    {
      System.out.println(e.toString());
    }
    sendok=sendok+1;
    if (!retSend.equals("ok"))
    {
      retSend=iPort+"/"+sPassword+"/"+sName+"/"+sFrom+"/"+sTo+"/"+sBject+"/"+sSmtp+"/"+textformat;//端口、密码、姓名、来自、发到、主题、smtp、格式
      //如果失败则写入日志文件
      FileWriteRead.FileGain(logpath+".log",retSend);
      FileWriteRead.WriteText("sAppend");
      //失败修改数据库
      //myselsql.Sqlgai(sTo,"sNo");
    }
    else
    {
      //成功修改数据库
      String sfile=myselsql.Sqlgai(sTo,"sYes");
    }
    //生成进度文件
    try
    {
        File filelog=new File(logpath+".sed");
        BufferedWriter out2=new BufferedWriter(new FileWriter(filelog.getPath(),false));
        String newline = System.getProperty("line.separator");
        out2.write(smtpBackup+"/"+sFromBackup+"/"+Email.length+"/"+sendok+"/");
        out2.close();
    }
    catch (IOException ex)
    {
        System.out.println(ex.toString());
    }
    //////////
  }
  //启动定时发送功能
  private void timersend()
  {
      //发信结束后启动定时发送功能
      String sTimerLog=logpath+".log";
      myEmailTimer=new TryTimer(sTimerLog,MailPathLog);
      System.out.println("已经启动定时发送退回邮件功能");
      myEmailTimer.start();
  }
  private String sSmtp;//smtp地址
  private int iPort;//port端口号
  private String sName;//用户姓名
  private String sPassword;//用户密码
  private String[] Email;//要发送的邮件地址
  private String sFrom;//发送人
  private String sBject;//主题
  private String sText;//发送的正文文本
  private String textformat;//发送的方式(text/heml)
  private String logpath;//日志文件路径
  private jbemail myjbemail=new jbemail();//生成邮件发送类对象
  private FileWrite FileWriteRead=new FileWrite();//生成日志记录类对象
  private TryTimer myEmailTimer;//自动定时发送类
  private selsql myselsql=new selsql();//数据库操作类
  private int sendok;
  private String MailPathLog;
  ////
  String[] sSmtpis;//邮件发送服务器的smtp地址
  String[] sEmailis;//邮件发送服务器邮箱地址
  String[] sNameis;//邮件发送服务器的用户名
  String[] sPasswordis;//邮件发送服务器的密码
  String[] sProtis;//邮件发送服务器的端口
  int iCountSmtp;//邮件发送服务器的总数
  int iCountNum=0;//发送邮件的随机数
  ///
  String smtpBackup=new String();
  String sFromBackup=new String();
}

⌨️ 快捷键说明

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