简体   繁体   English

通过javamail从办公室邮件发送电子邮件

[英]Send email from office mail via javamail

I am sending emails via gmail, but when I try to send emails from corporate mail I am get exception. 我正在通过gmail发送电子邮件,但是当我尝试从公司邮件发送电子邮件时,却出现异常。

From company mail I can send email to another employees mail, but when I try to send to gmail account I get javax.mail.SendFailedException: Invalid Addresses; 从公司邮件中,我可以将电子邮件发送给其他员工邮件,但是当我尝试发送至gmail帐户时,我收到javax.mail.SendFailedException:无效的地址; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay exception. 嵌套的异常是:com.sun.mail.smtp.SMTPAddressFailedException:550 5.7.1无法中继异常。 How can I fix it? 我该如何解决?

Via gmail 通过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);
    }

Via corporate mail 通过公司邮件

    final String toGmail = "test@gmail.com";
    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);
    }

The answer is in the JavaMail FAQ . 答案在JavaMail FAQ中 You're not authenticating to your corporate mail server so it's not letting you send messages outside the company. 您不是在对公司邮件服务器进行身份验证,因此不会让您在公司外部发送邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM