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

  • Home
  • SEARCH
  • 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 7648397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:42:19+00:00 2026-05-31T10:42:19+00:00

I have written java code for send mail which is giving exception : when

  • 0

I have written java code for send mail which is giving exception : when i use port number 465

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 l1sm2119061pbe.54

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

String USERNAME = username; 
String PASSWORD = password; 


Properties props = new Properties(); 
props.put("mail.smtp.host", smtpHost); 
if(smtpPort != null){
    int SMTP_PORT = Integer.parseInt(smtpPort); 
    props.put("mail.smtp.port", SMTP_PORT); 
}
props.put("mail.from",from);
props.put("mail.smtp.starttls.enable", "true"); 
if( auth == 1){
    props.put("mail.smtp.auth", "true"); 
}
props.put("mail.debug", "true"); 

Session session = Session.getInstance(props, new Authenticator() { 
        @Override 
        protected PasswordAuthentication getPasswordAuthentication() { 
            return new PasswordAuthentication(USERNAME, PASSWORD); 
        } 
}); 

MimeMessage msg = new MimeMessage(session); 
msg.setFrom(); 
msg.setRecipients(Message.RecipientType.TO, to); 
msg.setSubject(subject); 
msg.setSentDate(new Date());

Multipart content = new MimeMultipart();
MimeBodyPart bodyPart = new MimeBodyPart();

bodyPart.setText(htmlBody, "UTF-8");
bodyPart.setContent(htmlBody, "text/html");

content.addBodyPart(bodyPart);

if ( attachmentPath != null ){
    String[] a = attachmentPath.split(",");

    for (int i = 0; i < a.length; i++) { 
            if( a[i] != null )
        {
            MimeBodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.attachFile(a[i].trim());
            content.addBodyPart(attachmentPart);
        }
    }
}


msg.setContent(content);

Transport.send(msg); 
return true
  • 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-05-31T10:42:20+00:00Added an answer on May 31, 2026 at 10:42 am

    Code is self explanatory, this is a working code that i used in a project. Hope this help you out. Good Luck.

    /**
     GmailSmtpSSL emailNotify = new GmailSmtpSSL(cred[ID], cred[PASS]);
    
     emailNotify.sendMailTo("self","Testing AlfaDX Gmail module", "Yes it works");
    
    **/
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Properties;
    
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class GmailSmtpSSL {
    
        GmailSmtpSSL(String username,String password) {
            usern = username;
            pass  = password;
    
            setDebugMsg("Setting user name to : "+usern);
            setDebugMsg("Using given password : "+pass);
    
            props = new Properties();
            setDebugMsg("Setting smtp server: smtp.gmail.com");
            setDebugMsg("Using SSL at port 465 auth enabled");
    
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.user", usern);
    
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            //props.put("mail.smtp.debug", "true");
    
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");
    
            SMTPAuthenticator auth = new SMTPAuthenticator();
            session = Session.getDefaultInstance(props, auth);
            session.setDebug(true);
            setDebugMsg("Session initialization complete");
        }
    
        public void destroy() {
            props.clear();
            props = null;
            usern = null;
            pass  = null;
            session = null;
    
        }
    
        public void sendMailTo(String to, String sub, String body)
                                                        throws MessagingException {
    
             Calendar currentDate = Calendar.getInstance();
             SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss a"); 
             String dateToday = formatter.format(currentDate.getTime()).toLowerCase();
    
             if (to.equals("self")) 
                 to = usern;
    
             setDebugMsg("Composing message: To "+to);
             setDebugMsg("Composing message: Subject "+sub);
    
             try {
                 Message message = new MimeMessage(session);
                 message.setFrom(new InternetAddress(usern));
                 message.setRecipients(Message.RecipientType.TO, 
                                                        InternetAddress.parse(to));
                 message.setSubject("PinguBot: "+dateToday+" "+sub);
                 message.setText(body);
                 setDebugMsg("Attempting to send...");
                 Transport transport = session.getTransport("smtps");
    
                 transport.connect("smtp.gmail.com", 465, usern, pass);
                 Transport.send(message);
                 transport.close();
             }
    
             catch(MessagingException me) {
                 setDebugMsg(me.toString());
                 throw new MessagingException(me.toString());
             }
                 setDebugMsg("Mail was send successfully");
        }
    
    
        public String getDebugMsg() {
            String msg = new String(debugMsg);
            debugMsg = " ";
            return msg;
        }
        private static void setDebugMsg(String msg) {
            debugMsg += msg + "\n";
            System.out.println(msg);
        }
    
        private static String debugMsg = "";
        private String usern; 
        private String pass;
        private Session session;
        private static Properties props;
    
        private class SMTPAuthenticator extends Authenticator
        {
            public PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(usern, pass);
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Java code which imports an external jar file. How can
I have written a piece of Java code which is intended as to call
I am trying to use custom font in android. I have written java code
I have written the java code below, which executes another java program named Newsworthy_RB.
I have written code in Java to access web cam,and to save image... I
In our application, I have seen code written like this: User.java (User entity) public
I have written a java applet which opens a JFrame (so when run in
I have written a small java application for which I need to obtain performance
I have a program which i have myself written in java, but I want
Hi I work with netbeans. I have written a code which has two classes

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.