How to send an email from jsp/servlet?

前端 未结 5 807
日久生厌
日久生厌 2020-11-29 05:29

How to send an email from JSP/servlet? Is it necessary to download some jars or can you send an email from JSP/servlets without any jars?

  • What would my Java

5条回答
  •  攒了一身酷
    2020-11-29 06:16

    JSP page:

    To Email-id :

    Here's the Servlet code:

    String uri=req.getRequestURI();
    
    if(uri.equals("/mail.do"))
            {
                SendEmail sa=new SendEmail();
                            String to_mail=request.getParameter("email");
                            String body="
    Hi this is Test mail
    "; sa.SendingEmail(to_email,body); }

    And the SendEmail class:

     package Email;
    
    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class SendEmail {
    
        public void SendingEmail(String Email,String Body) throws AddressException, MessagingException
        {
    
                 String host ="smtp.gmail.com";
                 String from ="yourMailId";  //Your mail id
                 String pass ="yourPassword";   // Your Password
                 Properties props = System.getProperties();
                 props.put("mail.smtp.starttls.enable", "true"); // added this line
                 props.put("mail.smtp.host", host);
                 props.put("mail.smtp.user", from);
                 props.put("mail.smtp.password", pass);
                 props.put("mail.smtp.port", "25");
                 props.put("mail.smtp.auth", "true");
                 String[] to = {Email}; // To Email address
                 Session session = Session.getDefaultInstance(props, null);
                 MimeMessage message = new MimeMessage(session);
                 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++ )
                  { // changed from a while loop
                      toAddress[i] = new InternetAddress(to[i]);
                  }
                 System.out.println(Message.RecipientType.TO);
                 for( int j=0; j < toAddress.length; j++)
                 { // changed from a while loop
                 message.addRecipient(Message.RecipientType.TO, toAddress[j]);
                 }
                 message.setSubject("Email from SciArchives");
    
                 message.setContent(Body,"text/html");
                 Transport transport = session.getTransport("smtp");
                 transport.connect(host, from, pass);
                 transport.sendMessage(message, message.getAllRecipients());
                     transport.close();
            }
        }
    

提交回复
热议问题