简体   繁体   中英

java sending email using Ms exchange server

HI I have this code working, to send email´s using Ms exchange server .

The problem is that it uses the sender email address and is pwd to do the authentication.

Now the system admin, tells me that due to security enforcement rules , authentication must be done with username and password , no email and password .. at the moment could´t find any solution for this ... my working code :

props.put("mail.smtp.host", host.trim());
props.put("mail.smtp.port", port.trim());
props.put("mail.smtp.auth","true");
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.smtp.quitwait","false");
props.put("mail.smtp.socketFactory.class",javax.net.ssl.SSLSocketFactory.class);
props.put("mail.smtp.socketFactory.fallback","true");
props.put("mail.debug","false");
props.put("mail.mime.charset","utf-8");
Session session = Session.getInstance(props, new javax.mail.Authenticator()
        {
          protected PasswordAuthentication getPasswordAuthentication() 
          {
            return new PasswordAuthentication(properties.getProperty("emailFrom"),properties.getProperty("emailPassword"));
          }
         });

If I remember correctly it's a setting on Exchange server to allow SMTP client to use user name or email address for authentication.

There are several ways to get it work.

  1. On Exchange server, normally email address follows a fixed name convention rule which we call Address Policy. In some organizations email address is converted from username. That means it's possible that you can get the user name from the email address. For example, if my user name is dongmao, with the address policy my email address can be:

    username@domain.com -> dongmao@domain.com

    username.mail@domain.com -> dongmao.mail@domain.com

    So just ask the system admin is there is a way to figure out the user name. More details about email address policy can be found here through I don't think you will be interested if you are not an Exchange man.

  2. If you are dealing with Microsoft Exchange, then I assume you are in an environment with Microsoft Active Directory. Both email address and username are actually AD attributes and both of them are unique so it's easy to query username by email address or vice versa.

    I don't have the java code at hand to query AD, but there are quite a lot on the Internet, this can be a good one. Just pay attention to the LDAP query string.The attribute name in AD of email address is proxyAddresses which is a multi-value attribute. The username attribute is samAccountName .

Let me know if you have further questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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