[英]Error in sending mail in java/servlet
我正在嘗試使用java
發送email
。 但是我遇到如下錯誤
javax.mail.AuthenticationFailedException:連接失敗,未指定密碼
通過正確的身份驗證電子郵件和密碼后,為什么會出現此錯誤?
這是我的代碼
import java.io.IOException;
import java.net.PasswordAuthentication;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestMail
*/
@WebServlet("/TestMail")
public class TestMail extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestMail() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
todo(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
todo(request,response);
}
private void todo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
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.post","587");
Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"testing@gmail.com", "testing123");
}
});
Message message=new MimeMessage(session);
try {
message.setFrom(new InternetAddress("testing@gmail.com","hello"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("testing@gmail.com"));
message.setSubject("Testing Email");
message.setText("hello this is testing mail \n \n Congrets");
Transport.send(message);
System.out.println("Mail Sent Successfully");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
JavaMail Authenticator可在javax.mail包中找到,並且與同名的java.net類不同。 兩者不共享相同的Authenticator,因為JavaMail API與Java 1.1(不具有java.net品種)一起使用。
1)請為POP3電子郵件使用正確的jar文件:
import java.util.Properties;
import javax.activation.*;
import javax.mail.*;
2)然后建立您的訊息:
Properties props = new Properties();
props.put("mail.smtp.host", <mail_server>);
props.put("mail.smtp.port", <port>);
props.put("mail.smtp.user", <user>);
props.put("mail.smtp.password", <pw>);
props.put("mail.smtp.auth", <logon>);
props.put("mail.from", <from>);
Session session = Session.getInstance(props, null);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, <email_to>);
msg.setRecipients(Message.RecipientType.CC, <email_cc>);
msg.setRecipients(Message.RecipientType.BCC, <email_bcc>);
msg.setSubject(x_subject, Globals.q_UNICODE_MAIL);
msg.setSentDate(new Date());
msg.setHeader("Disposition-Notification-To", x_from);
//
//<build your Multipart message>
//
3)最后,連接並發送:
Transport transport = session.getTransport("smtp");
if ( <logon_required> ) {
transport.connect(<mail_server>, <user>, <pw>);
}
transport.sendMessage(msg, msg.getAllRecipients());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.