參考至 這裡
前言 :
Here’s two examples to show how to use JavaMail API method to send an email via Gmail SMTP server using both TLS and SSL connection.
JavaMail – GMail via TLS :
Sending an email via Gmail SMTP server using TLS connection. 範例代碼如下 :
JavaMail – GMail via SSL :
Sending an email via Gmail SMTP server using SSL connection. 範例代碼如下 :
補充說明 :
* JavaMail API – Sending email via Gmail SMTP example
* 利用JavaMail 收/發 Gmail 郵件(SSL)
前言 :
Here’s two examples to show how to use JavaMail API method to send an email via Gmail SMTP server using both TLS and SSL connection.
JavaMail – GMail via TLS :
Sending an email via Gmail SMTP server using TLS connection. 範例代碼如下 :
- GmailApp1.java : Using TLS to send Gmail
- package john.mail;
- 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 GmailApp1 {
- public static void main(String args[]) {
- String host = "smtp.gmail.com";
- int port = 587;
- final String username = "puremonkey2007@gmail.com";
- final String password = "your password";
- 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);
- message.setFrom(new InternetAddress("puremonkey2007@gmail.com"));
- 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 transport = session.getTransport("smtp");
- transport.connect(host, port, username, password);
- Transport.send(message);
- System.out.println("Done");
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- }
- }
JavaMail – GMail via SSL :
Sending an email via Gmail SMTP server using SSL connection. 範例代碼如下 :
- GmailApp2.java : Send Gmail through SSL
- package john.mail;
- 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);
- }
- }
- }
補充說明 :
* JavaMail API – Sending email via Gmail SMTP example
* 利用JavaMail 收/發 Gmail 郵件(SSL)
Gmail目前已经启用了POP3和SMTP服务,与其他邮箱不同的是Gmail提供的POP3和SMTP是使用安全套接字层SSL的,因此常规的JavaMail程序是无法收发邮件的,下面是使用JavaMail如何收取Gmail邮件以及发送邮件的代码...
This message was edited 6 times. Last update was at 08/12/2010 17:02:29
沒有留言:
張貼留言