2011-11-01 33 views
6

Sto lavorando a un progetto Web utilizzando Tomcat 6 come server Web e JSP come frontend. Voglio inviare una mail dal server web a un account di posta elettronica. Come posso raggiungere questo obiettivo?invio di posta da apache tomcat

Per iniziare, ecco la mia forma in JSP:

<form name="forgotpassword" onsubmit="return valid()">  
    <table> 
    <tr> 
     <td>Enter Employee ID</td> 
     <td><input type="text" name="emp_id" size="50"/></td> 
    </tr> 
    <tr> 
     <td>Enter Your Email Address</td> 
     <td><input type="text" name="mailid" size="50"/></td> 
    </tr> 
    <tr> 
     <td><input type="submit"style="margin-left:100px" name="forgot" value="SUBMIT">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" name="cancel" value="RESET"/></td> 
    </tr> 
    </table> 
</form> 
+0

Si chiama "application server", non "web server" e soprattutto non "mail server" :-) – Lachezar

+0

@Lucho: Tomcat non è un server di applicazioni. È un semplice contenitore servlet. – BalusC

+0

http://www.tomcatexpert.com/blog/2010/06/09/tomcat-application-server – Lachezar

risposta

13

Utilizzando Java-Mail API.

  • Prendere input richiesti da JSP,
  • Post it per servlet metodo di servizio
  • Invoke per inviare Mail da Servlet
  • Utilizzare Java Mail API per inviare messaggi di posta elettronica f rom service method, a quick example
+0

Inoltre, è possibile trovare maggiori informazioni in [here] (http://cs-sharenotes.blogspot.ca/2016 /02/how-connect-gmail-javamail-api.html) –

1

Questo è il modo più semplice, utilizzando . Non sono necessarie librerie aggiuntive.

public static void sendMail(String from, String to, String subject, String body, String[] headers) throws IOException { 
    System.setProperty("mail.host", "localhost"); 

    URL u = new URL("mailto:"+to); 
    MailToURLConnection con = (MailToURLConnection)u.openConnection(); 
    OutputStream os = con.getOutputStream(); 
    OutputStreamWriter w = new OutputStreamWriter(os); 

    DateFormat df = new SimpleDateFormat("E, d MMM yyyy H:mm:ss Z"); 
    Date d = new Date(); 
    String dt = df.format(d); 
    String mid = d.getTime()+from.substring(from.indexOf('@')); 

    w.append("Subject: "+subject+"\r\n"); 
    w.append("Date: " +dt+ "\r\n"); 
    w.append("Message-ID: <"+mid+ ">\r\n"); 
    w.append("From: "+from+"\r\n"); 
    w.append("To: <"+to+">\r\n"); 
    if(headers!=null) { 
     for(String h: headers) 
     w.append(h).append("\r\n"); 
    } 
    w.append("\r\n"); 

    w.append(body.replace("\n", "\r\n")); 
    w.flush(); 
    w.close(); 
    os.close(); 
    con.close(); 
} 
3

Questo funziona bene (ad esempio Gmail):

import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendMail { 

    public static void main(String[] args) { 

     final String username = "your_usename_goes_here"; 
     final String password = "your_password_goes_here"; 

     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.port", "587"); 

     Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
      }); 

     try { 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
      message.setSubject("A testing mail header !!!"); 
      message.setText("Dear Mail Crawler," 
       + "\n\n No spam to my email, please!"); 

      Transport.send(message); 

      System.out.println("Done"); 

     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 
+0

Ha funzionato alla grande! Utilizzato un server SMTP MS O365, ha verificato che l'indirizzo di invio fosse un alias o un utente di posta elettronica su O365 e funzionava subito. Eccezionale! –