实现简单邮件发送
配置信息
public static void config(Email email,String from ,String[] to,String[] cc, String[] bcc,String subject) throws EmailException{
email.setHostName("设置传出邮件服务器的主机名");
email.setAuthentication("邮箱","邮箱密码");
email.setSmtpPort(Integer.valueOf("端口"));
email.setCharset("UTF-8");
email.setSendPartial(true);
if(from != null && !StringUtils.isEmpty(from)){
email.setFrom(from);
}else{
return ;
}
if(to!= null && StringUtils.isNotEmpty(to.toString())){
email.addTo(to);
}
if(null!=cc && StringUtils.isNotEmpty(cc.toString())){
email.addCc(cc);
}
if(null!=bcc && StringUtils.isNotEmpty(bcc.toString())){
email.addBcc(bcc);
}
email.setSubject(subject);
}
邮件发送
public static String send(String from,String[] to,String subject,String content,String[] cc,List<File> fileList,String[] fileName, String[] bcc){
MultiPartEmail email = new MultiPartEmail();
try {
config(email,from,to,cc,bcc,subject);
email.setMsg(content);
if(fileList!=null){
for(int i=0;i<fileList.size();i++){
File file = fileList.get(i);
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(file.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("file");
attachment.setName(fileName[i]);
email.attach(attachment);
}
}
email.send();
} catch (EmailException e) {
e.printStackTrace();
return "false";
}catch(Exception e){
e.printStackTrace();
return "false";
}finally{
if(fileList!=null){
for(int i=0;i<fileList.size();i++){
fileList.get(i).delete();
}
}
}
return "true";
}
邮件发送 删除临时文件
public static String send1(String from,String[] to,String subject,String content,String[] cc,List<File> fileList,String[] fileName, String[] bcc,boolean deleteFile){
MultiPartEmail email = new MultiPartEmail();
try {
config(email,from,to,cc,bcc,subject);
email.setMsg(content);
if(fileList!=null){
for(int i=0;i<fileList.size();i++){
File file = fileList.get(i);
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(file.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("file");
attachment.setName(fileName[i]);
email.attach(attachment);
}
}
email.send();
} catch (EmailException e) {
e.printStackTrace();
return "false";
}catch(Exception e){
e.printStackTrace();
return "false";
}finally{
if(deleteFile){
if(fileList!=null){
for(int i=0;i<fileList.size();i++){
fileList.get(i).delete();
}
}
}
}
return "true";
}
Html 邮件发送
public static String sendHtmlEmail(String from,String[] to,String[] cc, String[] bcc,String subject,String content,List<File> fileList,String[] fileName) {
ImageHtmlEmail email = new ImageHtmlEmail();
try {
config(email,from,to,cc,bcc,subject);
DataSourceResolver[] dataSourceResolvers =
new DataSourceResolver[]{new DataSourceFileResolver()};
email.setDataSourceResolver(new DataSourceCompositeResolver(dataSourceResolvers));
email.setHtmlMsg(content);
email.setTextMsg("Your email client does not support HTML messages");
if(fileList!=null){
for(int i=0;i<fileList.size();i++){
File file = fileList.get(i);
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(file.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(fileName[i]);
attachment.setName(fileName[i]);
email.attach(attachment);
}
}
email.send();
}
catch (EmailException e) {
e.printStackTrace();
return "false";
}catch(Exception e){
e.printStackTrace();
return "false";
}finally{
if(fileList!=null){
for(int i=0;i<fileList.size();i++){
fileList.get(i).delete();
}
}
}
return "true";
}