发送电子邮件功能出错

时间:2017-09-29 18:27:04

标签: java maven compiler-errors

尝试使用Swing在Java中发送电子邮件,但是我收到了错误,我没有找到如何修复,有代码(我从某处获取并删除了几个字段):

 import sun.plugin2.message.transport.Transport;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

class SendEmailClient extends JFrame {
   ....    
    private SendEmailClient() {
        InitializeUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                SendEmailClient client = new SendEmailClient();
                client.setVisible(true);
            }
        });
    }

    private void InitializeUI() {
        setTitle("Send E-mail Client");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(new Dimension(400, 280));

        // Header Panel
      }

    private class SendEmailActionListener implements ActionListener {
        SendEmailActionListener() {
        }

        public void actionPerformed(ActionEvent e) {
            Properties props = new Properties();
            props.put("mail.smtp.host", mailSmtpHostComboBox.getSelectedItem());
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

            Session session = Session.getDefaultInstance(props);
            try {
                InternetAddress fromAddress = new InternetAddress(fromField.getText());
                InternetAddress toAddress = new InternetAddress(toField.getText());

                javax.mail.Message message = new MimeMessage(session);
                message.setFrom(fromAddress);
                message.setRecipient(javax.mail.Message.RecipientType.TO, toAddress);
                message.setSubject(subjectField.getText());
                message.setText(contentTextArea.getText());

                Transport.send(message, usernameField.getText(),
                        new String(passwordField.getPassword()));
            } catch (MessagingException ex) {
                ex.printStackTrace();
            }
        }
    }
}

和maven依赖(可能不需要其中一些):

  <dependency>
    <groupId>org.jdesktop.bsaf</groupId>
    <artifactId>bsaf</artifactId>
    <version>1.9RC5</version>
</dependency>


<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
</dependency>

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

在发送功能上有错误:

java: cannot find symbol
  symbol:   method send(javax.mail.Message,java.lang.String,java.lang.String)
  location: interface sun.plugin2.message.transport.Transport

我正在尝试在pom中添加一些依赖项,但它似乎是错误的决定,但不要介意如何修复它。有人知道吗?

1 个答案:

答案 0 :(得分:0)

首先从正确的pacakge导入传输类。

   import javax.mail.Transport;//Use this

现在使用身份验证器来验证用户名和密码

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

现在调用传输类的 send()方法,并传递消息对象。

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("from-email@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("to-email@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"
            + "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }