【问题标题】:Java connect to gmail and send fileJava连接到gmail并发送文件
【发布时间】:2018-08-14 04:49:20
【问题描述】:

代码:

public static void sendMailAttachments(String mensaje, String cartera, String asunto, String file) throws AddressException, MessagingException, IOException{

    //Get user, pass, to
    getDatosCorreo();

    Properties props = new Properties();
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user,pass);
                }
            });
    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));

        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to.get(0)));

        message.setSubject("Email Subject - Asunto del correo electronico");

        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText("Email text Body - Texto o cuerpo del correo electronico");

        Multipart multipart = new MimeMultipart();
        multipart = addAttachment(multipart, file);

        //Setting email text message
        multipart.addBodyPart(messageBodyPart);

        //set the attachments to the email
        message.setContent(multipart);

        Transport.send(message);

        System.out.println("Correo enviado");

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

}

例外:

GRAVE:无法将 RuntimeException 映射到响应, 重新抛出到 HTTP 容器 java.lang.RuntimeException: javax.mail.MessagingException:无法连接到 SMTP 主机: 本地主机,端口:465,响应:-1 at util.MAIL.sendMailAttachments(MAIL.java:128)

如何连接和发送文件?

此代码连接到 Gmail 并发送消息。

我需要添加一个文件才能发送。

getDatosCorreo();

// TODO Auto-generated method stub
// Step1
System.out.println("\n 1st ===> setup Mail Server Properties..");
mailServerProperties = System.getProperties();
mailServerProperties.put("mail.smtp.port", "587");
mailServerProperties.put("mail.smtp.auth", "true");
mailServerProperties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
mailServerProperties.put("mail.smtp.starttls.enable", "true");
System.out.println("Mail Server Properties have been setup successfully..");

// Step2
System.out.println("\n\n 2nd ===> get Mail Session..");
getMailSession = Session.getDefaultInstance(mailServerProperties, null);
generateMailMessage = new MimeMessage(getMailSession);
for(int i = 0; i < to.size(); i++) {
    if(i == 0) {
        generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to.get(i)));
    }else {
        generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(to.get(i)));
    }
}
generateMailMessage.setSubject(asunto+cartera);
generateMailMessage.setContent(mensaje, "text/html");
System.out.println("Mail Session has been created successfully..");

// Step3
System.out.println("\n\n 3rd ===> Get Session and Send mail");
Transport transport = getMailSession.getTransport("smtp");

// Enter your correct gmail UserID and Password
// if you have 2FA enabled then provide App Specific Password
transport.connect("smtp.gmail.com", user, pass);
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
System.out.println("\n\n 4rd ===> Send email correctly");

【问题讨论】:

  • 也许你也应该设置"mail.smtp.host"属性的正确值,它看起来默认是localhost

标签: java email gmail attachment


【解决方案1】:

以下设置在使用 SSL 的电子邮件服务时适用于我:

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.starttls.enable", "true");
props.put("mail.smtp.port", "465");

不知道是否需要设置mail.smtp.ssl.trust属性。 This SO answer 不需要它,我也没有让它与我的特定 SSL 电子邮件服务一起使用。

【讨论】:

    猜你喜欢
    • 2020-07-02
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 2016-09-03
    相关资源
    最近更新 更多