Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java/servlet中发送邮件时出错_Java_Jsp_Servlets_Jakarta Mail - Fatal编程技术网

在java/servlet中发送邮件时出错

在java/servlet中发送邮件时出错,java,jsp,servlets,jakarta-mail,Java,Jsp,Servlets,Jakarta Mail,我正在尝试使用java发送电子邮件。但我面临如下错误 javax.mail.AuthenticationFailedException:连接失败,未指定密码 当我通过正确的电子邮件id和密码进行身份验证时,为什么会出现此错误 这是我的密码 import java.io.IOException; import java.net.PasswordAuthentication; import java.util.Properties; import javax.mail.Authenticator;

我正在尝试使用
java
发送
电子邮件。但我面临如下错误

javax.mail.AuthenticationFailedException:连接失败,未指定密码

当我通过正确的电子邮件id和密码进行身份验证时,为什么会出现此错误

这是我的密码

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验证器位于javax.mail包中,与同名的java.net类不同。这两者不共享同一个验证器,因为JavaMail API使用的是Java 1.1,而Java 1.1没有Java.net变体

请参阅:

JavaMail验证器位于javax.mail包中,与同名的java.net类不同。这两者不共享同一个验证器,因为JavaMail API使用的是Java 1.1,而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>
  //
Properties=newproperties();
props.put(“mail.smtp.host”);
props.put(“mail.smtp.port”,);
props.put(“mail.smtp.user”,);
props.put(“mail.smtp.password”,);
put(“mail.smtp.auth”,);
道具放置(“mail.from”,);
Session Session=Session.getInstance(props,null);
试一试{
MimeMessage msg=新MimeMessage(会话);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,);
msg.setRecipients(Message.RecipientType.CC,);
msg.setRecipients(Message.RecipientType.BCC,);
msg.setSubject(x_subject,Globals.q_UNICODE_MAIL);
msg.setSentDate(新日期());
msg.setHeader(“处置通知到”,x_from);
//
//
//
3) 最后,连接并发送:

        Transport transport = session.getTransport("smtp");
        if ( <logon_required> ) {
             transport.connect(<mail_server>, <user>, <pw>);
        }
        transport.sendMessage(msg, msg.getAllRecipients());
Transport-Transport=session.getTransport(“smtp”);
如果(){
交通。连接(,);
}
transport.sendMessage(msg,msg.getAllRecipients());
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>
  //
Properties=newproperties();
props.put(“mail.smtp.host”);
props.put(“mail.smtp.port”,);
props.put(“mail.smtp.user”,);
props.put(“mail.smtp.password”,);
put(“mail.smtp.auth”,);
道具放置(“mail.from”,);
Session Session=Session.getInstance(props,null);
试一试{
MimeMessage msg=新MimeMessage(会话);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,);
msg.setRecipients(Message.RecipientType.CC,);
msg.setRecipients(Message.RecipientType.BCC,);
msg.setSubject(x_subject,Globals.q_UNICODE_MAIL);
msg.setSentDate(新日期());
msg.setHeader(“处置通知到”,x_from);
//
//
//
3) 最后,连接并发送:

        Transport transport = session.getTransport("smtp");
        if ( <logon_required> ) {
             transport.connect(<mail_server>, <user>, <pw>);
        }
        transport.sendMessage(msg, msg.getAllRecipients());
Transport-Transport=session.getTransport(“smtp”);
如果(){
交通。连接(,);
}
transport.sendMessage(msg,msg.getAllRecipients());

这是一个常见的错误,如.ok现在工作谢谢@Bill Shannon这是一个常见的错误,如.ok现在工作谢谢@Bill Shannon中所述