package com.cftech.cms.SendEmail;



import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
  import java.util.Enumeration;
  import java.util.Properties;
  import java.util.Vector;
  import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.Authenticator;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Multipart;
 import javax.mail.PasswordAuthentication;
 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;
 import javax.mail.internet.MimeUtility;

public class SendMail {

             public static String toChinese(String text) {
                 return text;
                }
            public static boolean sendMail(MailBean mb,String zipUrl) {
                 String host = mb.getHost();
                 final String username = mb.getUsername();
                 final String password = mb.getPassword();
                 String from = mb.getFrom();
                 String to = mb.getTo();
                 String subject = mb.getSubject();
                 String content = mb.getContent();
                 String fileName = mb.getFilename();
                 Properties props = System.getProperties();
                // 设置SMTP的主机
                 props.put("mail.smtp.host", host);
                // 需要经过验证
                 props.put("mail.smtp.auth", "true");
                 props.put("main.debug","true");
                props.put("main.debug","true");

                 Session session = Session.getInstance(props, new Authenticator() {
             public PasswordAuthentication getPasswordAuthentication() {
                                 return new PasswordAuthentication(username, password);
                             }
         });
                 try {
                         MimeMessage msg = new MimeMessage(session);
                         msg.setFrom(new InternetAddress(from));
                         InternetAddress[] address = { new InternetAddress(to) };
                         msg.setRecipients(Message.RecipientType.TO, address);
                         msg.setSubject(toChinese(subject));
                         Multipart mp = new MimeMultipart();
                         MimeBodyPart mbpContent = new MimeBodyPart();
                         mbpContent.setText(content);
                         mp.addBodyPart(mbpContent);
                         /* 往邮件中添加附件 */
                         MimeBodyPart mbpFile = new MimeBodyPart();
                         URL url = new URL(zipUrl);
                         DataSource dataSource=new URLDataSource(url);
                         mbpFile.setDataHandler(new DataHandler(dataSource));
                         mbpFile.setFileName(toChinese(mb.getFilename()+".zip"));
                         mp.addBodyPart(mbpFile);
                         System.out.println("添加成功");
                         msg.setContent(mp);
                         msg.setSentDate(new Date());
                         Transport.send(msg);
                     } catch (MessagingException me) {
                         me.printStackTrace();
                         return false;
                     } catch (MalformedURLException e) {
                     e.printStackTrace();
                 }
                return true;
             }

    public static void main(String[] args) {
        MailBean mb = new MailBean();
        mb.setHost("smtp.qq.com");
        mb.setUsername("1473570893");
        mb.setPassword("wpslkmbnnqcvfjib");
        mb.setFrom("1473570893@qq.com");
        mb.setTo("1315429856@qq.com");
        mb.setSubject("测试-斯凯孚");
        mb.setContent("您有新的邮件，注意查收!");
       // mb.attachFile("http://schaeffler-1255700813.picsh.myqcloud.com/gogi/newReCommend/附件1552532653503.zip");
        mb.setFilename("SKF");
        SendMail sm = new SendMail();
        if(sendMail(mb,"http://aidea-1255700813.picsh.myqcloud.com/gogi/newReCommend/附件1552617282288.zip")){
            //邮件发送成功
            //jsonObject.put("errorNo",0);
        }else{
            //jsonObject.put("errorNo",1);
        }
    }
}
