简体   繁体   中英

sending email using java mail-api fails

Desperatly trying to send an email to myself:

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.web.de");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getInstance(props);

    Message msg = new MimeMessage(session);
    try {
        msg.setFrom(new InternetAddress("myveryownemail@web.de"));
        msg.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("myveryownemail@web.de")});
        msg.setSubject("whatever");
        msg.setContent("whatever", "text/plain");

        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.web.de", 587, "myveryownemail", "myveryownandcorrectpassword");

        Transport.send(msg);
    } catch (MessagingException e) {
        e.printStackTrace();
    }

I´m getting
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
but username and password is absolutly correct (tried username with and without the @web.de). password is the one I use for normal login into my mail account.
don´t get it

Create the javax.mail.Session object like below, with your username and password:-

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

Ref tutorial:- http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

Set the below properties with your smtp user and password

props.put("mail.smtp.user", "myuser");
props.put("mail.smtp.password", "mypwd");

Reference : http://www.tutorialspoint.com/java/java_sending_email.htm

In past I solved similar issue adding this:

if ("true".equals(emailConfig.get("mail.smtp.auth"))) {
    session = Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(emailConfig.get("mail.smtp.user"), emailConfig.get("mail.smtp.password"));
        }
    });
}

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(emailFromAddress));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailToAdrress));
msg.setSubject(mailSubject);
msg.setText(mailMessageBody);
Transport.send(msg);

Session is javax.mail.Session

Hope you have included the "mail.jar" and "activation.jar" in your project. This link may help you

You're calling the Transport.send static method, which ignores the Transport instance you created and connected. Call the sendMessage method instead, or skip creating your own Transport instance and call the static send method that takes a username and password.

This is one of the JavaMail common mistakes .

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