如何在java中发送SMTP邮件? [英] How to send SMTP mail in java?
本文介绍了如何在java中发送SMTP邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码在java中发送SMTP电子邮件有问题。
当我点击按钮时,它没有任何反馈(没有JOptionPane)而且我没有收到邮件。
我是java的初学者,我不知道如何修复它。
请给我。谢谢。
这是我的代码:
I have a problem with the code send SMTP email in java.
When I click button, it does not have any feedback (no JOptionPane) and I don't get mail.
I am beginner in java and I don't know how to fix it.
Please hepl me. Thank.
Here is my code:
private void btn_mailActionPerformed(java.awt.event.ActionEvent evt) {
Properties pro = new Properties();
pro.put("mail.smtp.host","smtp.gmail.com");
pro.put("mail.smtp.socketFactory.port","465");
pro.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLServerSocketFactory");
pro.put("mail.smtp.auth","true");
pro.put("mail.smtp.port","465");
Session session = Session.getDefaultInstance(pro,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc@gmail.com","abc") ;
}
}
);
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("xyz@gmail.com"));
message.setSubject("Test Mail");
message.setText("Hello how are you???");
Transport.send(message);
JOptionPane.showMessageDialog(null, "Send");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
推荐答案
Gmail不适合测试。他们收到邮件一段时间后禁止你 - imho 15min。
使用本地邮件服务器,如免费的https://www.hmailserver.com/
(设置中还有禁止选项,可以轻松关闭!)
Gmail is not good for testing. They ban you after you have received mails for a certain time - imho 15min.
Use a local mailserver like the free https://www.hmailserver.com/
(There is also a ban option in the setting, which can be switched off easily!)
这篇关于如何在java中发送SMTP邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文