2015-06-19 8 views
5

Mam program java (skopiowany z google), aby wysyłać wiadomości e-mail za pomocą SMTP Office365, działa dobrze jako program stand java, ale kiedy wdrażam ten program java jako plik jar w sieci -inf/lib aplikacji WWW i wywołanie metody z jsp powoduje zgłoszenie następującego błędu: javax.mail.SendFailedException: Wysyłanie nie powiodło się; wyjątek zagnieżdżony to: javax.mail.MessagingException: 530 5.7.57 SMTP; Klient nie został uwierzytelniony, aby wysyłać anonimową pocztę podczas przesyłania wiadomości e-mail pod numeremjavax.mail.MessagingException: 530 5.7.57 SMTP; Klient nie został uwierzytelniony, aby wysyłać anonimową pocztę podczas wysyłania wiadomości e-mail pod nr

Czy ktoś może podzielić się swoimi poglądami na ten temat.

kod Java:

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; 

import org.apache.log4j.Logger; 

public class SendEmailUsingSMTP { 


    public static boolean sendEmail(String toAddress, String fromAddress, String userName, String userPassword,String smtpHost, String portNumber, String emailSubject,String emailBody) { 
     // Recipient's email ID needs to be mentioned. 

     Logger log = Logger.getLogger(SendEmailUsingSMTP.class); 
     log.info("toAddress : "+toAddress); 
     log.info("fromAddress : "+fromAddress); 
     log.info("userName : "+userName); 
     log.info("userPassword : "+userPassword); 
     log.info("smtpHost : "+smtpHost); 
     log.info("portNumber : "+portNumber); 
     log.info("emailSubject : "+emailSubject); 
     log.info("emailBody : "+emailBody); 

     String to = toAddress; 

     // Sender's email ID needs to be mentioned 
     String from = fromAddress;//change accordingly 
     final String username = userName;//change accordingly 
     final String password = userPassword;//change accordingly 

     // Assuming you are sending email through relay.jangosmtp.net 
     String host = smtpHost; 

     Properties props = new Properties(); 

     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.socketFactory.port", portNumber); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.port", portNumber); 

     // Get the Session object. 
     SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password); 
     props.put("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName()); 
     Session session = Session.getInstance(props, authenticator); 


     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(emailSubject); 

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

     // Send message 
     Transport.send(message); 

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

     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
     } 
    return true; 
    } 
} 
+0

The smtpHost, numer portu używam String smtpHost = "smtp.office365.com"; \t String portNumber = "587"; – Jay

+0

Mam dokładnie to samo isuse ... –

Odpowiedz

3

Można spróbować z poniższej konfiguracji, jak to działa na mnie:

"mail.smtp.starttls.enable":"true"

także użyłem host:

host = "Outlook.office365.com"

Nadzieja to pomaga tobie lub komuś innemu.

+0

Próbowałem tego. Ale ten sam wyjątek znowu nadchodzi 530 5.7.57 SMTP; Klient nie został uwierzytelniony, aby wysyłać anonimową pocztę podczas wysyłania wiadomości – apm

0

Ustawienie adresu smtp.starttls.enable na i hosta na smtp.office365.com rozwiązało podobny problem dla mnie.

Testowałem go z kodu przy użyciu tylko te 3 właściwości:

props.put("mail.smtp.auth",true); 
props.put("mail.smtp.starttls.enable",true); 
props.put("mail.smtp.host", host); 

host: smtp.office365.com i mogłem wysłał e-maile z mojego konta.

Cała funkcja:

public static boolean sendEmail(String toAddress, String fromAddress, String userName, String userPassword,String smtpHost, String emailSubject,String emailBody) { 
     // Recipient's email ID needs to be mentioned. 


     String to = toAddress; 

     // Sender's email ID needs to be mentioned 
     String from = fromAddress;//change accordingly 
     final String username = userName;//change accordingly 
     final String password = userPassword;//change accordingly 

     // Assuming you are sending email through relay.jangosmtp.net 
     String host = smtpHost; 

     Properties props = new Properties(); 

     props.put("mail.smtp.auth",true); 
     props.put("mail.smtp.starttls.enable",true); 
     props.put("mail.smtp.host", host); 

     // Get the Session object. 
     SimpleMailAuthenticator authenticator = new SimpleMailAuthenticator(username, password); 
     Session session = Session.getInstance(props, authenticator); 


     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(emailSubject); 

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

     // Send message 
     Transport.send(message); 

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

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

i uwierzytelnienia

class SimpleMailAuthenticator extends Authenticator { 


    String userName; 
    String password; 
    PasswordAuthentication authentication; 

    public SimpleMailAuthenticator(String userName,String password) { 
     super(); 
     this.userName = userName; 
     this.password = password;   
     authentication = new PasswordAuthentication(userName, password); 
    } 

    @Override 
    public PasswordAuthentication getPasswordAuthentication() { 
     return authentication; 
    } 


} 
0

Proszę ustawić poniżej X-Mailer jako message.setHeader ("X-Mailer", "Twoja nazwa aplikacji") ;

Powiązane problemy