锁定老帖子 主题:用javamail写的邮件发不出去
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-04-15
String host = "smtp.163.com"; String from = "ptvnet@163.com"; String to = "sshu0205@163.com"; Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth","true"); props.put("username","ptvnet"); props.put("password","******"); Session session = Session.getDefaultInstance(props, null); session.setDebug(true) ; MimeMessage message = new MimeMessage(session); message.setSubject("设置主题"); message.setContent("hello!", "text/plain"); Address sendaddress = new InternetAddress(from); Address toAddress = new InternetAddress(to); message.setFrom(sendaddress); message.addRecipient(Message.RecipientType.TO, toAddress); message.saveChanges(); // implicit with send() Transport.send(message); System.out.println("success!"); 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-04-15
提示这个错误:
nested exception is: class javax.mail.AuthenticationFailedException at javax.mail.Transport.send0(Transport.java:218) at javax.mail.Transport.send(Transport.java:80) |
|
返回顶楼 | |
发表时间:2007-04-15
你搜索一下,你那个服务器肯定是需要验证才能发邮件的.你这样把用户名和密码给它,它不认识的.我记得好象是有一个什么验证类.我以前写过,现在忘记了.
|
|
返回顶楼 | |
发表时间:2007-04-15
我用foxmail的时候,163的邮箱经常出现认证失败,可能是163邮件系统本身的问题,你换个其它的邮箱实验下.
|
|
返回顶楼 | |
发表时间:2007-04-15
static class SmtpAuth extends javax.mail.Authenticator {
private String user,password; public void setUserinfo(String getuser,String getpassword){ user = getuser; password = getpassword; } protected javax.mail.PasswordAuthentication getPasswordAuthentication(){ return new javax.mail.PasswordAuthentication(user,password); } } 然后这样用: SmtpAuth sa = new SmtpAuth(); sa.setUserinfo("ptvnet", "*****"); Session session = Session.getDefaultInstance(props, sa); |
|
返回顶楼 | |
发表时间:2007-04-25
Session session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }}); 上面是简单的写法 另外建议直接使用apache 的commons-email,这个对javamail做了封装,使用更方便了。 |
|
返回顶楼 | |
发表时间:2007-06-29
grandboy 写道 static class SmtpAuth extends javax.mail.Authenticator {
private String user,password; public void setUserinfo(String getuser,String getpassword){ user = getuser; password = getpassword; } protected javax.mail.PasswordAuthentication getPasswordAuthentication(){ return new javax.mail.PasswordAuthentication(user,password); } } 然后这样用: SmtpAuth sa = new SmtpAuth(); sa.setUserinfo("ptvnet", "*****"); Session session = Session.getDefaultInstance(props, sa); 没有用!我用@chinaren.com也发不出去,加了Authentication类验证也没有用,这次是报这个错: 535 Error: authentication failed 或者@163.com也会报这个错也可能报:550用户被锁定 javax.mail.AuthenticationFailedException 奇怪是有的@163.com可以发出去邮件. 有更好的解决办法没有... |
|
返回顶楼 | |
发表时间:2007-06-29
打开session的debug,把smtp命令与返回,贴出来看看。
|
|
返回顶楼 | |
发表时间:2007-06-29
/**
* 初始化SMTP服务器地址、发送者E-mail地址、接收者、主题、内容 */ public Mail(String smtpServer,String from,String displayName,String to,String subject,String content){ this.smtpServer=smtpServer; this.from=from; this.displayName=displayName; this.ifAuth=false; this.to=to; this.subject=subject; this.content=content; } /** * 发送邮件 */ public HashMap send(){ HashMap map=new HashMap(); map.put("state", "success"); String message="邮件发送成功!"; Session session=null; Properties props = System.getProperties(); props.put("mail.smtp.host", smtpServer); if(ifAuth){ //服务器需要身份认证 props.put("mail.smtp.auth","true"); SmtpAuth smtpAuth=new SmtpAuth(username,password); session=Session.getDefaultInstance(props, smtpAuth); }else{ props.put("mail.smtp.auth","false"); session=Session.getDefaultInstance(props, null); } session.setDebug(true); Transport trans = null; try { Message msg = new MimeMessage(session); try{ Address from_address = new InternetAddress(from, displayName); msg.setFrom(from_address); }catch(java.io.UnsupportedEncodingException e){ e.printStackTrace(); } InternetAddress[] address={new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO,address); msg.setSubject(subject); Multipart mp = new MimeMultipart(); MimeBodyPart mbp = new MimeBodyPart(); mbp.setContent(content.toString(), "text/html;charset=gb2312"); mp.addBodyPart(mbp); if(!file.isEmpty()){//有附件 Enumeration efile=file.elements(); while(efile.hasMoreElements()){ mbp=new MimeBodyPart(); filename=efile.nextElement().toString(); //选择出每一个附件名 FileDataSource fds=new FileDataSource(filename); //得到数据源 mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart mp.addBodyPart(mbp); } file.removeAllElements(); } msg.setContent(mp); //Multipart加入到信件 msg.setSentDate(new Date()); //设置信件头的发送日期 //发送信件 msg.saveChanges(); trans = session.getTransport("smtp"); trans.connect(smtpServer, username, password); trans.sendMessage(msg, msg.getAllRecipients()); trans.close(); }catch(AuthenticationFailedException e){ map.put("state", "failed"); message="邮件发送失败!错误原因:\n"+"身份验证错误!"; e.printStackTrace(); }catch (MessagingException e) { message="邮件发送失败!错误原因:\n"+e.getMessage(); map.put("state", "failed"); e.printStackTrace(); Exception ex = null; if ((ex = e.getNextException()) != null) { System.out.println(ex.toString()); ex.printStackTrace(); } } //System.out.println("\n提示信息:"+message); map.put("message", message); return map; } 结果: DEBUG: setDebug: JavaMail version 1.3.1 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25 220 smtp.qq.com ESMTP QQMail Ver 2.2 DEBUG SMTP: connected to host "smtp.qq.com", port: 25 EHLO queguohua 250-smtp.qq.com 250-PIPELINING 250-SIZE 52428800 250-AUTH LOGIN 250-AUTH=LOGIN 250 8BITMIME DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "SIZE", arg "52428800" DEBUG SMTP: Found extension "AUTH", arg "LOGIN" DEBUG SMTP: Found extension "AUTH=LOGIN", arg "" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Attempt to authenticate AUTH LOGIN 334 VXNlcm5hbWU6 NTQ1MTIyNDk2 334 UGFzc3dvcmQ6 YXNkMTIzNDU2 535 Error: authentication failed DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25 220 smtp.qq.com ESMTP QQMail Ver 2.2 DEBUG SMTP: connected to host "smtp.qq.com", port: 25 EHLO queguohua 250-smtp.qq.com 250-PIPELINING 250-SIZE 52428800 250-AUTH LOGIN 250-AUTH=LOGIN 250 8BITMIME DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "SIZE", arg "52428800" DEBUG SMTP: Found extension "AUTH", arg "LOGIN" DEBUG SMTP: Found extension "AUTH=LOGIN", arg "" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Attempt to authenticate AUTH LOGIN 334 VXNlcm5hbWU6 NTQ1MTIyNDk2 334 UGFzc3dvcmQ6 YXNkMTIzNDU2 535 Error: authentication failed javax.mail.AuthenticationFailedException邮件发送失败!错误原因: 身份验证错误! at javax.mail.Service.connect(Service.java:264) at javax.mail.Service.connect(Service.java:134) at com.suntray.service2.Mail.send(Mail.java:175) at com.suntray.service2.Mail.main(Mail.java:212) 备注:代码加了Authenticator类.我用的是我QQ信箱.报这个错.如果我用我自己163.com会报550:用户被锁定.如果用gq800307@163.com就没有问题. |
|
返回顶楼 | |
发表时间:2007-06-29
声明.我引用了redleaf的代码来做测试..在此向redleaf表示谢意.
我用了很多网上示例做测试,但结果同9楼结果一样. |
|
返回顶楼 | |