我有同样的问题。尝试从浏览器登录到Gmail帐户。如果Gmail认为是非法访问(可能来自其他地理位置),则会阻止身份验证请求。您可能会看到一条消息或通知。按照通知或消息中的说明进行 *** 作,然后可以取消阻止身份验证请求。您的身份验证将起作用。
我使用了以下代码:
public void sendMail(String mailId,String outputFile) { final String username = "a@gmail.com"; final String password = "MyPassowrd"; 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"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username,password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("a@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailId)); message.setSubject("MySubject"); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("This is the mail content"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource source =new FileDataSource(outputFile); messageBodyPart.setDataHandler( new DataHandler(source)); messageBodyPart.setFileName("MyFile.pdf"); multipart.addBodyPart(messageBodyPart); // Put parts in message message.setContent(multipart); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)