2017-10-20 152 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。您没有通过公司邮件服务器进行身份验证,因此它不会让您在公司外发送邮件。