【问题标题】:Java mail API is not sending emailsJava 邮件 API 不发送电子邮件
【发布时间】:2015-10-24 01:01:15
【问题描述】:

我有一个使用 Gmail SMTP 发送电子邮件的邮件服务类,它一直在成功运行,直到最近,同一个类无法发送电子邮件,我在调试时什至看不到任何错误消息或运行此代码。任何想法,发生了什么?

public class MailService {

public static void sendEmail(String subject, String msgBody, String[] toEmails, 
        String[] ccEmails, String[] bccEmails,
        String fromEmail, String toName){

     Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", true); // added this line
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.user", "mygmail-id");
        props.put("mail.smtp.password", "mypassword");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", true);



        Session session = Session.getInstance(props,null);
    List<InternetAddress> toAdresses = null;
    List<InternetAddress> ccAdresses = null;
    List<InternetAddress> bccAdresses = null;
    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(fromEmail));
        toAdresses = new ArrayList<InternetAddress>();
        ccAdresses = new ArrayList<InternetAddress>();
        bccAdresses = new ArrayList<InternetAddress>();
        for(String toEmail: toEmails){
            toAdresses.add(new InternetAddress(toEmail));
        }
        if(ccEmails != null && ccEmails.length > 0)
        for(String ccEmail: ccEmails){
            ccAdresses.add(new InternetAddress(ccEmail));
        }
        if(bccEmails != null && bccEmails.length > 0)
        for(String bccEmail: bccEmails){
            bccAdresses.add(new InternetAddress(bccEmail));
        }
        msg.addRecipients(Message.RecipientType.TO,
                toAdresses.toArray(new InternetAddress[toAdresses.size()]));
        msg.addRecipients(Message.RecipientType.CC,
                ccAdresses.toArray(new InternetAddress[ccAdresses.size()]));
        msg.addRecipients(Message.RecipientType.BCC,
                bccAdresses.toArray(new InternetAddress[bccAdresses.size()]));
        msg.setSubject(subject);
        msg.setContent(msgBody, "text/html");
        Transport.send(msg);

    } catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        // ...
    }
}

【问题讨论】:

  • 你看过thisthis吗?
  • 该代码永远不会工作。没有 mail.smtp.password 属性。您没有做任何事情来将用户名和密码传递给 Transport。使用允许您传递用户名和密码的连接方法之一,而不必费心将它们设置为属性。
  • @MadProgrammer,我已经尝试了所有方法,上面的一个是我尝试过的选项之一,但是这些都不起作用。

标签: java email gmail jakarta-mail


【解决方案1】:

两件事:

正如@Bill Shannon 建议的那样,您应该使用 Authenticator 而不是单独依赖 Properties:

Session session = Session.getInstance(props, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("my-gmail-id", "mypassword");
            }

        });

但即便如此 - 由于安全问题,Gmail 仍会阻止您发送电子邮件,并提供this link 以获取更多信息。

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 2016-11-30
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多