M
M
mrozhentsev2022-01-22 17:32:02
Java
mrozhentsev, 2022-01-22 17:32:02

Can't send message to Java Mail - Bad type on operand stack?

Mail.java

package com.example.babushki;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

class HelloEmail {
    public static void main(String[] args) throws MessagingException {
        //Запомним контекстный загрузчик классов
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(javax.mail.Session.class.getClassLoader());

            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.ru");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.socketFactory.port", "465");
            properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

            Session session = Session.getDefaultInstance(properties,
                    new Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication("[email protected]",
                                    "********");
                        }
                    });

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
            message.setSubject("Очень важное письмо!!!");
            message.setText("Hello, Email!");
            Transport.send(message);
        } finally {
            //Вернем исходный контекстный загрузчик классов
            Thread.currentThread().setContextClassLoader(loader);
        }
    }
}


Mistakes
Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    com/sun/mail/handlers/handler_base.getTransferData(Ljavax/activation/ActivationDataFlavor;Ljavax/activation/DataSource;)Ljava/lang/Object; @20: invokevirtual
  Reason:
    Type 'javax/activation/ActivationDataFlavor' (current frame, stack[1]) is not assignable to 'java/awt/datatransfer/DataFlavor'
  Current Frame:
    bci: @20
    flags: { }
    locals: { 'com/sun/mail/handlers/handler_base', 'javax/activation/ActivationDataFlavor', 'javax/activation/DataSource', '[Ljavax/activation/ActivationDataFlavor;', integer }
    stack: { 'javax/activation/ActivationDataFlavor', 'javax/activation/ActivationDataFlavor' }
  Bytecode:
    0000000: 2ab6 0003 4e03 3604 1504 2dbe a200 1e2d
    0000010: 1504 322b b600 0699 000d 2a2d 1504 322c
    0000020: b600 07b0 8404 01a7 ffe1 01b0          
  Stackmap Table:
    append_frame(@8,Object[#5],Integer)
    same_frame(@36)
    chop_frame(@42,1)

  at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
  at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3137)
  at java.base/java.lang.Class.getConstructor0(Class.java:3342)
  at java.base/java.lang.Class.newInstance(Class.java:556)
  at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:631)
  at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:585)
  at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:627)
  at javax.activation.DataHandler.writeTo(DataHandler.java:329)
  at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:327)
  at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1568)
  at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2264)
  at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2224)
  at javax.mail.Transport.send(Transport.java:123)
  at com.example.babushki.HelloEmail.main(Mail.java:35)


I need to send a message to a gmail box. Googled, nothing helps at all.

I can’t even imagine what the problem is, I don’t understand what Java wants to tell me with this. Came from another language, new to Java.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2022-01-22
Hasanly @azerphoenix

smtp.gmail.ru
are you sure about that?
rather:
smtp.gmail.com
Do you send a letter to yourself?

message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));

https://support.google.com/mail/answer/7126229?hl=...
smtp.gmail.com

Требуется SSL: да

Требуется TLS: да (при наличии)

Требуется аутентификация: да

Порт для SSL: 465

Порт для TLS/STARTTLS: 587

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question