簡體   English   中英

無法使用gmail的smtp服務器從jsp發送電子郵件

[英]not able to send email from jsp using gmail's smtp server

我是jsp的新手,我正在嘗試從jsp頁面發送電子郵件,但是它沒有任何stacktrace就會失敗。 這是我嘗試的代碼,

 <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <%@ page import="javax.mail.internet.*,javax.activation.*"%> <%@ page import="java.io.*,java.util.*,javax.mail.*"%> <% //username for abc@gmail.com will be "abc" String username = "abc"; String password = "password"; String result = null; try { Properties props = System.getProperties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.gmail.com"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); props.put("mail.debug", "true"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); Session emailSession = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("sender_username","sender_password"); } }); emailSession.setDebug(true); Message message = new MimeMessage(emailSession); message.setFrom(new InternetAddress( "sender_username@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("test123@gmail.com")); message.setSubject("Test mail from Java"); message.setText("Hello. this is a test"); Transport transport = emailSession.getTransport("smtps"); transport.connect("smtp.gmail.com", username, password); transport.sendMessage(message, message.getAllRecipients()); result = "Successfully sent email"; } catch (MessagingException e) { result = "Unable to send email"; e.printStackTrace(); } %> <html> <head> <title>Send Email using JSP</title> </head> <body> <center> <h1>Send Email using JSP</h1> </center> <p align="center"> <% out.println("Result: " + result + "\\n"); %> </p> </body> </html> 

我從博客/教程頁面上獲取了它。 由於它沒有打印堆棧跟蹤,因此我也看不到它在哪里出現故障。 失敗,顯示為“結果:無法發送電子郵件”

希望這項工作對您有用。

     final String username = "chandrasekar@gmail.com";//from which you want to send mail
    final String password = "chandra@123";//password off your mail id 

    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("chandrasekar@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("etc@gmail.com"));//whom you want to send mail
        message.setSubject("Testing Subject");
        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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM