通过 Gmail 发送电子邮件时出错
Getting error while sending email through Gmail
我在这里尝试使用 gmail 发送邮件,但出现以下错误
javax.mail.AuthenticationFailedException: 534-5.7.14
<https://accounts.google.com/ContinueSignIn?
sarp=1&scc=1&plt=AKgnsbu7f
534-5.7.14 Zt-
ibTxZf1iCvRrx_zqZ2e0gyU7UDBdKNf3Skj3y1daBQ4lwKDtlbWjuZVSBdqqJvWssPG
534-5.7.14
axQ9afV4DYvgwRA6V94E2JKjGlqxgk8V7wxG9-lgPZoqbzI4rgBIk8SjDYwFt06r7tzWjs
534-5.7.14 gn4zN1UWm-
_BhrTGzjP02vV710gi2NHsgX7efxMTbZSowI02n1DL31Qhf_ba5vvtN8mSkI
534-5.7.14 mutNhiGJSG0_sSI0ZAiblBGGfc1o> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 vy6sm35491986pac.38 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
我已经在网络浏览器中成功登录,但在通过代码发送时出现错误。我还没有激活两步验证。
我正在关注这些 link
JavaMail – 通过 TLS 的 GMail
使用 TLS 连接通过 Gmail SMTP 服务器发送电子邮件。
package com.mkyong.common;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "username@gmail.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
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-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
JavaMail – 通过 SSL 的 GMail
使用 SSL 连接通过 Gmail SMTP 服务器发送电子邮件。
package com.mkyong.common;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailSSL {
public static void main(String[] args) {
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");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@no-spam.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to@no-spam.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
这是我的参考linkenter link and go reference website
通过网络浏览器登录 gmail,然后单击 Clik-this。select 开启 button.Once 您 select 您将能够发送邮件。
我在这里尝试使用 gmail 发送邮件,但出现以下错误
javax.mail.AuthenticationFailedException: 534-5.7.14
<https://accounts.google.com/ContinueSignIn?
sarp=1&scc=1&plt=AKgnsbu7f
534-5.7.14 Zt-
ibTxZf1iCvRrx_zqZ2e0gyU7UDBdKNf3Skj3y1daBQ4lwKDtlbWjuZVSBdqqJvWssPG
534-5.7.14
axQ9afV4DYvgwRA6V94E2JKjGlqxgk8V7wxG9-lgPZoqbzI4rgBIk8SjDYwFt06r7tzWjs
534-5.7.14 gn4zN1UWm-
_BhrTGzjP02vV710gi2NHsgX7efxMTbZSowI02n1DL31Qhf_ba5vvtN8mSkI
534-5.7.14 mutNhiGJSG0_sSI0ZAiblBGGfc1o> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 vy6sm35491986pac.38 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
我已经在网络浏览器中成功登录,但在通过代码发送时出现错误。我还没有激活两步验证。 我正在关注这些 link
JavaMail – 通过 TLS 的 GMail 使用 TLS 连接通过 Gmail SMTP 服务器发送电子邮件。
package com.mkyong.common; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendMailTLS { public static void main(String[] args) { final String username = "username@gmail.com"; final String password = "password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); 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-email@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to-email@gmail.com")); message.setSubject("Testing Subject"); message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!"); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); }
} }
JavaMail – 通过 SSL 的 GMail 使用 SSL 连接通过 Gmail SMTP 服务器发送电子邮件。
package com.mkyong.common; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendMailSSL { public static void main(String[] args) { 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"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username","password"); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from@no-spam.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@no-spam.com")); message.setSubject("Testing Subject"); message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!"); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); }
} }
这是我的参考linkenter link and go reference website
通过网络浏览器登录 gmail,然后单击 Clik-this。select 开启 button.Once 您 select 您将能够发送邮件。