简体   繁体   English

从apache tomcat发送邮件

[英]sending mail from apache tomcat

I am working on a web project using Tomcat 6 as my webserver and JSP as frontend. 我正在开发一个Web项目,使用Tomcat 6作为我的webserver,JSP作为前端。 I want to send a mail from the web server to an email account. 我想从Web服务器向电子邮件帐户发送邮件。 How can I achieve this? 我怎样才能做到这一点?

To start, here is my form in JSP: 首先,这是我在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>

Using Java-Mail API. 使用Java-Mail API。

  • Take required inputs from JSP, 从JSP获取所需的输入,
  • Post it to Servlet 将其发布到Servlet
  • Invoke service method to send Mail from Servlet 调用服务方法从Servlet发送Mail
  • Use Java Mail API to send mails from service method, a quick example 使用Java Mail API从服务方法发送邮件,这是一个快速示例

This works fine (gmail example) : 这工作正常(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("fromSomeone@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("toSomeone@gmail.com"));
            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);
        }
    }
}

This is simplest way, using MailToURLConnection . 这是最简单的方法,使用MailToURLConnection No additional libraries needed. 无需其他库。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM