JavaMail簡単メール送信
2050 ワード
package cn.jmail.test;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class FirstMail {
/**
*
* @param host IP
* @param from
* @param to
* @param subject
* @param text
* @param senderUsername
* @param senderPassword
* mail.smtp.auth
*/
public static void sendMail(String host, String from, String to, String subject, String text,
final String senderUsername, final String senderPassword){
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(senderUsername, senderPassword);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(text);
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String host = "smtp.163.com";
String from = "[email protected]";
String to = "[email protected]";
String subject = "Hello, this is a test email.";
String text = "Hello,LiLei.";
String senderUsername = "[email protected]";
String senderPassword = "xxxxxx";
FirstMail.sendMail(host, from, to, subject, text, senderUsername, senderPassword);
}
}
ええ、もちろんmailが少なくないわけではありません.jar