无法使用Servlet和JavaMail发送电子邮件 [英] Unable to send email using servlet and JavaMail
问题描述
我正在使用JavaMail
发送电子邮件,但由于javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials g188sm3298732pfc.24 - gsmtp
I am working on sending emails using JavaMail
,but getting error as javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials g188sm3298732pfc.24 - gsmtp
- 我正在做的是,我有一个
EmailUtility
类. - 上面的类有一个静态方法
sendEmail()
–它以SMTP服务器设置和邮件详细信息为参数. - 我正在将SMTP服务器设置放入我的web.xml文件中 但不知道出了什么问题
- What i am doing is, i have a class as
EmailUtility
. - The above class has one static method,
sendEmail()
– which takes SMTP server settings and message details as its arguments. - I am putting SMTP server settings in my web.xml file but don't know what is going wrong
我的EmailUtility
班
public class EmailUtility {
public static void sendEmail(String host, String port, final String userName, final String password,
String toAddress, String subject, String message) throws AddressException, MessagingException {
// setting 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");
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
// 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());
msg.setText(message);
// sending the e-mail
Transport.send(msg);
}
}
这是我的web.xml文件
here is my web.xml file
<context-param>
<param-name>host</param-name>
<param-value>smtp.gmail.com</param-value>
</context-param>
<context-param>
<param-name>port</param-name>
<param-value>587</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>test.123@gmail.com</param-value>
</context-param>
<context-param>
<param-name>pass</param-name>
<param-value>12345698</param-value>
</context-param>
这是我的servlet类
And here is my servlet class
public void init() {
// reading SMTP server setting from web.xml file
ServletContext context = getServletContext();
host = context.getInitParameter("host");
port = context.getInitParameter("port");
user = context.getInitParameter("user");
pass = context.getInitParameter("pass");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// reading form fields
String recipient = request.getParameter("recipient");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
System.out.println(recipient+" sub "+subject+" content "+content);
String resultMessage = "";
try {
EmailUtility.sendEmail(host, port, user, pass, recipient, subject,
content);
resultMessage = "The e-mail was sent successfully";
} catch (Exception ex) {
ex.printStackTrace();
resultMessage = "There were an error: " + ex.getMessage();
}
我做对了,但不知道出了什么问题
I am doing the right thing but don't know what is the issue
如果我在gmail中启用了不太安全的应用"设置,那么它可以正常工作,我认为这不是解决此问题的正确方法,因为并非每个用户都愿意这样做
If I enable Less Secure App setting in gmail then its working fine, i don't think it is the right approach to solve the issue as not every user is going to do that
所以伙计们帮帮我,我不知道我在制造什么奇怪的东西,谢谢
So guys help me out, i don't know what bizarre i am making, thankyou
编辑是否有其他资源可以用来在类似gmail到yahoo这样的跨平台上发送邮件,我愿意使用任何其他资源来完成我要尝试的任务
Edit is there any other resource by which i can send mails on cross platforms like from gmail to yahoo like that, i am open to use any other resource which can do the task i am trying to
推荐答案
不同的服务器具有不同的安全要求.如果要处理所有这些问题,则在应用程序中将需要一些特殊情况.
Different servers have different security requirements. If you want to handle all of them, you're going to need some special cases in your application.
有时候,您需要用户为电子邮件服务器创建应用专用密码.
Sometimes you'll need the user to create an app-specific password for the email server.
有时候,如果服务器支持,则需要支持 OAuth .
Sometimes you'll need to support OAuth if the server supports it.
这篇关于无法使用Servlet和JavaMail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!