java - 在 jsp 中使用 JavaMailApi

标签 java jsp jakarta-mail

我在这个注册页面上工作,我需要发送到邮件的验证链接,同时执行得到错误并说java.net.ConnectException:连接被拒绝:连接 所以请帮助我谢谢

注册.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>REGISTRATION</title>
</head>
<body>
<%!
    DBCreation creation;
    Connection connection;
    %>
    <%
    String uid=request.getParameter("userid");
    String pwd=request.getParameter("pwd");
    String fName=request.getParameter("fname");
    String lName=request.getParameter("lname");
    String email=request.getParameter("email");
    String location=request.getParameter("location");
    String encpwd=encryptPwd(pwd);
    System.out.println(encpwd);
    connection=DBCreation.getConnection();
     String result;
       // Recipient's email ID needs to be mentioned.
       String toEmail = email;

       // Sender's email ID needs to be mentioned
       String fromEmail = "nbarath2008@gmail.com";

       // Assuming you are sending email from localhost
       //String host = "localhost";

       // Get system properties object
       Properties properties = System.getProperties();

       // Setup mail server
      // properties.setProperty("mail.smtp.host", host);


       // Get the default Session object.
       Session mailSession = Session.getDefaultInstance(properties);
       Transport transport = new SMTPTransport(mailSession,new URLName("localhost"));
       transport.connect("localhost",25,null,null);
       try{
              // Create a default MimeMessage object.
              MimeMessage message = new MimeMessage(mailSession);
              // Set From: header field of the header.
              message.setFrom(new InternetAddress(fromEmail));
              // Set To: header field of the header.
               message.setRecipients(Message.RecipientType.TO,""+email);

              // Set Subject: header field
              message.setSubject("This is the Subject Line!");

              // Send the actual HTML message, as big as you like
              message.setContent("<h1>This is actual message</h1>",
                                    "text/html" );
              // Send message
              Transport.send(message);
              result = "Sent message successfully....";


        PreparedStatement statement=connection.prepareStatement("insert into userregistration values(?,?,?,?,?,?,?)");
        statement.setString(1,uid);
        statement.setString(2,encpwd);
        statement.setString(3,fName);
        statement.setString(4, lName);
        statement.setString(5, location);
        statement.setString(6, fromEmail);
        statement.setString(7, toEmail);
        int i=statement.executeUpdate();
        if(i>0)
        {
             // Send message
              Transport.send(message);
              result = "Sent message successfully....";
            RequestDispatcher rd=request.getRequestDispatcher("regsuccess.jsp");
            rd.forward(request, response);

        }
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    %>
</body>
</html>

最佳答案

这是工作代码。有疑问请追问!

导入java.util.; 导入javax.mail。; 导入 javax.mail.internet.*;

public class Main {

    private static String USER_NAME = "gmail-user-name";  // GMail user name (just the part before "@gmail.com")
    private static String PASSWORD = "******"; // Enter your GMail password
    private static String RECIPIENT = "RECIPIENT EMAIL address";

    public static void main(String[] args) {
        String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT }; // list of recipient email addresses
        String subject = "Java send mail example";
        String body = "Welcome to JavaMail!";

        sendFromGMail(from, pass, to, subject, body);
    }

    private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }
    }
}

关于java - 在 jsp 中使用 JavaMailApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22217914/

相关文章:

java.sql.SQLException : [Oracle][ODBC][Ora]ORA-01008: not all variables bound

java - JSP无法删除FILE

Tomcat 内部的 javamail 附加 "noname"

java - Javax.mail.message 的 Java EE 7 编码错误

java - 如何将邮件发送给多个收件人

java - JOptionPane 给出错误

java - 检查一个树集值包含到另一个树集中

java - 不能在 final 中公开静态

java - 如何在FXML Controller 中填充javafx.TableView?

java - 如何检索多个选择值?