【问题标题】:Java Simple Email Program in EclipseEclipse 中的 Java 简单电子邮件程序
【发布时间】:2015-07-12 09:47:17
【问题描述】:

我想制作一个简单的程序,您可以在其中从 命令行 发送电子邮件。我找到了这个教程,' http://www.tutorialspoint.com/java/java_sending_email.htm ',但是下载没有。那么我在哪里可以获得 JavaMail API 和 Java Activation Framework (JAF) 以及如何将它放在我的类路径中。

我基本上是在找人来分解它并告诉我如何制作一个电子邮件程序。

我正在使用 Eclipse Luna。

【问题讨论】:

  • 你试过谷歌吗?

标签: java email web-applications send


【解决方案1】:

看看这个例子。此示例仅将一个附件作为邮件发送。附件quiz.txt内容如下:

What is the Capital of India?/New Delhi
Where is the Taj Mahal?/Agra

这是SendMailExample.java 文件:

import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class SendMailExample {

    private String from;
    private String to;
    private String subject;
    private String messageBody;
    private String fileName;
    private String host;

    private Properties properties;

    private MimeMessage message;
    private BodyPart messageBodyPart;
    private Multipart multipart;

    private Authenticator authenticator;

    public SendMailExample () {
        from = "sender@gmail.com";
        to = "recipient@gmail.com";
        subject = "Subject Testing";
        messageBody = "<html><body><h1>HAVE FAITH, AND STAY" +
                    " CALM :-) I AM WITH YOU, OKAY :-)</h1></body></html>";
        fileName = "quiz.txt";
        host = "smtp.gmail.com";

        authenticator = new SMTPAuthenticator ();
        properties = System.getProperties ();
        properties.put ( "mail.smtp.host", host );
        properties.put ( "mail.smtp.starttls.enable", "true" );
        properties.put ( "mail.smtp.port", "587" );
        properties.put ( "mail.smtp.auth", "true" );
    }

    private void sendMail ( String from, String to,
                    String subject, String messageBody, String fileName ) {
        try {
            Session session = Session.getDefaultInstance ( properties, authenticator );
            message = new MimeMessage ( session );
            message.setFrom ( new InternetAddress ( from ) );
            message.addRecipient ( Message.RecipientType.TO,
                                new InternetAddress ( to ) );
            message.setSubject ( subject );

            multipart = new MimeMultipart ();
            messageBodyPart = new MimeBodyPart ();
            messageBodyPart.setContent ( messageBody, "text/html" );
            multipart.addBodyPart ( messageBodyPart );

            messageBodyPart = new MimeBodyPart ();
            DataSource source = new FileDataSource ( fileName );
            messageBodyPart.setDataHandler ( new DataHandler ( source ) );
            messageBodyPart.setFileName ( fileName );
            multipart.addBodyPart ( messageBodyPart );

            message.setContent ( multipart );

            Transport.send ( message );
            System.out.println ( "Message send successfully...." );
        } catch ( Exception me ) {
            me.printStackTrace ();
        }
    } 

    private void performTask () {
        sendMail ( from, to, subject, messageBody, fileName );
    }

    public static void main ( String[] args ) {
        new SendMailExample ().performTask ();
    }
}

/**
  * SimpleAuthenticator is used to do simple authentication
  * when the SMTP server requires it.
  */

class SMTPAuthenticator extends Authenticator {

    private static final String SMTP_AUTH_USER = "example@gmail.com";
    private static final String SMTP_AUTH_PASSWORD = "somepassword";

    public PasswordAuthentication getPasswordAuthentication () {
        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PASSWORD;

        return new PasswordAuthentication( username,  password );
    }
}

您只需要这个mail.jar 文件。

要编译,只需编写(mail.jar 位于 C:\install\java\mail\mail.jar` 位置):

javac -classpath .;C:\install\java\mail\mail.jar SendMailExample.java

要运行,写:

java -classpath .;C:\install\java\mail\mail.jar SendMailExample

这样就可以了:-)

【讨论】:

    【解决方案2】:

    看看这个库 Commons Email 它会简化你的任务

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 2018-05-10
      • 2011-08-06
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      相关资源
      最近更新 更多