2011-11-01 34 views
6

我正在使用Tomcat 6作为我的webserver和JSP作为前端的web项目。我想从网络服务器发送邮件到一个电子邮件帐户。我怎样才能做到这一点?从apache tomcat发送邮件

要启动,这是我在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

这就是所谓的 “应用程序服务器”,而不是 “网络服务器”,最重要不是 “邮件服务器” :-) – Lachezar

+0

@Lucho的: Tomcat不是应用程序服务器。这是一个简单的servlet容器。 – BalusC

+0

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

回答

13

使用Java-Mail API.

  • 从JSP采取必要的投入,
  • 张贴到Servlet的
  • 调用服务的方法来发送来自Servlet的邮件
  • 使用Java Mail API发送邮件f ROM服务方法,a quick example
+0

此外,您可能会在[这里]找到更多信息(http://cs-sharenotes.blogspot.ca/2016 /02/how-connect-gmail-javamail-api.html) –

1

这是最简单的方法,使用MailToURLConnection。 不需要额外的库。

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

这工作正常(例如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

工作了不起! 使用MS O365 SMTP服务器,确保发送地址是O365上的别名或电子邮件用户,并且它立即工作。真棒! –