我正在尝试使用此代码发送电子邮件,但是每次显示用户名和密码时都不接受。但是我已经做了一切。我还设置了我的gmail帐户来访问第三方应用程序。现在我不知道问题出在哪里。[线程“主” javax.mail.AuthenticationFailedException中的异常:535-5.7.8和密码不被接受。了解更多535 5.7.8https://support.google.com/mail/?p=BadCredentialsd8sm6051763pfd.159-gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780)
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 com.mirajhossain.TransMail.sendMail(Main.java:37)
at com.mirajhossain.Main.main(Main.java:52)][1]
这是我的代码。
class TransMail{
public static void sendMail(String recepient) throws MessagingException {
Properties properties=new Properties();
properties.put("mail.from","REDACTED");
properties.put(" mail.user","REDACTED");
properties.put(" mail.passr","REDACTED");
properties.put("mail.smtp.auth","true");
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.host","smtp.gmail.com");
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
properties.put("mail.smtp.port","587");
String myAccount="[email protected]";
String pwd="69miraj69hossain69shawon69";
Session session= Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccount,pwd);
}
});
Message message= preparemessage(session,myAccount,recepient);
Transport.send(message);
System.out.println("yes");
}
private static Message preparemessage(Session session, String myAccount,String recepient) throws MessagingException {
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress(myAccount));
message.setRecipient(Message.RecipientType.TO,new InternetAddress(recepient));
message.setSubject("Verification");
message.setText("1254562");
return message;
}
}
public class Main{
public static void main(String[] args) throws MessagingException {
TransMail.sendMail("[email protected]");
}
}
我正在使用JavaMail库。看一下它的外观:
配置:
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host); // for example smtp.gmail.com
mailSender.setPort(port); // 465
mailSender.setUsername(username); // your email
mailSender.setPassword(password); // your password
Properties properties = mailSender.getJavaMailProperties();
properties.setProperty("mail.transport.protocol", protocol); // smtps
properties.setProperty("mail.debug", debug); // as you wish
properties.setProperty("mail.smtp.auth", auth); // true
properties.setProperty("mail.smtp.starttls.enable", enable); // true
}
发送消息:
private void send(String subject, String message) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(emailFrom);
mailMessage.setTo(emailTo); // receiver
mailMessage.setSubject(subject);
mailMessage.setText(message);
mailSender.send(mailMessage);
}
尝试一下。
请确保您必须通过gmail帐户设置打开不太安全的应用访问权限。
public class Main {
public static void main(String [] args)
{
// Sender's email ID needs to be mentioned
String from = "[email protected]";
String pass ="xxxxxxx";
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
String host = "smtp.gmail.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);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
int Vcode=(int)(10000+Math.random()*(20000-10000+1));
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("Email Verification");
// Now set the actual message
message.setText("Your verification code for our app is"+Vcode+".\n"+"Enter this code for complete your sign up process");
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}