Sending email using JSP

后端 未结 3 1525
囚心锁ツ
囚心锁ツ 2020-12-02 02:13

This problem is driving me nuts. I have the following code:



<%@ page import=\"java.util.*\" %>
<%@ page import=\"javax.mai         


        
3条回答
  •  攒了一身酷
    2020-12-02 03:09

          <%
          String host = "smtp.gmail.com";
          String from="send-from@gmail.com";//Your E-mail-Id
          String pass="xxxxxx";      //your e-mail account password
    
    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    String to = "send-to@gmail.com"; //recipient E-mail-Id
    String from = "send-from@gmail.com"; // Your E-mail Id
    String subject ="test mail";
    String messageText = "hello abc";
    boolean sessionDebug = true;
    
    
    Properties props = System.getProperties();
    props.put("mail.host", host);
    props.put("mail.transport.protocol.", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.", "true");
    props.put("mail.smtp.port", "465");//port number 465 for Secure (SSL) and we can also            use port no 587 for Secure (TLS)
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
    
    
    Session mailSession = Session.getDefaultInstance(props, null);
    mailSession.setDebug(sessionDebug);
    
    
    Message msg = new MimeMessage(mailSession);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address = {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setContent(messageText, "text/html");
    
    
    Transport transport = mailSession.getTransport("smtp");
    transport.connect(host, user, pass);
    
    
     try {
          transport.sendMessage(msg, msg.getAllRecipients());
          out.println("Send Success");
          boolean WasEmailSent = true; // assume it was sent
         }
    
    catch (Exception err) {
                           boolean WasEmailSent = false; 
                          }
                      transport.close();
          %>
    

提交回复
热议问题