摘要:java mail
>>>>>>>>>>>>>>>>>>>>>>>>>>use cht hot>>>>>>>>>>>>>>>>>>>>>>>>>>
public class GmailApp1 {
/**
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String host = "ms.cht.com.tw";
int port = 25;
final String username = "rickeysu";
final String password = "1qaz@WSX";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", port);
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
String encoding = "ISO8859_1";
InternetAddress[] mailAddrs = new InternetAddress[2];
// mailAddrs[0] = new InternetAddress("rickeysue@gmail.com","gmail",encoding);
// mailAddrs[1] = new InternetAddress("rickeysu@cht.com.tw","gmail2",encoding);
mailAddrs[0] = new InternetAddress("rickeysue@gmail.com");
mailAddrs[1] = new InternetAddress("rickeysu@cht.com.tw");
message.setFrom(new InternetAddress("rickeysu@cht.com.tw"));
message.setRecipients(Message.RecipientType.TO, mailAddrs);
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,\n\n No spam to my email, please!");
Transport transport = session.getTransport("smtp");
transport.connect(host, port, username, password);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>gmail host>>>>>>>>>>>>>>>>>>>>>>>>>>
- 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 GmailApp2 {
- public static void main(String[] args) {
- final String username = "puremonkey2007@gmail.com";
- final String password = "your password";
- Properties props = new Properties();
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.socketFactory.port", "465");
- props.put("mail.smtp.socketFactory.class",
- "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.port", "465");
- Session session = Session.getInstance(props, new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress("puremonkey2007@gmail.com.tw"));
- message.setRecipients(Message.RecipientType.TO, InternetAddress
- .parse("john_k_lee@trend.com.tw"));
- message.setSubject("Testing Subject");
- message
- .setText("Dear Mail Crawler,\n\n No spam to my email, please!");
- Transport.send(message);
- System.out.println("Done");
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- }
- }