Created
October 4, 2011 01:57
-
-
Save freeeve/1260739 to your computer and use it in GitHub Desktop.
JavaMail simple text sample
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
import java.util.*; | |
import javax.mail.*; | |
import javax.mail.internet.*; | |
import javax.activation.*; | |
// Send a simple, single part, text/plain e-mail | |
public class TestEmail { | |
public static void main(String[] args) { | |
String to = "sendToMailAddress"; | |
String from = "sendFromMailAddress"; | |
String host = "infoa.com"; | |
Properties props = new Properties(); | |
props.put("mail.smtp.host", host); | |
Session session = Session.getInstance(props); | |
try { | |
Message msg = new MimeMessage(session); | |
msg.setFrom(new InternetAddress(from)); | |
InternetAddress[] address = {new InternetAddress(to)}; | |
msg.setRecipients(Message.RecipientType.TO, address); | |
msg.setSubject("test email subject"); | |
// Set message body | |
msg.setText("test email body.\n" + | |
"test email body line 2."); | |
Transport.send(msg); | |
} | |
catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment