2014-02-19 75 views
4

我們正在使用Spring郵件發送從java應用程序的電子郵件 org.springframework.mail.javamail.JavaMailSenderImpl例外:被拒絕的傳遞

春天電子郵件配置是

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" autowire-candidate="default"> 
     <property name="host" value="${test.email.host}" /> 
     <property name="port" value="${test.email.port}" /> 
     <property name="username" value="${test.email.username}" /> 
     <property name="password" value="${test.email.password}" /> 
     <property name="javaMailProperties"> 
      <props> 
       <prop key="mail.smtp.auth">true</prop> 

      </props> 
     </property> 
    </bean> 

的Java代碼快照

@Autowired(required = true) 
    @Qualifier("errorMessageMailSender") 
    JavaMailSenderImpl mailSender; 

    .............. 
    .............. 

      try { 
       MimeMessage mailMessage = buildEmailMimeMessage(properties,mimeMultipart); 
       logger.info(String.format("Built MimeMessage object is <%s>",mailMessage)); 
       if (mailMessage != null) { 
        mailSender.send(mailMessage); 
        logger.info("Mail sent Successfully"); 
       }else{ 
        logger.info("Mail send failed as Mail message object construction failed."); 
       } 
       result=true; 
      } catch (Exception e) { 
       logger.error("An exception occurred while sending mail :: " + e.getMessage()); 
      } 

屬性文件

test.email.host=mail.mydomain.net 
test.email.port=2525 
[email protected] 
test.email.password=mypassword 

但我們例外下面,並沒有被髮送的電子郵件

An exception occurred while sending mail :: Failed messages: javax.mail.SendFailedException: Invalid Addresses; 
    nested exception is: 
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 <[email protected]>... Relaying denied 
+0

檢查http://www.gammadyne.com/relaying_denied.htm,谷歌的第一個結果 –

+0

這是一個SMTP問題,而不是Java問題... – fge

+0

如果我保持我的用戶名和密碼屬性爲空白,將smtp auth屬性設置爲true,會導致這個問題? –

回答

4

,你要發送郵件的SMTP服務器將拒絕中繼。如果您的本地服務器不是問題,則可以更改配置以從mydomain.net中繼郵件。但如果它是外部服務器(例如gmail),則必須使用真實的註冊域。

爲了在郵件服務器上測試你的代碼,我建議你設置一個dockerized的Apache James服務器,創建一些測試用戶併發送和接收郵件。

1

工作代碼,

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 JavaEmail { 

    Properties emailProperties; 
    Session mailSession; 
    MimeMessage emailMessage; 

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

     JavaEmail javaEmail = new JavaEmail(); 

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

    public void setMailServerProperties() { 

     String emailPort = "587";//gmail's 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 = { "enter to mail ids" }; 
     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 = "enter from Id";//ignore gmail domain 
     String fromUserEmailPassword = "enter password"; 

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

     transport.connect(emailHost, fromUser, fromUserEmailPassword); 
     transport.sendMessage(emailMessage, emailMessage.getAllRecipients()); 
     transport.close(); 

    } 

} 
0

據我所知,你的emailservice必須是你的生產領域內。你不能從該領域之外打出prod smtp服務器。至少我遇到的問題就是這種情況。

相關問題