mvn install:install-file -DgroupId=javax.mail \
-DartifactId=mail -Dversion=1.4.1 -Dpackaging=jar \
-Dfile=mail.jar
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
package com.hddigitalworks;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class HelloToMe {
public static void main(String... args) throws Exception {
String[] recipients = {"someone@somewhere.com", "someoneelse@somewhereelse.com"};
String subject = "重要消息 From Master Mo";
String body = "This is a very important message\n" +
"這是一則非常重要消息。\n" +
"Это будет очень важное сообщение.";
sendViaGMail(recipients, subject, body);
}
public static void sendViaGMail(String recipients[],
String subject,
String body) throws MessagingException {
boolean debug = true;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "you@here.com");
props.put("mail.smtp.auth", "true");
if (debug) {
props.put("mail.debug", "true");
}
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
// your google account name (without the '@gmail.com' part!)
final String username = "google-sign-in";
final String password = "your-password";
Authenticator auth = new Authenticator() {
@Override protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
MimeMessage email = new MimeMessage(session);
email.setSubject(subject, "utf-8");
email.setText(body, "utf-8");
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
email.setRecipients(Message.RecipientType.TO, addressTo);
Transport.send(email);
}
}
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
August 2005 September 2005 October 2005 December 2005 January 2006 February 2006 March 2006 April 2006 December 2006 January 2008 February 2008 March 2008 October 2009
Subscribe to Comments [Atom]