[英]Sending email by using java code
Good morning . 早上好 。 I have a question .
我有个问题 。 I want to send Email via Gmail by using eclipse oxygen.3 march 2018 .
我想使用eclipse oxygen通过Gmail发送电子邮件.2018年3月3日。 I used two jars : mail-1.4.7 and activation 1.1.1 .
我使用了两个jar:mail-1.4.7和激活1.1.1。 And I allowed less secure access for my Gmail account .
而且我允许我的Gmail帐户的安全访问权限降低。 this is my code :
这是我的代码:
package Login_sys;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class EmailSend {
final String emailSMTPserver = "smtp.gmail.com";
final String emailServerPort = "587";
String receiverEmail = null;
String emailSubject = null;
String emailBody = null;
String from = null ;
String password = null ;
public EmailSend(String from,String password,String subject ,String message,String to)
{
this.from = from ;
this.password = password ;
this.emailSubject = subject ;
this.emailBody = message ;
this.receiverEmail = to ;
Properties props = System.getProperties();
props.put("mail.smtp.user", from);
props.put("mail.smtp.host", emailSMTPserver);
props.put("mail.smtp.port", emailServerPort);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.password", password);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", emailServerPort);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
Message msg = new MimeMessage(session);
msg.setText(emailBody);
msg.setSubject(emailSubject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(receiverEmail));
Transport.send(msg);
System.out.println("send successfully");
} catch (Exception ex) {
System.err.println("Error occurred while sending.!");
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from , password);
}
}}
I call it from other class : 我从其他班级调用它:
EmailSend f = new EmailSend ("******@gmail.com","*****","Testing","testin
email","****@gmail.com");
it always print : Error occurred while sending.! 它总是打印:发送时发生错误。 .
。 I don't know where is my mistake
我不知道我的错误在哪里
You have set the wrong port number. 您设置了错误的端口号。 Change it to:
更改为:
final String emailServerPort = "465";
Gmail SMTP port (TLS): 587
Gmail SMTP端口(TLS):587
Gmail SMTP port (SSL): 465
Gmail SMTP端口(SSL):465
To fix the SSLHandshakeException
, you need to update your cacerts
in JAVA_HOME/jre/lib/security
. 要修复
SSLHandshakeException
,您需要在JAVA_HOME/jre/lib/security
更新cacerts
。 Get a copy of InstallCert.java
. 获取
InstallCert.java
的副本。 Have a look here: InstallCert.java 在这里看看: InstallCert.java
You can either place the jssecacerts
under your /lib/security
or use KeyStore Explorer and export the certificate from jssecacerts
and import it to cacerts
. 您可以将
jssecacerts
放在/lib/security
也可以使用KeyStore Explorer从jssecacerts
导出证书并将其导入cacerts
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.