简体   繁体   中英

java mail javax.mail.AuthenticationFailedException

Hi i have write a code for sending mails from my server using java mail

Here is my code

Properties pros = new Properties();
pros.put("mail.smtp.host", "my Ip Adress");
pros.put("mail.smtp.auth", "true");
pros.put("mail.smtp.port", "25");
Session mailSession = Session.getDefaultInstance(pros,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("emailId","password");
                }
            });
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("sendingAddress"));
message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("recieverAddress"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
                    "\n\n No spam to my email, please!");
Transport.send(message);
return "SUCCESS";

All the details i have provided in my code are correct, i mean the property setting,i have checked it. But unfortunately it shows some error message like Authentication failed

The error message is like this

javax.mail.AuthenticationFailedException

Anybody have any idea about this?

I am using struts2 framework for my project.

Use this code..! you are using traditional method of getting authentication from mail server

import java.util.*;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
public void Mail(String to[], String subject, String content_message)
        throws MessagingException {

    boolean debug = false;

    try {
        String host = "smtp.gmail.com";
        String from = "yourmail@gmail.com";
        String pass = "yourpwd";
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true"); 
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) { 
            toAddress[i] = new InternetAddress(to[i]);
        }
        System.out.println(Message.RecipientType.TO);
        for (int i = 0; i < toAddress.length; i++) { 
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }
        message.setSubject(subject);

        message.setContent(content_message, "text/html; charset=\"UTF-8\"");
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    catch (Exception e) {
        System.out.println("Unable to connect");
    }
}

Call the constructor whenever you need to send a mail..

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