【问题标题】:sending mail from apache tomcat从 apache tomcat 发送邮件
【发布时间】:2011-12-19 17:27:06
【问题描述】:

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

首先,这是我在 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>

【问题讨论】:

标签: java jsp tomcat servlets jakarta-mail


【解决方案1】:

使用Java-Mail API.

  • 从 JSP 获取所需的输入,
  • 发布到 Servlet
  • 调用服务方法从Servlet发送邮件
  • 使用Java Mail API从服务方法发送邮件,a quick example

【讨论】:

  • 另外,您可以在here找到更多信息
【解决方案2】:

这是最简单的方法,使用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("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);
            }
        }
    }
    

    【讨论】:

    • 工作得很好!使用 MS O365 SMTP 服务器,确保发送地址是 O365 上的别名或电子邮件用户,并且它立即工作。太棒了!
    猜你喜欢
    • 1970-01-01
    • 2022-07-24
    • 2011-10-09
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    相关资源
    最近更新 更多