Sud*_*yak 6 java email jakarta-mail
我想使用JavaMailAPI发送邮件
我做了一些编码,但它没有工作抛出异常: -
消息发送Failedjavax.mail.AuthenticationFailedException:534-5.7.9需要特定于应用程序的密码.
package com.appreciationcard.service;
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;
import com.appreciationcard.dao.DAOFactory;
import com.appreciationcard.forms.ComposeForm;
public class MailServiceImpl implements MailService {
public boolean mailsent(ComposeForm composeForm) {
String to = composeForm.getTo();
String from = composeForm.getFrom();
String cc = composeForm.getCc();
String bcc = composeForm.getBcc();
String subject = composeForm.getSubject();
String messages = composeForm.getMessage();
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.port", "587");
props.put("mail.transport.protocol", "smtp");
props.put("mail.debug", "true");
System.out.println("Properties" + props);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"tosudhansusekhar@gmail.com", "xxxx");
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("dynamicmihir@gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject(subject);
message.setText(messages);
Transport.send(message);
} catch (MessagingException mex) {
System.out.println("Message Sending Failed" + mex);
mex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
}
我在服务器控制台上得到了Exception
消息发送Failedjavax.mail.AuthenticationFailedException:534-5.7.9需要特定于应用程序的密码.
如需了解更多信息,请访问534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 o5sm11464195pdr.50 - gsmtp
愿任何人帮助我解决这个问题.
小智 10
只需为您的帐户创建一个应用程序密码并使用该密码即可。
创建密码的步骤:
转到您的帐户设置 ( https://myaccount.google.com/ ) -->> 安全 -->> 在登录 Google 下 -->> 应用程序密码 -->> 输入您的凭据以登录您的帐户 - ->> 选择“应用程序”和“设备”-->> 生成。
将密码复制并粘贴到某处。
您可以使用此密码代替您的帐户密码。
| 归档时间: |
|
| 查看次数: |
8018 次 |
| 最近记录: |