|
| 1 | +package sporadic; |
| 2 | + |
| 3 | +import java.util.Properties; |
| 4 | + |
| 5 | +import javax.mail.Message; |
| 6 | +import javax.mail.MessagingException; |
| 7 | +import javax.mail.Session; |
| 8 | +import javax.mail.Transport; |
| 9 | +import javax.mail.internet.InternetAddress; |
| 10 | +import javax.mail.internet.MimeMessage; |
| 11 | + |
| 12 | +//TODO: make this program work! |
| 13 | + |
| 14 | +/** |
| 15 | + * To send an e-mail using your Java Application is simple enough but to start |
| 16 | + * with you should have JavaMail API and Java Activation Framework (JAF) |
| 17 | + * installed on your machine. |
| 18 | + * |
| 19 | + * [DONE] You can download latest version of JavaMail (Version 1.2) from Java's |
| 20 | + * standard website. |
| 21 | + * |
| 22 | + * [DONE] You can download latest version of JAF (Version 1.1.1) from Java's |
| 23 | + * standard website. |
| 24 | + * |
| 25 | + * Download and unzip these files, in the newly created top level directories |
| 26 | + * you will find a number of jar files for both the applications. You need to |
| 27 | + * add mail.jar and activation.jar files in your CLASSPATH. |
| 28 | + * |
| 29 | + * Send a Simple E-mail: Here is an example to send a simple e-mail from your |
| 30 | + * machine. |
| 31 | + * |
| 32 | + * Here it is assumed that your localhost is connected to the internet and |
| 33 | + * capable enough to send an email. |
| 34 | + */ |
| 35 | + |
| 36 | +// This app is not running now, I guess it's b/c my localhost is not connected |
| 37 | +// to Internet yet. I need to figure this out. |
| 38 | +public class SendEmail { |
| 39 | + |
| 40 | + public static void main(String[] args) { |
| 41 | + // Recipient's email ID needs to be mentioned. |
| 42 | + String to = "sunjiahuan@gmail.com"; |
| 43 | + |
| 44 | + // Sender's email ID needs to be mentioned |
| 45 | + String from = "steve.j.sun@gmail.com"; |
| 46 | + |
| 47 | + // Assuming you are sending email from localhost |
| 48 | + String host = "10.34.128.145"; |
| 49 | + |
| 50 | + // Get system properties |
| 51 | + Properties properties = System.getProperties(); |
| 52 | + |
| 53 | + // Setup mail server |
| 54 | + properties.setProperty("mail.smtp.host", host); |
| 55 | + |
| 56 | + // Get the default Session object. |
| 57 | + Session session = Session.getDefaultInstance(properties); |
| 58 | + |
| 59 | + try { |
| 60 | + // Create a default MimeMessage object. |
| 61 | + MimeMessage message = new MimeMessage(session); |
| 62 | + |
| 63 | + // Set From: header field of the header. |
| 64 | + message.setFrom(new InternetAddress(from)); |
| 65 | + |
| 66 | + // Set To: header field of the header. |
| 67 | + message.addRecipient(Message.RecipientType.TO, new InternetAddress( |
| 68 | + to)); |
| 69 | + |
| 70 | + // Set Subject: header field |
| 71 | + message.setSubject("This is the Subject Line!"); |
| 72 | + |
| 73 | + // Now set the actual message |
| 74 | + message.setText("This is actual message"); |
| 75 | + |
| 76 | + // Send message |
| 77 | + Transport.send(message); |
| 78 | + System.out.println("Sent message successfully...."); |
| 79 | + } catch (MessagingException mex) { |
| 80 | + mex.printStackTrace(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments