简体   繁体   中英

sending mail through java encounter error

now a days i'm facing quite strange problem & that is during send mails this is strange because when i'm testing my application on localhost everything goes perfect but as i deployed my application its enable to send mails .

you guys can check it as well http://www.mkjit-solutions.com/app/jsf/Contactus.xhtml

i'm seeing this exception:

javax.mail.SendFailedException: Sending failed; nested exception is:
class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. yv6sm13782372pac.29 - gsmtp 

the code which i used is

props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.port", "587"); 
props.put("mail.debug", "true"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.starttls.enable", "true");


Session session = Session.getInstance(props,
          new javax.mail.Authenticator() 
                  {
            protected PasswordAuthentication getPasswordAuthentication() 
                        {
                return new PasswordAuthentication(userAddress, userPassword);
            }
          });

        customMsg += "3 - ";

        try {

                        customMsg += "3.1 - ";
            Message message = new MimeMessage(session);

                        customMsg += "3.2 - ";
            message.setFrom(new InternetAddress(userAddress));

                        customMsg += "3.3 -  ";
                        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(toAddress));

                        customMsg += "3.4 -  ";
                        message.setSubject(subject);

                        customMsg += "3.5 -  ";
                        message.setText(msg);

                        customMsg += "3.6 -  ";
 Transport transport = session.getTransport("smtp");
            Transport.send(message);

                  customMsg += "4 -  ";


                    System.out.println("ua"+userAddress);
                    System.out.println(""+userPassword);
                    System.out.println("ta"+toAddress);




                  System.out.println("Done");
                        flag = true;
                        customMsg += " [Value of Flag "+flag+"] ";
        } 
                catch (Exception ex) 
                {
                    System.out.println("");
                    ex.printStackTrace();
                    flag = false;
                    customMsg += " [Value of Flag "+flag+"] ";
                    this.e = ex;
                    //throw new RuntimeException(e);

        }

                return flag;

Start by fixing these common mistakes .

Gmail sample code is in the JavaMail FAQ .

If you still can't get it to work, follow these debugging tips , and post the results.

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