Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:10:03+00:00 2026-06-09T22:10:03+00:00

I am making use of Java Mail API to develop an app to send

  • 0

I am making use of Java Mail API to develop an app to send mail. I can successfully send the mail if I use the mail server hosting by yahoo or gmail. However, when I try to use my own mail server, I am fail to send the mail, what are the possible reasons causing the failure? When I execute my app in my device, it displays “Email was not sent”, so no exception occurs. May I ask how can I change my program so I can successfully connect my app to my smtp server, or how can I get more information to display therefore I can understand why the connection has failed.

Here is my program:
Sender.java

public class Sender extends Activity {

    Button Send;
    TextView text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sender);

        Send = (Button) findViewById(R.id.mail);
        text = (TextView) findViewById(R.id.textView1);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
        .permitAll().build();
        StrictMode.setThreadPolicy(policy); 

        Send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new SendMail().execute();
            }
        });

    }

    private class SendMail extends AsyncTask<String, Void, Integer> 
    { 
        ProgressDialog pd = null;
        String error = null;
        Integer result;

        @Override 
        protected void onPreExecute() { 
            // TODO Auto-generated method stub 
            super.onPreExecute(); 
            pd = new ProgressDialog(Sender.this); 
            pd.setTitle("Sending Mail"); 
            pd.setMessage("Please wait..."); 
            pd.setCancelable(false); 
            pd.show(); 
        } 

        @Override  
        protected Integer doInBackground(String... params) {  
            // TODO Auto-generated method stub  

            MailSender sender = new MailSender("me@mysmtp.com", "password");  

            sender.setTo(new String[]{"you@mysmtp.com", "me@gmail.com"}); 
            sender.setFrom("me@mysmtp.com"); 
            sender.setSubject("Test mail"); 
            sender.setBody("This is the mail body"); 
            try { 
                if(sender.send()) { 
                    System.out.println("Message sent"); 
                    return 1; 
                } else {
                    return 2; 
                } 
            } catch (Exception e) {    
             error = e.getMessage(); 
             Log.e("SendMail", e.getMessage(), e);     
            } 
            return 3;  
        }  

        protected void onPostExecute(Integer result) { 
            pd.dismiss(); 
            if(error!=null) { 
                text.setText(error); 
            } 
            if(result==1) { 
                Toast.makeText(Sender.this, 
                    "Email was sent successfully.", Toast.LENGTH_LONG) 
                    .show(); 
            } else if(result==2) { 
                Toast.makeText(Sender.this, 
                    "Email was not sent.", Toast.LENGTH_LONG).show(); 
            } else if(result==3) { 
                Toast.makeText(Sender.this, 
                    "There was a problem sending the email.", 
                    Toast.LENGTH_LONG).show(); 
            } 
        } 
    }  
}

MailSender.java

public class MailSender extends Authenticator { 
    private String user; 
    private String password; 

    private String [] to; 
    private String from; 

    private String port; 
    private String sport; 

    private String host; 

    private String subject; 
    private String body; 

    private boolean auth; 
    private boolean debuggable; 

    private Multipart multi; 

    public MailSender(){ 
        host = "web.mysmtp.com"; 
        port = "25"; 
        sport = "25"; 

        user = ""; 
        password = ""; 
        from = ""; 
        subject = ""; 
        body = ""; 

        debuggable = false; 
        auth = true; 

        multi = new MimeMultipart(); 

        // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. 
        MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();  
        mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");  
        mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");  
        mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
        mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");  
        CommandMap.setDefaultCommandMap(mc);  
    } 

    public MailSender(String user, String password){ 
        this();       
        this.user = user; 
        this.password = password;    
    } 

    public boolean send() throws Exception { 
        Properties props = setProperties(); 

        try{ 
            Session session = Session.getInstance(props, this); 
            session.setDebug(true); 

            MimeMessage msg = new MimeMessage(session); 

            msg.setFrom(new InternetAddress(from)); 

            InternetAddress[] addressTo = new InternetAddress[to.length]; 
            for(int i=0; i<to.length; i++){ 
                addressTo[i] = new InternetAddress(to[i]); 
            } 

            msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 
            msg.setSubject(subject); 
            msg.setSentDate(new Date()); 

            BodyPart messageBodyPart = new MimeBodyPart(); 
            messageBodyPart.setText(body); 
            multi.addBodyPart(messageBodyPart); 

            msg.setContent(multi); 

            Transport transport = session.getTransport("smtps"); 
            transport.connect(host, 25, user, password); 
            transport.sendMessage(msg, msg.getAllRecipients()); 
            transport.close(); 
            return true; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            return false; 
        } 
    } 

    public void addAttachment(String filename) throws Exception { 
        BodyPart messageBodyPart = new MimeBodyPart(); 
        DataSource source = new FileDataSource(filename); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(filename); 

        multi.addBodyPart(messageBodyPart); 
    } 

    @Override  
      public PasswordAuthentication getPasswordAuthentication() {  
        return new PasswordAuthentication(user, password);  
      } 

    private Properties setProperties() { 
        Properties props = new Properties(); 

        props.put("mail.smtp.host", host); 

        if(debuggable) { 
            props.put("mail.debug", "true"); 
        } 

        if(auth) { 
            props.put("mail.smtp.auth", "true"); 
        } 

        props.put("mail.smtp.port", port); 
        props.put("mail.smtp.socketFactory.port", sport); 
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
        props.put("mail.smtp.socketFactory.fallback", "false"); 

        return props; 
    } 

    public void setTo(String[] toAddress) { 
        this.to = toAddress; 
    } 

    public void setFrom(String fromAddress) { 
        this.from = fromAddress; 
    } 

    public void setSubject(String subject) { 
        this.subject = subject; 
    } 

    public void setBody(String body) {  
        this.body = body;  
    } 
}

Error messages in error log:

08-15 11:21:37.284: W/System.err(2553):     javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5c075548: Failure in SSL library, usually a protocol error
08-15 11:21:37.291: W/System.err(2553): Caused by: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5c075548: Failure in SSL library, usually a protocol error
08-15 11:21:37.299: W/System.err(2553): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5c075548: Failure in SSL library, usually a protocol error
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:10:04+00:00Added an answer on June 9, 2026 at 10:10 pm

    You’re connecting to port 25 and using SSL. Port 25 is the non-SSL (plain text) port. Assuming your server is set up for SSL, use the connect method that doesn’t require you to specify a port number and let JavaMail use the default port.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java web service and a Java web client making use of
I am making a TicTacToe game in Java I am required to use a
I'm making use of an API on the internet that is marshalling objects to
I am making heavy use of Springs XML Configuration for Java Beans. Is there
I have defined a Pipe class in Java, making use of PipedReader and PipedWriter
I'm a Fedora user currently making use of inotify-java to implement directory polling. I
I've been using SQLite in my Java and C# applications, making use of the
I'm making use of the jQuery Tabs plugin along with the History plugin, to
We recently started making use of the new Google Expansion APK mechanism. Overall it
I am making use of a custom cursor on itemRenderers in a List component.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.