2016-02-21 7 views
1

После многочисленных исследований я не могу добиться успеха, чтобы отправить почту с ЯвойJava: Отправка неудачен

Properties props = new Properties(); 

props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.starttls.required", "true"); 
props.put("mail.transport.protocol","smtp"); 
props.put("mail.smtp.host", "smtp.live.com"); 
props.put("mail.smtp.port", "587"); 

Session session = Session.getDefaultInstance(props, null); 
Message msg = new MimeMessage(session); 
msg.setFrom(new InternetAddress("[email protected]")); 
InternetAddress toAddress = new InternetAddress("[email protected]"); 

msg.addRecipient(Message.RecipientType.TO, toAddress); 
msg.setSubject("sujet du mail de text"); 
msg.setText("aaa"); 

Transport transport = session.getTransport("smtps"); 
transport.connect("smtp.gmail.com", 587,"[email protected]", "mypasswordcached"); 
transport.send(msg); 

У меня есть исключение в потоке «основной» javax.mail.NoSuchProviderException: Нет провайдер для SMTPS Я не» t понять почему Кто-нибудь может мне помочь?

+0

Это выглядит примерно http://stackoverflow.com/questions/4457731/error-in-sending-mail-through- smtps надеются, что это поможет. – Steve

ответ

0

Попробуйте следующий код:

package org.kodejava.example.mail; 

import javax.mail.*; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import java.util.Date; 
import java.util.Properties; 

public class GmailSendEmailTLS { 
    public static final String USERNAME = "username"; 
    public static final String PASSWORD = "password"; 

    public static void main(String[] args) throws Exception { 
     // 
     // Email information such as from, to, subject and contents. 
     // 
     String mailFrom = "[email protected]"; 
     String mailTo = "[email protected]"; 
     String mailSubject = "TLS - Gmail Send Email Demo"; 
     String mailText = "TLS - Gmail Send Email Demo"; 

     GmailSendEmailTLS gmail = new GmailSendEmailTLS(); 
     gmail.sendMail(mailFrom, mailTo, mailSubject, mailText); 
    } 

    private void sendMail(String mailFrom, String mailTo, 
          String mailSubject, String mailText) 
      throws Exception { 

     Properties config = createConfiguration(); 

     // 
     // Creates a mail session. We need to supply username and 
     // password for Gmail authentication. 
     // 
     Session session = Session.getInstance(config, new Authenticator() { 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(
         GmailSendEmailTLS.USERNAME, 
         GmailSendEmailTLS.PASSWORD 
       ); 
      } 
     }); 

     // 
     // Creates email message 
     // 
     Message message = new MimeMessage(session); 
     message.setSentDate(new Date()); 
     message.setFrom(new InternetAddress(mailFrom)); 
     message.setRecipient(Message.RecipientType.TO, 
       new InternetAddress(mailTo)); 
     message.setSubject(mailSubject); 
     message.setText(mailText); 

     // 
     // Send a message 
     // 
     Transport.send(message); 
    } 

    private Properties createConfiguration() { 
     return new Properties() {{ 
      put("mail.smtp.auth", "true"); 
      put("mail.smtp.host", "smtp.gmail.com"); 
      put("mail.smtp.port", "587"); 
      put("mail.smtp.starttls.enable", "true"); 
     }}; 
    } 
} 

Этот пример взят из: Sending email using gmail via TLS

Смежные вопросы