Solved

Using JavaMail in a webapp

Posted on 2013-11-19
4
237 Views
Last Modified: 2013-11-25
Hello,

Im coding a web app in which there is an option to send an email with some data using our Exchange server.

Im using a servlet and a class with the JavaMail api to do that, but I want to authenticate that the person sending the email has a valid Exchange username and password, but I can write anything on those fields and the email is sent without a problem.

This is my mail client code:

public static void sendEmail(String toAddress, String html,String cuenta) 
            throws AddressException,
            MessagingException {
 
        final String userName = "myexchangemail@mailserver.com";
        final String password = "1234";
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", "smtp.mailserver.com");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "25");
        
        
        Authenticator auth = new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };
 
        Session session = Session.getInstance(properties, auth);
 
        Message msg = new MimeMessage(session);
 
        msg.setFrom(new InternetAddress(userName));
        InternetAddress[] toAddresses = InternetAddress.parse(toAddress) ;
        msg.setContent(html,"text/html");
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject("Test Email");
        msg.setSentDate(new Date());
        Transport.send(msg);
}

Open in new window


I need that only correct username/password combinations of existing exchange mailboxes work on this webapp.
0
Comment
Question by:ScreenFox
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39659975
I need that only correct username/password combinations of existing exchange mailboxes work on this webapp.
But you've hardcoded the credentials - you're not using those of the user ...
0
 

Author Comment

by:ScreenFox
ID: 39661762
Yeah I should have stated that those were for testing purposes only, my point is that I can change those Strings to anything and the server accepts the authentication and sends the email from behalf of the userName (in this case myexchangemail@mailserver.com).

In the final code the userName and password will be entered by the user on the webapp.

What I want is that only actual mailbox users of this Exchange server can login with their userName and password.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39662316
What I want is that only actual mailbox users of this Exchange server can login with their userName and password.
That will automatically be the case. If the Exchange server sends mail with arbitrary credentials then it's seriously misconfigured and you should inform the sysadmin for the server
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39673906
:)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In-place Upgrading Dirsync to Azure AD Connect
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
how to add IIS SMTP to handle application/Scanner relays into office 365.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question