java向邮箱发送信息_java向邮箱发送消息失败!

出现的错误如下:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.qq.com:465

at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1469)

at org.apache.commons.mail.Email.send(Email.java:1496)

at emailtest.Test.main(Test.java:21)

Caused by: javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:950)

at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:861)

at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:766)

at javax.mail.Service.connect(Service.java:388)

at javax.mail.Service.connect(Service.java:246)

at javax.mail.Service.connect(Service.java:195)

at javax.mail.Transport.send0(Transport.java:254)

at javax.mail.Transport.send(Transport.java:124)

at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1459)

... 2 more

代码:

public class Test {

public static void main(String[] args) {

Email email = new SimpleEmail();

email.setHostName("smtp.qq.com");// 发送服务器的名字

email.setAuthenticator(new DefaultAuthenticator("123456789@qq.com", "zzz"));//邮箱名和密码

email.setSSLCheckServerIdentity(true);

email.setSSLOnConnect(true);

email.setSmtpPort(465);

try {

email.setFrom("123456789@qq.com");

email.setSubject("测试");// 发送主题

email.setMsg("邮件测试内容!");// 发送内容

email.addTo("123456789@qq.com");// 接受邮件账户

email.send();// 发送

} catch (EmailException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}// 发送邮箱

}

}

使用的jar包如下:

9c930bf6d8a92a145f7da2e9401326d2.png

使用如上代码,首先确保邮箱已经开启了 POP3、SMTP等服务。

420917b70f346af6dddb5d7475e42e6b.png

开启之后,一定要生成授权码,上面的邮箱名和密码

email.setAuthenticator(new DefaultAuthenticator("123456789@qq.com", "zzz"));//邮箱名和密码

密码是授权码,而不是你的邮箱真正的密码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java调用API发送企业邮箱,需要按照以下步骤操作: 1. 配置企业邮箱SMTP服务 首先需要在企业邮箱后台配置SMTP服务,获取SMTP服务器地址、端口号、用户名和密码等信息。一般情况下,SMTP服务器地址为smtp.exmail.qq.com,端口号为465或587。 2. 导入JavaMail库 JavaMail是一款Java邮件发送库,可以通过Maven或手动导入jar包的方式引入到项目中。 3. 编写Java代码 使用JavaMail库发送邮件的代码示例: ``` public static void sendMail(String smtpServer, String smtpPort, String username, String password, String fromAddress, String toAddress, String subject, String content) throws MessagingException { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", smtpServer); props.setProperty("mail.smtp.port", smtpPort); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.ssl.enable", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromAddress)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); message.setSubject(subject); message.setContent(content, "text/html;charset=UTF-8"); Transport.send(message); } ``` 其中,smtpServer、smtpPort、username和password为企业邮箱SMTP服务的配置信息;fromAddress和toAddress为发件人和收件人的邮箱地址;subject为邮件主题;content为邮件正文。 4. 调用Java代码 在Java程序中调用sendMail()方法即可发送邮件。 示例代码: ``` public static void main(String[] args) throws Exception { String smtpServer = "smtp.exmail.qq.com"; String smtpPort = "465"; String username = "your_email@your_company.com"; String password = "your_password"; String fromAddress = "your_email@your_company.com"; String toAddress = "recipient_email@recipient_company.com"; String subject = "Test Email"; String content = "<h1>Hello, World!</h1>"; sendMail(smtpServer, smtpPort, username, password, fromAddress, toAddress, subject, content); } ``` 以上就是使用Java调用API发送企业邮箱的步骤和代码示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值