如何使用 Gmail 服务器使用 java.mail 从 Web 服务器发送电子邮件
How to send email from Web Server using java.mail using Gmail server
我正在尝试使用 javax.mail 使用 gmail smtp 发送电子邮件。以下是我的代码
public static void send(String from,String password,String to,String sub,String msg){
//Get properties object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
代码工作正常当我在我的本地服务器上 运行 连接它但是当我试图在 Elastic beantalk 上 运行 它时(我的服务器在 AWS 上 运行 连接EBS) then authentication fail 异常来了
注意:我已经从 Google A/c 设置中打开了对不太安全的应用程序的访问权限,但我仍然收到此错误
javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 l13sm3053341iti.6 - gsmtp
请试试这个
public static void sendPDFReportByGMail(String from, String pass, String to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
MimeMessage message = new MimeMessage(session);
try {
// Set from address
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set subject
message.setSubject(subject);
// Set Mail body
message.setText(body);
BodyPart objMessageBodyPart = new MimeBodyPart();
objMessageBodyPart.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (AddressException ae) {
ae.printStackTrace();
} catch (MessagingException me) {
me.printStackTrace();
}
}
我最近遇到了同样的问题,我发现问题出在 EC2 区域。 Google 不允许从用户不经常访问的位置登录 less Secure 应用程序。您可以使用 Google 邮件 API 或使用其他一些邮件平台,例如 Yahoo。检查您的 EC2 实例区域。尝试使用雅虎邮件跟踪代码,将其部署在 Elastic beanstalk 或您正在使用的任何环境中。这对我有用。
public void yahooSend(String mail,String subject,String msg) {
// Sender's email ID needs to be mentioned
String from = "YOUR_YAHOO_MAIL";
String pass ="YOUR_YAHOO_PASSWORD";
// Recipient's email ID needs to be mentioned.
String to = mail;
String host = "smtp.mail.yahoo.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
// props.put("mail.smtp.user", "YAHOO_USER_NAME");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(msg);
System.out.print("Sending msg "+msg);
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host,587, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System. out.println("Sent message successfully....");
}catch (MessagingException mex) {
System. out.print(mex);
mex.printStackTrace();
}
}
我正在尝试使用 javax.mail 使用 gmail smtp 发送电子邮件。以下是我的代码
public static void send(String from,String password,String to,String sub,String msg){
//Get properties object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
代码工作正常当我在我的本地服务器上 运行 连接它但是当我试图在 Elastic beantalk 上 运行 它时(我的服务器在 AWS 上 运行 连接EBS) then authentication fail 异常来了 注意:我已经从 Google A/c 设置中打开了对不太安全的应用程序的访问权限,但我仍然收到此错误
javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 l13sm3053341iti.6 - gsmtp
请试试这个
public static void sendPDFReportByGMail(String from, String pass, String to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
MimeMessage message = new MimeMessage(session);
try {
// Set from address
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set subject
message.setSubject(subject);
// Set Mail body
message.setText(body);
BodyPart objMessageBodyPart = new MimeBodyPart();
objMessageBodyPart.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (AddressException ae) {
ae.printStackTrace();
} catch (MessagingException me) {
me.printStackTrace();
}
}
我最近遇到了同样的问题,我发现问题出在 EC2 区域。 Google 不允许从用户不经常访问的位置登录 less Secure 应用程序。您可以使用 Google 邮件 API 或使用其他一些邮件平台,例如 Yahoo。检查您的 EC2 实例区域。尝试使用雅虎邮件跟踪代码,将其部署在 Elastic beanstalk 或您正在使用的任何环境中。这对我有用。
public void yahooSend(String mail,String subject,String msg) {
// Sender's email ID needs to be mentioned
String from = "YOUR_YAHOO_MAIL";
String pass ="YOUR_YAHOO_PASSWORD";
// Recipient's email ID needs to be mentioned.
String to = mail;
String host = "smtp.mail.yahoo.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
// props.put("mail.smtp.user", "YAHOO_USER_NAME");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(msg);
System.out.print("Sending msg "+msg);
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host,587, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System. out.println("Sent message successfully....");
}catch (MessagingException mex) {
System. out.print(mex);
mex.printStackTrace();
}
}