【问题标题】:How to send email from Web Server using java.mail using Gmail server如何使用 Gmail 服务器使用 java.mail 从 Web 服务器发送电子邮件
【发布时间】:2019-09-29 01:38:58
【问题描述】:

我正在尝试使用 javax.mail 使用 gmail smtp 发送电子邮件。以下是我的代码

 public static void send(String from,String password,String to,String sub,String msg){  
      //Get properties object    
      Properties props = new Properties();    
      props.put("mail.smtp.host", "smtp.gmail.com");    
      props.put("mail.smtp.socketFactory.port", "465");    
      props.put("mail.smtp.socketFactory.class",    
                "javax.net.ssl.SSLSocketFactory");    
      props.put("mail.smtp.auth", "true");    
      props.put("mail.smtp.port", "465");    
      //get Session   
      Session session = Session.getDefaultInstance(props,    
       new javax.mail.Authenticator() {    
       protected PasswordAuthentication getPasswordAuthentication() {    
       return new PasswordAuthentication(from,password);  
       }    
      });    
      //compose message    
      try {    
       MimeMessage message = new MimeMessage(session);    
       message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));    
       message.setSubject(sub);    
       message.setText(msg);    
       //send message  
       Transport.send(message);    
       System.out.println("message sent successfully");    
      } catch (MessagingException e) {throw new RuntimeException(e);}    

}  

代码运行良好当我在本地服务器上运行它但是当我尝试在 Elastic beanstalk 上运行它(我的服务器在 AWS EBS 上运行)时,身份验证失败异常即将到来 注意:我已经从 Google A/c Setting 开启了对不太安全的应用程序的访问,但我仍然收到此错误

javax.mail.AuthenticationFailedException: 534-5.7.14 请通过您的网络浏览器登录,然后重试。 534-5.7.14 了解更多信息 534 5.7.14 https://support.google.com/mail/answer/78754l13sm3053341iti.6 - gsmtp

【问题讨论】:

    标签: java gmail gmail-api amazon-elastic-beanstalk


    【解决方案1】:

    我最近遇到了同样的问题,我发现问题是 EC2 区域的。谷歌不允许从用户的非经常位置登录不太安全的应用程序。您要么使用 Google Mail API,要么使用其他一些邮件平台,例如 Yahoo。检查您的 EC2 实例区域。尝试使用 yahoo 邮件跟踪代码,将其部署在 Elastic beanstalk 或您使用的任何环境中。这对我有用。

    public void yahooSend(String mail,String subject,String msg) {
    
            // Sender's email ID needs to be mentioned
             String from = "YOUR_YAHOO_MAIL";
             String pass ="YOUR_YAHOO_PASSWORD";
            // Recipient's email ID needs to be mentioned.
           String to = mail;
           String host = "smtp.mail.yahoo.com";
    
           // Get system properties
           Properties properties = System.getProperties();
           // Setup mail server
           properties.put("mail.smtp.starttls.enable", "true");
           properties.put("mail.smtp.host", host);
           properties.put("mail.smtp.user", from);
           properties.put("mail.smtp.password", pass);
          // props.put("mail.smtp.user", "YAHOO_USER_NAME"); 
           properties.put("mail.smtp.port", "587");
           properties.put("mail.smtp.auth", "true");
    
    
           // Get the default Session object.
           Session session = Session.getDefaultInstance(properties);
    
           try{
              // Create a default MimeMessage object.
              MimeMessage message = new MimeMessage(session);
    
              // Set From: header field of the header.
              message.setFrom(new InternetAddress(from));
    
              // Set To: header field of the header.
              message.addRecipient(Message.RecipientType.TO,
                                       new InternetAddress(to));
    
              // Set Subject: header field
              message.setSubject(subject);
    
              // Now set the actual message
              message.setText(msg);
              System.out.print("Sending msg "+msg);
              // Send message
              Transport transport = session.getTransport("smtp");
              transport.connect(host,587, from, pass);
              transport.sendMessage(message, message.getAllRecipients());
              transport.close();
             System. out.println("Sent message successfully....");
           }catch (MessagingException mex) {
               System. out.print(mex);
              mex.printStackTrace();
           }
     }
    

    【讨论】:

      【解决方案2】:

      请试试这个

      public static void sendPDFReportByGMail(String from, String pass, String to, String subject, String body) {
          Properties props = System.getProperties();
          String host = "smtp.gmail.com";
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.user", from);
          props.put("mail.smtp.password", pass);
          props.put("mail.smtp.port", "587");
          props.put("mail.smtp.auth", "true");
      
          Session session = Session.getInstance(props, new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(from, pass);
              }
          });
      
          MimeMessage message = new MimeMessage(session);
      
          try {
              // Set from address
              message.setFrom(new InternetAddress(from));
              message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
              // Set subject
              message.setSubject(subject);
              // Set Mail body
              message.setText(body);
      
              BodyPart objMessageBodyPart = new MimeBodyPart();
      
              objMessageBodyPart.setText(body);
      
              Transport transport = session.getTransport("smtp");
              transport.connect(host, from, pass);
              transport.sendMessage(message, message.getAllRecipients());
              transport.close();
          } catch (AddressException ae) {
              ae.printStackTrace();
          } catch (MessagingException me) {
              me.printStackTrace();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-09-07
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 2012-10-20
        • 2010-10-17
        相关资源
        最近更新 更多