Created
August 1, 2013 14:08
-
-
Save dlwhitehurst/6131691 to your computer and use it in GitHub Desktop.
Very simple Java emailer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.dlw.mail; | |
import java.util.Properties; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Session; | |
import javax.mail.Transport; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeMessage; | |
/** | |
* @author david.whitehurst | |
* | |
*/ | |
public class SmtpGrunt { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
sendMessage(); | |
} | |
public static void sendMessage() { | |
// Recipient's email | |
String to = "xyz@yahoo.com"; | |
// Sender's email | |
String from = "abc@yahoo.com"; | |
// SMTP Relay Host | |
String host = "smtp.yahoo.com"; | |
// Get system properties | |
Properties properties = System.getProperties(); | |
// Setup mail server | |
properties.setProperty("mail.smtp.host", host); | |
// Get the default Session object. | |
Session session = Session.getDefaultInstance(properties); | |
try{ | |
// Create a default MimeMessage object. | |
MimeMessage message = new MimeMessage(session); | |
// Set From: header field of the header. | |
message.setFrom(new InternetAddress(from)); | |
// Set To: header field of the header. | |
message.addRecipient(Message.RecipientType.TO, | |
new InternetAddress(to)); | |
// Set Subject: header field | |
message.setSubject("Test Email from ABC"); | |
// Now set the actual message | |
message.setText("This is actual message."); | |
// Send message | |
Transport.send(message); | |
System.out.println("Sent message successfully...."); | |
}catch (MessagingException mex) { | |
mex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment