last updated: march 2nd 2007
Sending SMPT email in Java example requires:
- Java's javax.mail API
- JavaBeans Activation Framework (part of Java EE 5 and Java SE 6)
tested on Mac OS X (10.4.8), but should work on any platform supporting Java JDK 1.4 or higher
Example.java
/*
A simple javax.mail example showing how to send smtp email using .Mac
and Gmail accounts.
Released under permissive non-copyleft free software license, compatible
with the GNU GPL
Copyright (C) 2007 gerard ziemski
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL gerard ziemski BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
-- requires:
javax.mail API:
http://java.sun.com/products/javamail/downloads/index.html
JavaBeans Activation Framework:
http://java.sun.com/products/javabeans/jaf/downloads/index.html
-- compile and run commands below assume you put the jar files in the
same folder as Example.java (ideally you want them in your Java lib
extension folder):
javac -extdirs . Example.java
java -cp mail.jar:activation.jar:. Example
-- don't forget to set "smtpUser" and "smtpPassword" before running the
example!
*/
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class Example
{
static boolean useMac = false; // either .Mac or Gmail account
static boolean debug = true; // debug for nice spewage
public static void main(String args[]) throws Exception
{
new Example();
}
public Example() throws Exception
{
String smtpHost = null;
String smtpUser = null;
String smtpPassword = null;
int smtpPort = 587;
String from = null;
if (this.useMac == true)
{
// .Mac account
smtpHost = "smtp.mac.com";
smtpUser = "username";
smtpPassword = "password";
from = smtpUser + "@mac.com";
}
else
{
// Gmail account
smtpHost = "smtp.gmail.com";
smtpUser = "username" + "@gmail.com";
smtpPassword = "password";
from = smtpUser;
}
String to = from;
String subject = "test";
String bodytext = "this is a test";
Properties props = System.getProperties();
if (this.debug == true)
{
props.put("mail.debug", "true");
}
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", String.valueOf(smtpPort));
props.put("mail.smtp.starttls.enable", "true"); // required by GMAIL
Session session = Session.getDefaultInstance(props, new SMTPAuthenticator(smtpUser, smtpPassword));
MimeMessage email = new MimeMessage(session);
email.setHeader("X-Mailer", "JavaMail");
email.setContent(bodytext, "text/plain");
email.setSentDate(new java.util.Date());
email.setFrom(new InternetAddress(from));
email.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
email.setSubject(subject);
email.setText(bodytext);
email.saveChanges();
Transport tr = session.getTransport("smtp");
try
{
tr.connect();
tr.sendMessage(email, email.getAllRecipients());
}
catch (Exception e)
{
System.err.println("caught exception="+e);
}
finally
{
tr.close();
}
}
class SMTPAuthenticator extends Authenticator
{
PasswordAuthentication auth;
public SMTPAuthenticator(String usr, String pswd)
{
this.auth = new PasswordAuthentication(usr, pswd);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return this.auth;
}
}
}Thanks to Markus Gebhard for his excellent Java2Html tool.
contact info:
gziemski
@
mac
.
com