Created
July 26, 2012 09:34
-
-
Save MaySnow/3181223 to your computer and use it in GitHub Desktop.
JavaMail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kaishengit.test; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import org.apache.commons.mail.EmailException; | |
import org.apache.commons.mail.HtmlEmail; | |
public class Test { | |
/** | |
* 在lib里导入jar包commons-email-1.2.jar和mail.jar | |
* @throws IOException | |
* | |
*/ | |
public static void main(String[] args) throws IOException { | |
//发送HTML邮件,在配置文件中建一个mailTemplate.txt配置文件(里面是一个html),然后读取,再替换里面需要替换的内容 | |
//字节流 | |
InputStream inStream = Test.class.getClassLoader().getResourceAsStream("mailTemplate.txt"); | |
//字符流 | |
InputStreamReader reader = new InputStreamReader(inStream); | |
char[] buffer = new char[512]; | |
int len = -1; | |
StringBuffer sb = new StringBuffer(); | |
while((len = reader.read(buffer)) != -1) { | |
sb.append(new String(buffer, 0, len)); | |
} | |
reader.close(); | |
String html = sb.toString(); | |
html = html.replace("{name}", "rose").replace("{link}", "http://www.kaishengit.com"); | |
HtmlEmail mail = new HtmlEmail(); | |
mail.setHostName("smtp.qq.com"); | |
mail.setAuthentication("994608455", "*******"); | |
mail.setCharset("UTF-8"); | |
mail.setTLS(true); | |
try { | |
mail.setFrom("[email protected]"); | |
mail.setSubject("注册信息激活"); | |
mail.setMsg(html); | |
mail.addTo("[email protected]"); | |
mail.send(); | |
} catch (EmailException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.mail.Email; | |
import org.apache.commons.mail.EmailAttachment; | |
import org.apache.commons.mail.EmailException; | |
import org.apache.commons.mail.MultiPartEmail; | |
public class Test { | |
/** | |
* 在lib里导入jar包commons-email-1.2.jar和mail.jar | |
* | |
*/ | |
public static void main(String[] args) { | |
//发送带附件的邮件 | |
EmailAttachment ea = new EmailAttachment(); | |
ea.setPath("F:\\1.jpg"); | |
MultiPartEmail mail = new MultiPartEmail(); | |
mail.setHostName("smtp.qq.com"); | |
mail.setAuthentication("994608455", "123456"); | |
mail.setCharset("UTF-8"); | |
mail.setTLS(true); | |
try { | |
mail.setFrom("[email protected]"); | |
mail.setSubject("注册信息激活"); | |
mail.setMsg("这是用commons email 发送的邮件"); | |
mail.addTo("[email protected]"); | |
mail.attach(ea); | |
mail.send(); | |
} catch (EmailException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream | |
at javax.mail.internet.MimeUtility.<clinit>(MimeUtility.java:1161) | |
at javax.mail.internet.InternetAddress.setPersonal(InternetAddress.java:196) | |
at org.apache.commons.mail.Email.createInternetAddress(Email.java:596) | |
at org.apache.commons.mail.Email.setFrom(Email.java:668) | |
at org.apache.commons.mail.Email.setFrom(Email.java:651) | |
at org.apache.commons.mail.Email.setFrom(Email.java:631) | |
at com.kaishengit.test.Test.main(Test.java:18) | |
Solution(解决办法): | |
1.在磁盘上找到javaee.jar(其路径可以在Java EE 5 Libraries下有显示,我的在C:\Program Files\MyEclipse\Common\plugins\com.genuitec.eclipse.j2eedt.core_10.0.0.me201110301321\data\libraryset\EE_5 ) | |
2.用winrar打卡 | |
3.删除javax目录中的mail文件夹 | |
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.qq.com:25 | |
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242) | |
at org.apache.commons.mail.Email.send(Email.java:1267) | |
at com.kaishengit.test.Test.main(Test.java:24) | |
Caused by: javax.mail.AuthenticationFailedException: 454 Authentication failed, please open smtp flag first! | |
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809) | |
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752) | |
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669) | |
at javax.mail.Service.connect(Service.java:317) | |
at javax.mail.Service.connect(Service.java:176) | |
at javax.mail.Service.connect(Service.java:125) | |
at javax.mail.Transport.send0(Transport.java:194) | |
at javax.mail.Transport.send(Transport.java:124) | |
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232) | |
... 2 more | |
Solution(解决办法): | |
邮箱的SMTP服务未开启,需要开启邮箱的SMTP服务 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.mail.Email; | |
import org.apache.commons.mail.EmailException; | |
import org.apache.commons.mail.SimpleEmail; | |
public class Test { | |
/** | |
* 在lib里导入jar包commons-email-1.2.jar和mail.jar | |
* | |
*/ | |
public static void main(String[] args) { | |
//发送普通文本邮件 | |
Email mail = new SimpleEmail(); | |
mail.setHostName("smtp.qq.com"); | |
mail.setAuthentication("994608455", "123456"); | |
mail.setCharset("UTF-8"); | |
mail.setTLS(true); | |
try { | |
mail.setFrom("[email protected]"); | |
mail.setSubject("注册信息激活"); | |
mail.setMsg("这是用commons email 发送的邮件"); | |
mail.addTo("[email protected]"); | |
mail.send(); | |
} catch (EmailException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment