JavaMail API:不接受用户名和密码(Gmail) [英] JavaMail API : Username and Password not accepted (Gmail)

查看:256
本文介绍了JavaMail API:不接受用户名和密码(Gmail)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将电子邮件从我的Java应用程序发送到gmail帐户,我使用了以下代码和javaMail API,但是Gmail不接受用户名和密码以及抛出的异常 有人可以帮我解决这个问题吗?

I want to send email from my java application to gmail account I used the following code and javaMail API ,But Gmail not accepted username and password and exception thrown Can anybody help me how can I fix this issue?

MailService.java

public class MailService {

    String email;
    String content;
    public void sendMail(String email,String content)
    {
        this.email=email;
        this.content=content;
        // Recipient's email ID needs to be mentioned.
          String to = email;

          // Sender's email ID needs to be mentioned
          String from = senderemail@gmail.com;
          final String username = "myusername";//change accordingly
          final String password = "*******";//change accordingly

          // Assuming you are sending email through relay.jangosmtp.net
          String host = "smtp.gmail.com";

          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", "587");

          // Get the Session object.
          Session session = Session.getInstance(props,
             new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                   return new PasswordAuthentication(username, password);
           }
             });

          try {
           // Create a default MimeMessage object.
           Message message = new MimeMessage(session);

           // Set From: header field of the header.
           message.setFrom(new InternetAddress(from));

           // Set To: header field of the header.
           message.setRecipients(Message.RecipientType.TO,
                   InternetAddress.parse(to));

           // Set Subject: header field
           message.setSubject("Did you get my message?");

           // Now set the actual message
           message.setText(content);

           // Send message
           Transport.send(message);

           System.out.println("Sent message successfully....");

          } catch (MessagingException e) {
             throw new RuntimeException(e);
          }
       }
    }

推荐答案

要能够通过您的gmail帐户发送邮件,您应该在Google帐户安全设置中允许使用不安全的应用程序(从gmail的角度来看,这是您的应用程序).

To be able send mail messages via your gmail account, you should allow unsecure applications (which your application is by gmail's point of view) in google account security settings.

UPD: 另外,如果您想查看调试消息,请使用下一个java mail属性:

UPD: Also, if you want to see debug messages, use next java mail property:

props.put("mail.debug", "true");

它可以帮助您了解幕后发生的事情.

It can help you to find out what happening behind the scenes.

这篇关于JavaMail API:不接受用户名和密码(Gmail)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆