2017-10-20 193 views
0

我通過Gmail發送電子郵件,但是當我嘗試從公司郵件發送電子郵件時,我得到異常。通過javamail從辦公室郵件發送電子郵件

從公司郵件我可以發送電子郵件給其他員工的郵件,但是當我嘗試發送到gmail帳戶時,我得到javax.mail.SendFailedException:無效的地址; 嵌套的異常是: com.sun.mail.smtp.SMTPAddressFailedException:550 5.7.1無法中繼異常。 我該如何解決它?

通過Gmail

final String username = "mail goes here"; 
    final String password = "password goes here"; 

    final String to = "mail goes here"; 
    final String from = username; 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.transport.protocol", "smtp"); 
    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"); 
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com"); 

    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(from)); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(toGmail)); 
     message.setSubject("Test"); 
     message.setText("Testing"); 
     Transport.send(message); 
     System.out.println("Mail sent succesfully"); 
    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 

通過企業郵件

final String toGmail = "[email protected]"; 
    final String toCompany = "one of employees mail goes here"; 
    final String from = "company's noreply mail goes here"; 
    final String to = toGmail; 

    Properties props = new Properties(); 
    props.put("mail.transport.protocol", "smtp"); 
    props.put("mail.smtp.port", "25"); 
    props.put("mail.smtp.host", "10.100.25.5"); 
    props.setProperty("mail.debug", "true"); 

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

    Session session = Session.getInstance(props); 

    try { 
     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(from)); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(to)); 
     message.setSubject("Test"); 
     message.setText("Testing"); 
     Transport.send(message); 
     System.out.println("Mail sent succesfully"); 
    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 
+0

我很困惑從哪裏到哪裏你想發送郵件。你能澄清一下嗎? –

+0

從公司mait到gmail –

+0

您是否嘗試過使用SMTP端口465? –

回答

0

答案是在JavaMail FAQ。您沒有通過公司郵件服務器進行身份驗證,因此它不會讓您在公司外發送郵件。