Sunga
Sunga

Reputation: 309

Send email in Java via relay server

I am running a Java web application and attempting to send email notifications from it. When we were hosting this application on one of our servers it worked perfectly fine. We pointed it to smtp.gmail.com on port 25 and the messages we able to be sent.

But now we are hosting this on one of our client's servers for security purposes. They happen to block all smtp domains and only allow their own (SMTPRELAY.COMPANYNAME.COM). I changed the host in my code, but now the authentication is failing.

Is it possible to still authenticate my gmail account while using this company's relay server to send the emails? What is the best solution?

Below is the relevant portion of the code.

content = "message content";
List<String> recipients = LIST_OF_RECIPIENT_EMAIL_ADDRESSES;

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "SMTPRELAY.COMPANYNAME.COM");
    props.put("mail.smtp.port", "25");
    props.put("mail.smtp.ssl.trust", "SMTPRELAY.COMPANYNAME.COM");
    props.put("mail.debug", "true");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            //This is where the email account name and password are set
            return new PasswordAuthentication("[email protected]", "PASSWORD");
        }
      });
    try{
         MimeMessage message = new MimeMessage(session);

         message.setReplyTo(new Address[]{new InternetAddress("[email protected]")});
         for(String recipient: recipients){
             message.addRecipient(Message.RecipientType.BCC,new InternetAddress(recipient));
         }
         message.setSubject(subject);
         message.setContent(content,"text/html");
         Transport.send(message);
         return true;
      }catch (MessagingException mex) {
         mex.printStackTrace();
         return false;
      }

Here is the error I keep getting:

javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful

Upvotes: 3

Views: 15406

Answers (1)

Sunga
Sunga

Reputation: 309

It turns out the company relay smtp server can be used without authentication using any spoofed email address that has their domain (e.g. [email protected]). Thus the need to authenticate against smtp.gmail.com is rendered moot.

Still curious if it's possible to authenticate against a blocked smtp while sending the actual message through another. But my problem is solved.

If you are curious how to send an email without authentication there are many places to look up how to do it, but basically just change my code in my original question from:

props.put("mail.smtp.auth", "true");
.
.
.
Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(email+"@"+domain, password);
            }
          });

Change the above portions to the following:

props.put("mail.smtp.auth", "false");
.
.
.
Session session = Session.getInstance(props);

Upvotes: 8

Related Questions