2015-06-19 23 views
5

Tôi có chương trình java (Sao chép từ google) để gửi email bằng cách sử dụng office365 SMTP, nó hoạt động tốt như một chương trình java đứng nhưng khi tôi triển khai chương trình java này dưới dạng tệp jar trong web -inf/lib của một ứng dụng web và gọi phương thức từ jsp nó là ném lỗi dưới đây: javax.mail.SendFailedException: Gửi không thành công; ngoại lệ lồng nhau là: javax.mail.MessagingException: 530 5.7.57 SMTP; Khách hàng không được xác thực để gửi thư ẩn danh trong MAIL FROMjavax.mail.MessagingException: 530 5.7.57 SMTP; Khách hàng đã không được xác thực để gửi thư nặc danh trong MAIL FROM

Ai đó có thể chia sẻ quan điểm của họ về vấn đề này.

Mã 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

SmtpHost, portNumber Tôi đang sử dụng String smtpHost = "smtp.office365.com"; \t Chuỗi cổngNumber = "587"; – Jay

+0

Tôi đang đối mặt với chính xác cùng một sử dụng ... –

Trả lời

3

Bạn có thể thử với cấu hình như sau, vì nó làm việc cho tôi:

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

Ngoài ra, tôi sử dụng host:

host = "Outlook.office365.com"

Hope điều này giúp bạn hoặc người khác.

+0

Tôi đã thử điều này. Nhưng cùng một ngoại lệ lại đến 530 5.7.57 SMTP; Khách hàng không được xác thực để gửi thư ẩn danh trong MAIL FROM – apm

0

Đặt smtp.starttls.enable thành true và máy chủ lưu trữ smtp.office365.com giải quyết được vấn đề tương tự đối với tôi.

Tôi đã thử nghiệm nó với mã của bạn chỉ sử dụng những 3 thuộc tính:

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

dẫn chương trình: smtp.office365.com và tôi có thể gửi email từ tài khoản của tôi.

Toàn bộ chức năng:

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; 
    } 

và xác thực

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

Hãy đặt bên dưới X-Mailer như message.setHeader ("X-Mailer", "tên ứng dụng của bạn") ;

Các vấn đề liên quan