java实现发邮件,
导入mail.jar 和activation.jar,common-email.jar
1,发送普通文本EMAIL
代码:
public void sendEmail(String userEmail,String emailTheme,String emailConnection) { Properties p = new Properties(); try { p.load(OpinionfeedbackAction.class.getClassLoader().getResourceAsStream("email.properties")); String emailName = p.getProperty("email.name"); String emailPwd = p.getProperty("email.password"); String hostName = p.getProperty("email.hostname"); String emailFullName = p.getProperty("email.fullName"); //System.out.println("emialFullName:"+ emailFullName); Email email = new SimpleEmail(); email.setHostName(hostName); email.setAuthentication(emailFullName,emailPwd);//这里写emailName也可以 email.setCharset("UTF-8"); email.setTLS(true); //email.setFrom(new InternetAddress(emailFullName,"国家林业科学数据平台").toString());//这里改昵称,也许会报错 email.setFrom(emailFullName,"普通邮件测试");//这里改昵称 email.setSubject(emailTheme); email.setMsg(emailConnection); email.addTo(userEmail); email.send(); } catch (Exception e1) { e1.printStackTrace(); } }
email.properties配置文件:
email.name=emailUsername email.password=pwd email.hostname=smtp.forestdata.cn email.fullName=emailUsername@forestdata.cn
可能遇到问题:
错误一:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
解决方法:到D:\MyEclipse 6.5
\myeclipse\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_6.5.0.zmyeclipse650200806
\data\libraryset\EE_5目录下找到javaee.jar删除其中到mail文件夹。
错误二:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/BEncoderStream
解决方法:
下载mail.jar和activation.jar文件加入到工程中。
2,发送HTML邮件。
其实也就是把Email e = new SimpleEmail()改为HtmlEmail e = new HtmlEmail();
然后再把发送内容的方法改为:e.setHtmlMsg(Content);即可
具体代码如下:
package com.peng; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.apache.commons.mail.Email; import org.apache.commons.mail.HtmlEmail; import org.apache.commons.mail.SimpleEmail; import org.apache.struts2.ServletActionContext; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; public class EmailTest { public static void main(String[] args) throws Exception { //String inputpath = "D:\\zipTest"; //String zipOutpath = "D:\\ziptest.zip"; //zipTest(inputpath,zipOutpath); // unZip ( "D:\\ziptest.zip" , "D://opt" ); StringBuilder c = new StringBuilder(); c.append("<a href=\"http://www.baidu.com\">链接到百度<a>"); c.append("<div style=\" color:red;\">红色DIV</div>"); SendEmail("[email protected]", c.toString(),"邮件测试"); } public static void SendEmail(String email,String content,String theme) throws Exception{ //Email e = new SimpleEmail(); HtmlEmail e = new HtmlEmail(); e.setHostName("smtp.163.com"); e.setAuthentication("pengenjing","pwd"); e.setCharset("Utf-8"); e.setTLS(true); e.setFrom("[email protected]","HTML测试"); e.setSubject(theme); //e.setMsg(content); e.setHtmlMsg(content); e.addTo(email); e.send(); } }
3,发送附件
public String sendEnclosure() throws EmailException{ String result = null; EmailAttachment emailattachment = new EmailAttachment(); //设置附件路径 emailattachment.setPath(file); //System.out.println(path); emailattachment.setDisposition(EmailAttachment.ATTACHMENT); //附件描述 emailattachment.setDescription("This is Smile picture"); /* * 设置附件的中文乱码问题,解决附件的中文名称 乱码问题 */ BASE64Encoder enc = new BASE64Encoder(); //this.getName().getBytes()使用的是系统缺省的编码处理,这里是GBK emailattachment.setName("=?GBK?B?"+enc.encode(file.getBytes())+"?="); //attachment.setName(this.getName()); //不处理字符集的话,发送的附件中文名称是乱码 //创建一个email MultiPartEmail multipartemail = new MultiPartEmail(); //设置主机名称 multipartemail.setHostName(UserInfo.sendHost); //设置字符编码 multipartemail.setCharset("GB2312"); //设置发送邮件目的地址 multipartemail.addTo(to); //设置发送又见源地址 multipartemail.setFrom(from); //设置用户名和密码 multipartemail.setAuthentication(UserInfo.username, UserInfo.password); //设置主题 multipartemail.setSubject(subject); //设置邮件内容 multipartemail.setMsg(msg); //添加附件 multipartemail.attach(emailattachment); //发送邮件 multipartemail.send(); result = "The attachmentEmail send sucessful!!!"; return result; }