简体   繁体   English

如何使用Gmail服务器使用java.mail从Web服务器发送电子邮件

[英]How to send email from Web Server using java.mail using Gmail server

I am trying to send email using gmail smtp using javax.mail. 我正在尝试使用javax.mail使用gmail smtp发送电子邮件。 following is my code 以下是我的代码

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

}  

Code is working fine When I am running it on my local server but when I am trying to run it on Elastic beanstalk (My Server is running on AWS EBS) then authentication fail exception is coming Note : I have turn ON access to less Secure app from Google A/c Setting but still I am getting this error 代码可以正常工作当我在本地服务器上运行它,但是尝试在Elastic beanstalk上运行它(我的服务器在AWS EBS上运行)时,身份验证失败异常即将到来注意:我可以打开对较少安全性应用程序的访问权限从Google A / c设置中获取,但仍然出现此错误

javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and then try again. javax.mail.AuthenticationFailedException:534-5.7.14请通过Web浏览器登录,然后重试。 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 l13sm3053341iti.6 - gsmtp 534-5.7.14进一步了解534 5.7.14 https://support.google.com/mail/answer/78754 l13sm3053341iti.6-gsmtp

Same problem I faced recently, I found problem is of EC2 region. 我最近遇到的同样问题,我发现问题出在EC2区域。 Google does not allow to login less Secure app from a non-frequent location of user. Google不允许从用户的非经常位置登录较少的Secure应用程序。 Either you use Google Mail APIs or use some other mail platforms like Yahoo. 您可以使用Google Mail API或使用其他一些邮件平台,例如Yahoo。 Check your EC2 instance region. 检查您的EC2实例区域。 try following code with yahoo mail, Deploy it on Elastic beanstalk or whatever environment you are using. 尝试使用yahoo邮件遵循以下代码,将其部署在Elastic beantalk或您使用的任何环境上。 This works for me. 这对我有用。

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

Please try with this 请尝试这个

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

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

相关问题 如何使用apache james电子邮件服务器向gmail发送邮件 - how to send mail using apache james email server to gmail 使用gmail smtp服务器和javamail在Java中发送未经身份验证的电子邮件 - Send Email Without authentication in java using gmail smtp server and javamail 使用Java Mail API将电子邮件从一台地址服务器发送到另一台服务器 - Send Email from one address server to another server using Java Mail API 如何使用Java从我们的邮件服务器发送电子邮件 - how to send email from our mail server in java 使用java.mail软件包的邮件内容中不能使用英镑符号? - pound sign is not working in mail content using java.mail package? 将邮件从本地计算机邮件服务器发送到gmail或Java中的其他邮件服务器 - Send mail from local machine mail server to gmail or other mail server in java 如何从java.mail获取HTML文本/纯文本 - How to get HTML text / plain text from java.mail 如何使用来自@ outlook.com的Java邮件发送电子邮件? - How to send an email using java mail from @outlook.com? 使用SSL连接通过Gmail SMTP服务器发送电子邮件通知 - Send an email notification via Gmail SMTP server using SSL connection 尝试发送电子邮件,出现错误,提示“此导入java.mail”无法解决 - Trying to send email, getting error saying “this import java.mail” cannot be resolved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM