[英]Login failed with Javax.mail via google
我一直在筆記本電腦上成功使用以下代碼,收件人收到了預期的郵件。 但是,將代碼放在jar中並從托管服務器運行時。 我收到以下錯誤。
碼
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", 465);
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.transport.protocol", "smtp");
Session mailSession = null;
mailSession = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("emailadress", "password");
}
});
try {
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Todays Email!:");
message.setFrom(new InternetAddress("theemailsender@gmail.com"));
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient1@gmail.com"));
String body = theBody;
message.setContent(body,"text/html");
transport.connect();
transport.sendMessage(message,message.getAllRecipients());
transport.close();
} catch (Exception exception) {
}
錯誤
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: STARTTLS requested but already using SSL
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
我假設此時必須將startle.enable或ssl.enable設置為true,但不能同時設置為true。
如果可以,這段代碼對我有用。 您可以刪除與附件相關的零件。
public static void sendEmailWithAttachments(String host, String port, final String userName,
final String password, String toAddress, String subject, String message,
String[] attachFiles)
throws AddressException, MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", userName);
properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile(filePath);
} catch (IOException ex) {
ex.printStackTrace();
}
multipart.addBodyPart(attachPart);
}
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
在安全模式下,您可能必須將端口465替換為587。
遵循以下您的問題有望解決
首先登錄您的Gmail帳戶,然后單擊以下路徑: google帳戶
在安全塊中,剛打開
允許安全性較低的應用:開啟
希望它能節省您的一天.....祝您好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.