Solved

Using JavaMail in a webapp

Posted on 2013-11-19
4
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

734 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