为什么javamail发送失败???

u011052412 2013-12-28 10:27:15
前几次还能发送成功,但是后来一次都不行了

public class MailUtil {

static int port = 587;

static String server = "smtp.qq.com";//邮件服务器mail.cpip.net.cn

static String from = "张三";//发送者,显示的发件人名字

static String user = "邮箱地址";//发送者邮箱地址

static String password = "**************";//密码


public static void sendEmail(String email, String subject, String body) throws UnsupportedEncodingException {
try {
Properties props = new Properties();
props.put("mail.smtp.host",server);
props.put("mail.smtp.port","465");
props.put("mail.smtp.auth","true");
Transport transport = null;
Session session = Session.getDefaultInstance(props, null);
transport = session.getTransport("smtp");
transport.connect(server, user, password);
MimeMessage msg = new MimeMessage(session);
msg.setSentDate(new Date());
InternetAddress fromAddress = new InternetAddress(user,from,"UTF-8");
msg.setFrom(fromAddress);
InternetAddress[] toAddress = new InternetAddress[1];
toAddress[0] = new InternetAddress(email);
msg.setRecipients(Message.RecipientType.TO, toAddress);
msg.setSubject(subject, "UTF-8");
msg.setText(body, "UTF-8");
msg.saveChanges();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws UnsupportedEncodingException
{

sendEmail("2105616491@qq.com","淫荡的人","猥琐!!");//收件人
System.out.println("ok");
}


这是错误代码


ok
javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at com.javaMail.entity.MailUtil.sendEmail(MailUtil.java:38)
at com.javaMail.entity.MailUtil.main(MailUtil.java:60)



跪求高手!!!!
...全文
19610 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
菲菲12311 2016-04-23
  • 打赏
  • 举报
回复
因为现在不能用自已原来的密码了,要用他给你的授权码。
5207 2016-02-24
  • 打赏
  • 举报
回复
腾讯邮箱真麻烦: 1、要开通SMTP服务才发送 2、第三方客户端要使用授权码才能发送(除非很早前开通过SMTP) 3、不是所有的邮箱都可以使用非SSL发送,我测试了4个QQ邮箱,只有我自己的可以使用25端口发送,其他的都提示楼主的这个错误。 所以我觉得解决的方法就是加上SSL支持。
daker_129 2016-01-27
  • 打赏
  • 举报
回复
Uh oh , It's very amazing !
NeverGiveUp7 2016-01-24
  • 打赏
  • 举报
回复

QQ密码,邮箱独立密码,第三方登录密码我都试过,就是没任何反应,控制台也没有错误!
NeverGiveUp7 2016-01-24
  • 打赏
  • 举报
回复
楼主,还能看到博客吗?用了你的代码,没有任何反应,也没有出错误!求楼主帮助一下! static String from = "forever";//发送者,显示的发件人名字 static String user = "799788525@qq.com";//发送者邮箱地址 static String password = "密码";//密码
muzi1314_ 2016-01-19
  • 打赏
  • 举报
回复
以QQ邮件服务器为例,你需要在登录QQ邮箱后台在"设置"=》账号中开启POP3/SMTP服务, 才能使用使用第三方邮件服务器如QQ的SMTP服务器;
qq_30969395 2015-12-15
  • 打赏
  • 举报
回复
/** * 获得邮件会话属性 */ public Properties getProperties() { Properties p = new Properties(); Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); p.put("mail.smtp.socketFactory.fallback", "false"); p.put("mail.smtp.starttls.enable", "true"); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port", this.mailServerPort); p.put("mail.smtp.auth", validate ? "true" : "false"); p.put("mail.mime.address.strict", "false"); return p; } 这样配置可以
顾小林 2014-05-08
  • 打赏
  • 举报
回复
引用 32 楼 u012285326 的回复:
[quote=引用 28 楼 shen332401890 的回复:] 找到一个新的解决办法,javaxmail提供了一种ssl的方案 Class MailSSLSocketFactory An SSL socket factory that makes it easier to specify trust. This socket factory can be configured to trust all hosts or trust a specific set of hosts, in which case the server's certificate isn't verified. Alternatively, a custom TrustManager can be supplied. public class SimpleEmailClient { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true");// 必须 普通客户端 // props.setProperty("mail.transport.protocol", "smtp");// 必须选择协议 // MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); // Session session = Session.getDefaultInstance(props); session.setDebug(true);// 设置debug模式 在控制台看到交互信息 Message msg = new MimeMessage(session); // 建立一个要发送的信息 msg.setText("li72 welcome ");// 设置简单的发送内容 msg.setFrom(new InternetAddress("wenjian_332401890@qq.com"));// 发件人邮箱号 msg.setSubject("test"); Transport transport = session.getTransport();// 发送信息的工具 transport.connect("smtp.qq.com", 465, "wenjian_332401890@qq.com", "MEIY0Umima");// 发件人邮箱号// 和密码 // transport.connect("smtp.exmail.qq.com", 25, "wenjian_332401890@qq.com", "MEIY0Umima");// 发件人邮箱号 transport.sendMessage(msg, new Address[] { new InternetAddress( "wenjian_332401890@qq.com") });// 对方的地址 transport.close(); } } 测试代码,最终测试成功。
大神 我用你的代码为啥发布出去 ????求解啊[/quote] 你要输入自己的账号和密码 还有你的错误log呢?
smallant93 2014-05-08
  • 打赏
  • 举报
回复
为什么我的不行,总是显示这句 javax.mail.AuthenticationFailedException: 521 Error: Invalid domain name, please use smtp.qq.com. More information at http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371
gy坏坏 2014-05-07
  • 打赏
  • 举报
回复
引用 28 楼 shen332401890 的回复:
找到一个新的解决办法,javaxmail提供了一种ssl的方案 Class MailSSLSocketFactory An SSL socket factory that makes it easier to specify trust. This socket factory can be configured to trust all hosts or trust a specific set of hosts, in which case the server's certificate isn't verified. Alternatively, a custom TrustManager can be supplied. public class SimpleEmailClient { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true");// 必须 普通客户端 // props.setProperty("mail.transport.protocol", "smtp");// 必须选择协议 // MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); // Session session = Session.getDefaultInstance(props); session.setDebug(true);// 设置debug模式 在控制台看到交互信息 Message msg = new MimeMessage(session); // 建立一个要发送的信息 msg.setText("li72 welcome ");// 设置简单的发送内容 msg.setFrom(new InternetAddress("wenjian_332401890@qq.com"));// 发件人邮箱号 msg.setSubject("test"); Transport transport = session.getTransport();// 发送信息的工具 transport.connect("smtp.qq.com", 465, "wenjian_332401890@qq.com", "MEIY0Umima");// 发件人邮箱号// 和密码 // transport.connect("smtp.exmail.qq.com", 25, "wenjian_332401890@qq.com", "MEIY0Umima");// 发件人邮箱号 transport.sendMessage(msg, new Address[] { new InternetAddress( "wenjian_332401890@qq.com") });// 对方的地址 transport.close(); } } 测试代码,最终测试成功。
大神 我用你的代码为啥发布出去 ????求解啊
gy坏坏 2014-05-07
  • 打赏
  • 举报
回复
引用 20 楼 sun1102 的回复:
试了下 smtp.qq.com 改成 smtp.exmail.qq.com 就好了
哥们我的到链接时就异常 能帮我解决下吗
pureweber 2014-04-24
  • 打赏
  • 举报
回复
引用 20 楼 sun1102 的回复:
试了下 smtp.qq.com 改成 smtp.exmail.qq.com 就好了
测试了,成功了!谢谢
顾小林 2014-04-08
  • 打赏
  • 举报
回复
为了回答问题我把自己的密码都贴上去了,,害我又重新改了一次密码
顾小林 2014-04-08
  • 打赏
  • 举报
回复
找到一个新的解决办法,javaxmail提供了一种ssl的方案 Class MailSSLSocketFactory An SSL socket factory that makes it easier to specify trust. This socket factory can be configured to trust all hosts or trust a specific set of hosts, in which case the server's certificate isn't verified. Alternatively, a custom TrustManager can be supplied. public class SimpleEmailClient { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true");// 必须 普通客户端 // props.setProperty("mail.transport.protocol", "smtp");// 必须选择协议 // MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); // Session session = Session.getDefaultInstance(props); session.setDebug(true);// 设置debug模式 在控制台看到交互信息 Message msg = new MimeMessage(session); // 建立一个要发送的信息 msg.setText("li72 welcome ");// 设置简单的发送内容 msg.setFrom(new InternetAddress("wenjian_332401890@qq.com"));// 发件人邮箱号 msg.setSubject("test"); Transport transport = session.getTransport();// 发送信息的工具 transport.connect("smtp.qq.com", 465, "wenjian_332401890@qq.com", "MEIY0Umima");// 发件人邮箱号// 和密码 // transport.connect("smtp.exmail.qq.com", 25, "wenjian_332401890@qq.com", "MEIY0Umima");// 发件人邮箱号 transport.sendMessage(msg, new Address[] { new InternetAddress( "wenjian_332401890@qq.com") });// 对方的地址 transport.close(); } } 测试代码,最终测试成功。
顾小林 2014-04-08
  • 打赏
  • 举报
回复
引用 20 楼 sun1102 的回复:
试了下 smtp.qq.com 改成 smtp.exmail.qq.com 就好了
换企业版的的确解决了问题,但请问一下为什么另smtp.qq.com不能使用呢, DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN failed 应该是登录获取权限失败。
nb0574119 2014-03-05
  • 打赏
  • 举报
回复
引用 25 楼 u013456765 的回复:
终于找到该问题的解决办法了, smtp.qq.com 改成 smtp.exmail.qq.com 就好了
220 smtp.qq.com Esmtp QQ Mail Server EHLO QQ 250-smtp.qq.com 250-PIPELINING 250-SIZE 52428800 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN 250-MAILCOMPRESS 250 8BITMIME AUTH LOGIN 334 VXNlcm5hbWU6 Njk5MzXxYDA= 530 Error: A secure connection is requiered(such as ssl). More information at ht tp://service.mail.qq.com/cgi-bin/help?id=28 失去了跟主机的连接。 C:\Documents and Settings\admin>
向下生长的花 2014-03-04
  • 打赏
  • 举报
回复
终于找到该问题的解决办法了, smtp.qq.com 改成 smtp.exmail.qq.com 就好了
  • 打赏
  • 举报
回复
这代码在我这里运行,控制台啥也没输出,也不只对不对
xnx3 2014-01-27
  • 打赏
  • 举报
回复
设置QQ邮箱属性 http://user.qzone.qq.com/921153866/blog/1390792186
xue_x_d 2014-01-17
  • 打赏
  • 举报
回复
引用 21 楼 xue_x_d 的回复:
[quote=引用 20 楼 sun1102 的回复:] 试了下 smtp.qq.com 改成 smtp.exmail.qq.com 就好了
的确可以用。看来qq邮箱是对测试邮件做了专门的处理。。[/quote] 上面回答有问题,找到qq官方的文档了 http://service.exmail.qq.com/cgi-bin/help?id=28&no=1000585&subtype=1 如果您的电子邮件客户端支持SSL,可以在设置中选择使用SSL。 通用配置参数: (我们已经默认都支持这些协议,用户无需自己手动开启这些服务器与端口) POP3/SMTP协议 接收邮件服务器:pop.exmail.qq.com ,使用SSL,端口号995 发送邮件服务器:smtp.exmail.qq.com ,使用SSL,端口号465 海外用户可使用以下服务器 接收邮件服务器:hwpop.exmail.qq.com ,使用SSL,端口号995 发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465 IMAP协议 接收邮件服务器:imap.exmail.qq.com ,使用SSL,端口号993 发送邮件服务器:smtp.exmail.qq.com ,使用SSL,端口号465 海外用户可使用以下服务器 接收邮件服务器:hwimap.exmail.qq.com ,使用SSL,端口号993 发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465
加载更多回复(20)
适用人群通用各大网易系,腾讯QQ系,新浪系,阿里系等主流邮箱;同时也适用于企业开发的企业邮箱,进行收件和发件。课程概述通用各大网易系,腾讯QQ系,新浪系,阿里系等主流邮箱;同时也适用于企业开发的企业邮箱,进行收件和发件。POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议。它是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的SMTP 的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。   SMTP 认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录 SMTP 服务器,这就使得那些垃圾邮件的散播者无可乘之机。。【开发者如何进行快速开发邮件发送系统???本课程系统进行快速研发,项目实战】 部分截图如下:完整版请查看课件或者视频

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧