Java Simple Email Program in Eclipse

前端 未结 2 494
再見小時候
再見小時候 2021-01-29 12:33

I want to make a simple program where you can send email from the Command Line. I found this tutorial, \' http://www.tutorialspoint.com/java/java_sending_em

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-29 13:14

    Do have a look at this example. This example, simply sends one attachment as a mail. The contents of attachment quiz.txt are as follows:

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

    Here is the SendMailExample.java file:

    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 = "

    HAVE FAITH, AND STAY" + " CALM :-) I AM WITH YOU, OKAY :-)

    "; 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 ); } }

    You simply needed this mail.jar file.

    To compile, simply write ( mail.jar is present at C:\install\java\mail\mail.jar` location ):

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

    To run, write:

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

    THis will do :-)

提交回复
热议问题