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

📄 jbemail.java

📁 javamail发送程序
💻 JAVA
字号:
///////////////////////////////////
////// 发送邮件类         /////////
//////当前的函数有以下几个
//////按标记拆分字符串(函数) public String[] myinstr(String strSource,String mytag)
//////替换函数 public String myreplace(String strSource,String strFrom,String strTo)
//////发送邮件主函数 public String sendmail(String password,String username,String myfrom,String myto,String mysubject,String mytext,String mysmtp,String[] filepath,String[] newfilename,String htmlandtext)
//////
package myemail;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
import java.util.*;
import java.text.*;
import java.io.*;
public class jbemail
{
    //传入的参数有密码、姓名、谁发、发给谁、主题、正文内容、smtp地址、附件文件路径、附件的新文件名、发送类型(text/html)
    //发送邮件主函数
    public String sendmail(int myport,String password,String username,String myfrom,String myto,String mysubject,String mytext,String mysmtp,String[] filepath,String[] newfilename,String htmlandtext)
    {
    	try{
                int indexstr=0;
                if (filepath[0]!=null && !filepath[0].equals(""))
                    indexstr=1;
                    //替换字符串
               //     jbemail myjbemail=new jbemail();
               //     filepath=myjbemail.myreplace(filepath,"\\","\\\\");
               //     System.out.println("福建地址"+filepath+"服务器地址"+mysmtp+mysmtp.length());
	    	//Properties props = new Properties();
	        Properties props = System.getProperties();
	        Session sendMailSession;
	        Store store;  //收邮件时使用
	        Transport transport;//发邮件时使用
	        props.put("mail.smtp.host",mysmtp);
	        props.put("mail.smtp.auth","true");
	        SmtpAuthenticator sa=new SmtpAuthenticator(username,password);
	        sendMailSession = Session.getInstance(props,sa);
	        //sendMailSession = Session.getInstance(props,null);
	        sendMailSession.setDebug(true);

	        MimeMessage newMessage = new MimeMessage(sendMailSession);
	        newMessage.setFrom(new InternetAddress(myfrom));
	        newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(myto));
	        //设定邮件格式
	        newMessage.setSentDate(new Date());
                System.out.println(htmlandtext+"邮件正文格式");
                Multipart multipart = new MimeMultipart();
                if (htmlandtext.equals("text"))
                {
                    //获得文本格式的邮件

                    newMessage.setSubject(mysubject);
                    BodyPart messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setText(mytext);
                    multipart.addBodyPart(messageBodyPart);
                }
                else if(htmlandtext.equals("html"))
                {
                    //设置邮件内容,将邮件body部分转化为HTML格式
                    newMessage.setSubject(mysubject,"gb2312");
                    BodyPart messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setDataHandler(new DataHandler(mytext,"text/html;charset=gb2312"));
                    multipart.addBodyPart(messageBodyPart);
                }
                if (indexstr>0)
                {

                    for(int i=0;i<newfilename.length;i++)
                    {
                        if (newfilename[i]!=null)
                        {
                            //创建BodyPart对象以便获得附件
                            BodyPart messageBodyPart = new MimeBodyPart();
                            System.out.println("附件地址"+filepath[i]);
                            DataSource source = new FileDataSource(filepath[i]);
                            messageBodyPart.setDataHandler(new DataHandler(source));
                            messageBodyPart.setFileName(newfilename[i]);
                            multipart.addBodyPart(messageBodyPart);
                        }
                    }
                }
                //将正文和附件添加到邮件中
                newMessage.setContent(multipart);
	        newMessage.saveChanges();
	        //transport = sendMailSession.getStore("pop3");
	        transport = sendMailSession.getTransport("smtp");
	        transport.connect(mysmtp,myport,username,password);
	        //transport.connect();
	        transport.send(newMessage,newMessage.getAllRecipients());
	        System.out.println("成功发送到"+myto);
	        return "ok";
        }
        catch(MessagingException m)
	{
                System.out.println(m.toString()+"失败");
                return myto;
	}
    }
    //不含发送附件的函数
    //传入的参数有port地址、密码、姓名、谁发、发给谁、主题、正文内容、smtp地址、发送类型(text/html)
    public String sendmail(String mailPathlog,int myport,String password,String username,String myfrom,String myto,String mysubject,String mytext,String mysmtp,String htmlandtext)
    {
    	try{
                //解码
                mysubject=java.net.URLDecoder.decode(mysubject);
	    	//Properties props = new Properties();
	        Properties props = System.getProperties();
	        Session sendMailSession;
	        Store store;  //收邮件时使用
	        Transport transport;//发邮件时使用
	        props.put("mail.smtp.host",mysmtp);
	        props.put("mail.smtp.auth","true");
	        SmtpAuthenticator sa=new SmtpAuthenticator(username,password);
                //身份验证
	        sendMailSession = Session.getInstance(props,sa);
	        //sendMailSession = Session.getInstance(props,null);
	        sendMailSession.setDebug(true);
	        MimeMessage newMessage = new MimeMessage(sendMailSession);
                try
                {
                  newMessage.setFrom(new InternetAddress(myfrom,"法律之星"));
	          newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(myto));
	        }
                catch(java.io.UnsupportedEncodingException ex)
                {
                   System.out.println(ex.toString());
                }
                //设定邮件格式
	        newMessage.setSentDate(new Date());
                System.out.println(htmlandtext+"邮件正文格式");
                Multipart multipart = new MimeMultipart();
                if (htmlandtext.equals("text"))
                {
                    //获得文本格式的邮件

                    newMessage.setSubject(mysubject);
                    BodyPart messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setText(mytext);
                    multipart.addBodyPart(messageBodyPart);
                }
                else if(htmlandtext.equals("html"))
                {
                    //设置邮件内容,将邮件body部分转化为HTML格式
                    newMessage.setSubject(mysubject,"gb2312");
                    BodyPart messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setDataHandler(new DataHandler(mytext,"text/html;charset=gb2312"));
                    multipart.addBodyPart(messageBodyPart);
                }
                //将正文添加到邮件中
                newMessage.setContent(multipart);
	        newMessage.saveChanges();
	        //transport = sendMailSession.getStore("pop3");
	        transport = sendMailSession.getTransport("smtp");
	        transport.connect(mysmtp,myport,username,password);
	        //transport.connect();
	        transport.send(newMessage,newMessage.getAllRecipients());
	        System.out.println("成功发送到"+myto+mytext);
	        return "ok";
        }
        catch(MessagingException m)
	{
                System.out.println(m.toString()+"失败");
                //生成当前日期
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Date dateTime= new Date();
                String sDateTime=dateFormat.format(dateTime);
                //生成日志文件
                try
                {
                    File filelog=new File(mailPathlog+"\\"+"mainlog.txt");
                    BufferedWriter out2=new BufferedWriter(new FileWriter(filelog.getPath(),true));
                    String newline = System.getProperty("line.separator");
                    out2.write(sDateTime+"/"+mysmtp+"/"+myfrom+"/"+myto+"/"+m.toString()+"/"+newline);
                    out2.close();
                }
                catch (IOException ex)
                {
                    System.out.println(ex.toString());
                }
                return myto;
	}
    }
    //替换函数
    //strFrom 要替换的内容,strTo替换成的内容,strSource要查找的字符串
    public String myreplace(String strSource,String strFrom,String strTo)
    {
        String strDest = "";
        int intFromLen = strFrom.length();
        int intPos;

        while((intPos=strSource.indexOf(strFrom))!=-1)
        {
            strDest = strDest + strSource.substring(0,intPos);
            strDest = strDest + strTo;
            strSource = strSource.substring(intPos+intFromLen);
         }
         strDest = strDest + strSource;
         return strDest;
    }
    //按标记拆分字符串(函数)
    //返回拆份好的字符串数组
    public String[] myinstr(String strSource,String mytag)
    {
        int j=0;//数组索引
    	int intPos;
    	int indexstr=strSource.length();
        //确定由几个分割符
        int ihCount=0;
        System.out.println("ok");
        for(int h=0;h<indexstr;h++)
        {
            char ch=Character.toLowerCase(strSource.charAt(h));
            if(ch==mytag.charAt(0))
            {
                ihCount++;
            }
        }
        ihCount=ihCount+1;
        String[] renstr=new String[ihCount];
        System.out.println(strSource+"###"+ihCount);
        //开始拆分字符串
        if (strSource.indexOf(mytag)==-1)
            renstr[0]=strSource;
        else
        {
            while((intPos=strSource.indexOf(mytag))!=-1)
            {
                renstr[j]=strSource.substring(0,intPos);
                strSource = strSource.substring(intPos+1,indexstr);
                indexstr=strSource.length();
                System.out.println(renstr[j]);
                j=j+1;
            }
            renstr[j]=strSource;
            System.out.println(renstr[j]+"#"+j);
        }
        return renstr;
    }
}

⌨️ 快捷键说明

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