Advertisement
Advertisement
| 06.29.2008 at 10:42PM PDT, ID: 23525959 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: |
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Email {
private Session getMailSession() throws Exception {
Properties mailProperties = new Properties();
Authenticator auth = new SMTPAuthenticator();
mailProperties.setProperty("mail.transport.protocol", "smtp");
mailProperties.setProperty("mail.smtp.host", SMTP_HOST);
mailProperties.put("mail.smtp.port", "25");
mailProperties.put("mail.smtp.auth", "true");
return Session.getInstance(mailProperties, auth);
}
public void sendmail() throws Exception {
//MimeMessage msg = new MimeMessage(getMailSession());
Session mailSession = (Session)getMailSession();
mailSession.setDebug(true);
//MimeMessage msg = new MimeMessage(getMailSession());
MimeMessage msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(SENDER_ADDRESS));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(RECIPIENT_ADDRESS));
msg.setSubject("test mail subject");
msg.setText("test mail text");
Transport.send(msg);
}
public static void main(String[] av) {
Email mail = new Email();
try {
mail.sendmail();
System.out.println("mail send successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
class SMTPAuthenticator extends javax.mail.Authenticator {
private final String SMTP_AUTH_USER = USERNAME;
private final String SMTP_AUTH_PWD = PASSWORD;
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
|