packagecom.allianture.core.sendEmail.util;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.InputStream;importjava.io.UnsupportedEncodingException;importjava.net.MalformedURLException;importjava.net.URL;importjava.security.Security;importjava.util.Date;importjava.util.Properties;importjavax.activation.DataHandler;importjavax.activation.DataSource;importjavax.activation.URLDataSource;importjavax.mail.Authenticator;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.NoSuchProviderException;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;importjavax.mail.util.ByteArrayDataSource;importcom.allianture.platform.common.api.config.FtpConfig;importcom.allianture.platform.common.api.util.FtpUtils;importcom.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;public classTestMail {//static int port = 587;
static String server = "smtp.exmail.qq.com";//邮件服务器mail.cpip.net.cn
static String from = "AA";//发送者,显示的发件人名字
static String user = "aa@aa.com";//发送者邮箱地址
static String password = "aaaa";//密码
public static void sendEmail() throwsUnsupportedEncodingException {try{
Security.addProvider(newcom.sun.net.ssl.internal.ssl.Provider());
Properties props= newProperties();
props.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.store.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
Session session= Session.getInstance(props, newAuthenticator() {protectedPasswordAuthentication getPasswordAuthentication() {return newPasswordAuthentication(user, password);
}
});
MimeMessage msg= newMimeMessage(session);
String nick=javax.mail.internet.MimeUtility.encodeText("aaa");
msg.setFrom(newInternetAddress(user, nick));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("11@qq.com", false));
msg.setSubject("testest");
msg.setSentDate(newDate());
MimeMultipart multipart= new MimeMultipart("mixed");//邮件内容,采用HTML格式
MimeBodyPart messageBodyPart = newMimeBodyPart();
messageBodyPart.removeHeader("Content-Type");
messageBodyPart.removeHeader("Content-Transfer-Encoding");
messageBodyPart.addHeader("Content-Type", "text/html; charset=gbk");
messageBodyPart.addHeader("Content-Transfer-Encoding", "base64");
messageBodyPart.setContent(getHtml(),"text/html;charset=GBK");
multipart.addBodyPart(messageBodyPart);//内嵌图片
messageBodyPart=newMimeBodyPart();//DataSource dataSource=new FileDataSource("d:/1.png");
URL url;try{
url= new URL("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
DataSource dataSource=newURLDataSource(url);
DataHandler dataHandler=newDataHandler(dataSource);
messageBodyPart.setDataHandler(dataHandler);
messageBodyPart.setContentID("testi");
multipart.addBodyPart(messageBodyPart);//添加附件//messageBodyPart=new MimeBodyPart();//DataSource dataSource1=new FileDataSource("d:/aa.doc");//dataHandler=new DataHandler(dataSource1);//messageBodyPart.setDataHandler(dataHandler);//messageBodyPart.setFileName(MimeUtility.encodeText(dataSource1.getName()));
messageBodyPart=newMimeBodyPart();
InputStream is=downloadFtp();//DataSource dataSource1=new FileDataSource("d:/aa.doc");
DataSource dataSource1=new ByteArrayDataSource(is, "application/png");
dataHandler=newDataHandler(dataSource1);
messageBodyPart.setDataHandler(dataHandler);
messageBodyPart.setFileName(MimeUtility.encodeText("aa.doc"));
multipart.addBodyPart(messageBodyPart);
}catch(MalformedURLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
msg.setContent(multipart);
msg.saveChanges();
Transport.send(msg);
}catch(NoSuchProviderException e) {
e.printStackTrace();
}catch(MessagingException e) {
e.printStackTrace();
}
}public staticString getHtml(){
InputStream is;try{
is= new FileInputStream("d:/测试.html");byte[] b = new byte[1024];int size =is.read(b);return new String(b,0,size);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}return null;
}public static void main(String args[]) throwsUnsupportedEncodingException {
sendEmail();//收件人//System.out.println(getHtml());//downloadFtp();
System.out.println("ok");
}public staticInputStream downloadFtp(){try{
FtpUtils ftpUtils= newFtpUtils();
FtpConfig config= newFtpConfig();
config.setEncode("GBK");//设置编码
config.setServer("101.31.116.513");//设置服务
config.setPort(21);//设置端口
config.setUsername("weblogic");//设置用户名
config.setPassword("weblogic");//设置密码
config.setLocation("/home/weblogic/ebiz/mailtest");boolean flag =ftpUtils.connectServer(config);
System.out.println(flag);
System.out.println(ftpUtils.getFileList());
InputStream is=ftpUtils.getFtpClient().retrieveFileStream("aa.doc");
ftpUtils.closeServer();returnis;
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}return null;
}
}