2015-06-19 97 views
5

我有java程序(複製谷歌)發送郵件使用office365 SMTP,它作爲一個站點的Java程序工作正常,但當我部署此Java程序作爲jar文件在網絡-inf/lib並從jsp調用該方法時拋出以下錯誤: javax.mail.SendFailedException:發送失敗; 嵌套的異常是: javax.mail.MessagingException:530 5.7.57 SMTP;客戶未通過身份驗證發送匿名郵件時發送郵件來自javax.mail.MessagingException:530 5.7.57 SMTP;客戶端未通過身份驗證發送匿名郵件在郵件從

有人可以請分享他們對此問題的看法。

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字符串smtpHost =「smtp.office365.com」; \t String portNumber =「587」; – Jay

+0

我面對完全相同的isuse ... –

回答

3

你可以用下面的配置嘗試,因爲它爲我的作品:

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

另外,我用主持人:

host = "Outlook.office365.com"

希望這有助於你或其他人。

+0

我試過這個。但同樣的例外再次出現 530 5.7.57 SMTP;在MAIL FROM期間,客戶端未通過身份驗證發送匿名郵件 – apm

0

smtp.starttls.enable設置爲true,主機爲smtp.office365.com解決了我的類似問題。

我只使用這3個屬性的代碼進行了測試:

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

主機:smtp.office365.com,我可以從我的帳戶發送電子郵件。

的整體功能:

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

和認證

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

請設置以下 X - 梅勒爲message.setHeader( 「X - 梅勒」, 「你的應用程序名稱」) ;

相關問題