[英]Unable to send Mails using Java Mail API [Using Gmail]
I have used following code which was working fine in year 2014 but currently its not working.我使用了以下代码,该代码在 2014 年运行良好,但目前无法正常工作。
Credentials used in this code are also correct.此代码中使用的凭据也是正确的。
public class SendMail
{
public void SendMailToTheUserWhoHaveForgotThePassword(String MailTo,String Password)
{
String to = MailTo;
String from = "chatna06062016@gmail.com";
final String username = "chatna06062016";
final String password = "xxxxxxxx";
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}
);
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
message.setSubject("FORGOTTEN PASSWORD");
message.setText("Dear User The Password that you have forgotten is <b>"+Password +"</b>"+
"This email is sended you by using JavaMailAPI "
+ "HAVE A NICE DAY"
+ "DO USE THIS SERVICE WHENEVER YOU NEED IT");
Transport.send(message);
System.out.println("Sent message successfully....");
}
catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
I have got email from google like this on the usage of the method used in above class.我收到了来自谷歌的电子邮件,内容涉及上述课程中使用的方法的使用。
Hi ChatNa,
Someone just tried to sign in to your Google Account chatna06062016@gmail.com from an app that doesn't meet modern security standards.
Details:
Sunday, June 26, 2016 12:57 PM (India Standard Time)
Noida, Uttar Pradesh, India*
We strongly recommend that you use a secure app, like Gmail, to access your account. All apps made by Google meet these security standards. Using a less secure app, on the other hand, could leave your account vulnerable. Learn more.
Google stopped this sign-in attempt, but you should review your recently used devices:
What to do now unable to find out anything helpful anywhere.现在该怎么办 无法在任何地方找到任何有用的信息。
you may need to do following setting in the gmail account:您可能需要在 Gmail 帐户中进行以下设置:
goto : myaccount -> signIn & security -> connected apps & sites -> Allow less secure apps: ON转到:我的帐户 -> 登录和安全 -> 连接的应用程序和站点 -> 允许不太安全的应用程序:开启
The main issue here is that you are logging directly into the SMTP/IMAP mail server using the users login and password.这里的主要问题是您使用用户登录名和密码直接登录到 SMTP/IMAP 邮件服务器。 This is not secure.
这是不安全的。
You should consider switching to useing XOauth2 then you can verify your application and it will no longer be considered insecure.您应该考虑切换到使用XOauth2,然后您可以验证您的应用程序,它不再被视为不安全。
The XOAUTH2 mechanism allows clients to send OAuth 2.0 access tokens to the server.
XOAUTH2 机制允许客户端向服务器发送 OAuth 2.0 访问令牌。 The protocol uses encoded values shown in the following sections.
该协议使用以下部分中显示的编码值。
[connection begins]
C: C01 CAPABILITY
S: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA XLIST
CHILDREN XYZZY SASL-IR AUTH=XOAUTH2 AUTH=XOAUTH
S: C01 OK Completed
C: A01 AUTHENTICATE XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvb
QFhdXRoPUJlYXJlciB5YTI5LnZGOWRmdDRxbVRjMk52YjNSbGNrQmhkSFJoZG
1semRHRXVZMjl0Q2cBAQ==
S: A01 OK Success
[connection continues...]
Option number to was mentioned by others Just enable less secure apps.其他人提到的选项编号只启用不太安全的应用程序。 You can read all about it here Less secure apps & your Google Account and the dangers associated with enabling it.
您可以在此处阅读所有相关信息不太安全的应用程序和您的 Google 帐户以及与启用它相关的危险。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.