java email发送(附件中文的处理)

这里使用的是commons-email-1.3.2.jar进行的开发,自认为这是简单的邮件发送。 
package com.yt.base.common; 

import java.io.UnsupportedEncodingException; 

import javax.mail.internet.MimeUtility; 

import org.apache.commons.mail.DefaultAuthenticator; 
import org.apache.commons.mail.EmailAttachment; 
import org.apache.commons.mail.EmailException; 
import org.apache.commons.mail.HtmlEmail; 

public class email { 

public static void main(String[] args) { 
// TODO Auto-generated method stub 
try { 
HtmlEmail textemail = new HtmlEmail(); 
textemail.setHostName("smtp.163.com");//smtp邮箱服务器的地址 
textemail.setSmtpPort(25);//smtp的端口号 
textemail.setAuthenticator(new DefaultAuthenticator("xxx@163.com", "soiladmin"));//发送放的用户名和密码 
textemail.setSSLOnConnect(false); 
textemail.setFrom("xxx@163.com");//发送人 
textemail.setSubject("标题");//发送标题 

String info = "aaaaaaaaaaaa"; 
textemail.setHtmlMsg(info);//发送邮箱内容 
textemail.addTo("2xx@qq.com");//给什么人(用户)发送的邮箱地址 

//email附件发送开始 
EmailAttachment attach = new EmailAttachment(); 
String fileN="D:\\msj.dox"; 
attach.setPath(fileN); 
attach.setDisposition(EmailAttachment.ATTACHMENT); 
try { 
attach.setName(MimeUtility.encodeText(fileN)); 
} catch (UnsupportedEncodingException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
textemail.attach(attach); 
//email附件发送结束 
String aa= textemail.send(); 
} catch (EmailException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 
}

 

posted @ 2019-09-06 11:31  比较杂的代码工程师  阅读(385)  评论(0编辑  收藏  举报