2015-05-27 7 views
5
kullanarak posta göndermeye çalışırken

Stackoverflow ve diğer çeşitli sitelerde verilen tüm çözümleri okudum ve denedim, ancak yine de sorun alıp istisna elde ediyorum.javax.mail.NoSuchProviderException java

Kodu:

import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class sendmail { 

Properties emailProperties; 
Session mailSession; 
MimeMessage emailMessage; 

public static void main(String args[]) throws AddressException, 
     MessagingException { 

    sendmail javaEmail = new sendmail(); 

    javaEmail.setMailServerProperties(); 
    javaEmail.createEmailMessage(); 
    javaEmail.sendEmail(); 
} 

public void setMailServerProperties() { 

    String emailPort = "587";//gmail's smtp port 

    emailProperties = System.getProperties(); 
    emailProperties.put("mail.smtp.port", emailPort); 
    emailProperties.put("mail.smtp.auth", "true"); 
    emailProperties.put("mail.smtp.starttls.enable", "true"); 

} 

public void createEmailMessage() throws AddressException, 
     MessagingException { 
    String[] toEmails = { "[email protected]" }; 
    String emailSubject = "Java Email"; 
    String emailBody = "This is an email sent by JavaMail api."; 

    mailSession = Session.getDefaultInstance(emailProperties, null); 
    emailMessage = new MimeMessage(mailSession); 

    for (int i = 0; i < toEmails.length; i++) { 
     emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i])); 
    } 

    emailMessage.setSubject(emailSubject); 
    emailMessage.setContent(emailBody, "text/html");//for a html email 
    //emailMessage.setText(emailBody);// for a text email 

} 

public void sendEmail() throws AddressException, MessagingException { 

    String emailHost = "smtp.gmail.com"; 
    String fromUser = "emailid";//just the id alone without @gmail.com 
    String fromUserEmailPassword = "test"; 

    Transport transport = mailSession.getTransport("smtp"); 

    transport.connect(emailHost, fromUser, fromUserEmailPassword); 
    transport.sendMessage(emailMessage, emailMessage.getAllRecipients()); 
    transport.close(); 
    System.out.println("Email sent successfully."); 
} 

} 

Kodunu ayıklama, bu hat durmasına: taşıma taşıma = mailSession.getTransport ("SMTP");

Eklediğim aşağıdaki kavanozları:

Mail.jar, pop3.jar, smtp-1.4.2.jar, activation.jar, additional.jar

Tam istisna:

dizisindeki

"ana" javax.mail.NoSuchProviderException: smtp javax.mail.Session.getService de (Session.java:764) javax de javax.mail de javax.mail.Session.getTransport (Session.java:612) de javax.mail.Session.getTransport (Session.java:632) de .mail.Session.getTransport (Session.java:689) .Session.getTransport (Session.java:667) javax.mail.Transport.send0 adresinde (Transport.java:154) javax.mail.Transport.send adresinden (Transport.java:80) adresinden JannyaPaid_Device.sendmail.sendEmail JannyaPaid_Device.sendmail.main (sendmail.java:26) 'den (sendmail.java:68)

Ayrıca ben posta göndermek için bu şeyleri önleyebilir o firewall sormak istiyorum? Bazı güvenlik duvarı kurulu olduğu için, ancak posta thrugh gmail'i manuel olarak açıp gönderebiliyorum. yanı Eposta sunucu tarafında bir STARTTLS komutu (taşıma katmanı güvenliği start), sen (JDK/JRE) yolunda bir sertifika gerekebilir ve aynı sorunu olmalı - For

+0

'Transport.send (emailMessage)' posta göndermek için neden bu şekilde göndermek yeterlidir? – Babel

+0

Ama eğer hoşuma giderse 'Transport transport = null; \t Transporter (emailMessage); –

+0

Kullanılacak protokolü ayarlamadınız Şu anki adresi ayarlamak istiyorum "emailProperties.put (" mail.transport.protocol "," smtp "); – Babel

cevap

5
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendMailTLS { 

public static void main(String[] args) { 

    final String username = "[email protected]"; 
    final String password = "password"; 

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

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

    try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("[email protected]")); 
     message.setSubject("Testing Subject"); 
     message.setText("Dear Mail Crawler," 
      + "\n\n No spam to my email, please!"); 

     Transport.send(message); 

     System.out.println("Done"); 

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

}

+0

Aynı istisnayı alma :( –

+0

Güncel kaynak kodu ... Benim için çalışıyor –

+0

Şimdi "thread özel durum" main "java.lang.RuntimeException: javax.mail.SendFailedException gibi yeni bir hata alıyor: Gönderme başarısız oldu; iç içe istisna: \t sınıf javax.mail.MessagingException: 530 5.7.0 Önce bir STARTTLS komutu vermelisiniz.cc14sm16722533pac.6 - gsmtp \t, sendmail.main (sendmail.java:45) Nedeni: javax.mail.SendFailedException: Gönderme başarısız oldu; iç içe istisna: \t sınıf javax.mail.MessagingException: 530 5.7.0 Önce bir STARTTLS komutu vermelidir. bc14sm16722533pac.6 - gsmtp \t javax.mail.Transport.send0 (Transport.java:218) \t de javax.mail.Transport.send (Transport.java:80) en \t ' –

2

.