javamail使用教程,正确代码,各种错误及解决方案
2016-11-11 10:10
543 查看
在实现javamail之前,你需要将程序用到的两个jar包导进来。activation.jar和mail.jar
按照流程,我走了好多遍,终于把它给走通。下面我总结了可能出现的各种原因
错误一:535 5.7.8 authentication failed 身份认证失败 原因:(1)可能是用户名密码错误(2)主机名错误
错误二:com.sun.mail.smtp.SMTPSenderFailedException: 553 Envolope sender mismatch with login user.. :登录人和信封上的发送人不匹配 原因:检查你的发件人邮箱和发件人用户名,密码
错误三:javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 501 Syntax error :错误的地址 原因:收件人邮箱格式错误
下面是我的代码,是可以跑通的
package tm.change.test;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import org.junit.Test;
public class test {
@Test
public void a(){
//(1)建立和邮件服务器的回话
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");//协议
props.setProperty("mail.smtp.host", "smtp.sina.com");//主机名
props.setProperty("mail.smtp.auth", "true");//是否开启权限控制
props.setProperty("mail.debug", "true");//是否打印发送时的信息
Session session = Session.getInstance(props);
//2)创建邮件
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("hxsf1@sina.com"));
msg.setSubject("这是一封来自java的邮件");//标题
msg.setText("你好,点击如下连接激活帐号,如果不能点击请复制连接到浏览器地址栏访问:http://localhost:8080/estore/ActiveServlet?activecode=");
msg.setRecipient(RecipientType.TO, new InternetAddress("receiver@qq.com"));//收件人
//3)发送邮件
Transport trans = session.getTransport();
trans.connect("hxsf1","QWErty");//发件人的用户名和密码
trans.sendMessage(msg, msg.getAllRecipients());
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//发件人邮箱
}
}
按照流程,我走了好多遍,终于把它给走通。下面我总结了可能出现的各种原因
错误一:535 5.7.8 authentication failed 身份认证失败 原因:(1)可能是用户名密码错误(2)主机名错误
错误二:com.sun.mail.smtp.SMTPSenderFailedException: 553 Envolope sender mismatch with login user.. :登录人和信封上的发送人不匹配 原因:检查你的发件人邮箱和发件人用户名,密码
错误三:javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 501 Syntax error :错误的地址 原因:收件人邮箱格式错误
下面是我的代码,是可以跑通的
package tm.change.test;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import org.junit.Test;
public class test {
@Test
public void a(){
//(1)建立和邮件服务器的回话
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");//协议
props.setProperty("mail.smtp.host", "smtp.sina.com");//主机名
props.setProperty("mail.smtp.auth", "true");//是否开启权限控制
props.setProperty("mail.debug", "true");//是否打印发送时的信息
Session session = Session.getInstance(props);
//2)创建邮件
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("hxsf1@sina.com"));
msg.setSubject("这是一封来自java的邮件");//标题
msg.setText("你好,点击如下连接激活帐号,如果不能点击请复制连接到浏览器地址栏访问:http://localhost:8080/estore/ActiveServlet?activecode=");
msg.setRecipient(RecipientType.TO, new InternetAddress("receiver@qq.com"));//收件人
//3)发送邮件
Transport trans = session.getTransport();
trans.connect("hxsf1","QWErty");//发件人的用户名和密码
trans.sendMessage(msg, msg.getAllRecipients());
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//发件人邮箱
}
}
相关文章推荐
- Windows10 使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案
- Qt5 : 使用QtDesigner的时候查看代码正确 但是编译的时候总出现 为定义的错误
- 浅议JavaMail使用教程以及所遇问题及解决方案(壹)
- Windows10 使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案
- Windows10下使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案
- ASP中使用存储过程,遭遇"参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突"错误
- ASP 在Windows 2003 server使用纯代码上传大文件遇到的错误,及解决方法
- 教程┊解决使用USB键盘进行游戏后按任意键出现蓝屏的错误
- HTTP常见错误代码列表汇总及解决方案
- HTTP常见错误代码列表汇总及解决方案
- map[]的正确使用:自以为是导致map的一个错误使用
- SDL入门教程(十):4、用iconv获得正确的Unicode,使用FriBiDi实现UTF-8到Unicode的正确转换
- MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API
- window 和 frame 关系的错误理解及正确使用
- 一个问题阻止windows正确检查此机器的许可证 错误代码:0x80070002
- 关于"使用注册为 allowDefinition='MachineToApplication' 的节是错误的"的解决方案
- [转载]关于"编译器失败,错误代码为128。"的解决方案
- MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API
- 最近使用asp.net时遇到 "运行时错误" 解决方案
- 【翻译】MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API