注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

Send or fetch mail using java  

2012-01-06 22:56:13|  分类: javamail |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Maven dependencies:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.2</version>
</dependency>
<!-- Java Mail API -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.5-SNAPSHOT</version>
</dependency>

 

public static void prepareMail(Email email) throws EmailException {
email.setHostName("smtp.163.com");
email.setSmtpPort(25);
email.setDebug(true);
email.setAuthenticator(new DefaultAuthenticator("xxx", "***"));
email.addTo("yyq2008@gmail.com", "Daniel Yang");
email.setFrom("xxx@163.com", "Me");
email.setCharset("utf-8");
}

 

Send a simple mail:     

public static void sendSimpleEmail() throws EmailException {
Email email = new SimpleEmail();
prepareMail(email);
email.setSubject("Test");
email.setMsg("This is a test mail!");
email.send();
System.out.println("OK!");
}

Send a html mail:

public static void sendHtmlEmail() throws EmailException, MalformedURLException {
// Create the email message
HtmlEmail email = new HtmlEmail();
prepareMail(email);
email.setSubject("Test email with inline image");
// embed the image and get the content id
URL url = new URL("http://www.apache.org/images/feather-small.gif");
String cid = email.embed(url, "Apache logo");
// set the html message
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid + "\"></html>");
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
}


Send a mail with remote attachment:

public static void sendRemoteAttachmentEmail() throws EmailException, MalformedURLException {
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL("http://www.apache.org/images/feather-small.gif"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Apache logo");
attachment.setName("Apache logo");

// Create the email message
MultiPartEmail email = new MultiPartEmail();
prepareMail(email);
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// add the attachment
email.attach(attachment);

// send the email
email.send();

}


Send a mail with local attachment

public static void sendLocalAttachmentEmail() throws EmailException {
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("c:/1.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("John");

// Create the email message
MultiPartEmail email = new MultiPartEmail();
prepareMail(email);
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// add the attachment
email.attach(attachment);
// send the email
email.send();
}


Fetch mail from mail server:

public static void loadMail() throws Exception {
String host = "smtp.163.com";
// SUBSTITUTE YOUR USERNAME AND PASSWORD TO ACCESS E-MAIL HERE!!!
String user = "xxx";
String password = "**";
Email email = new SimpleEmail();
prepareMail(email);
Session session = email.getMailSession();
URLName urln = new URLName("pop3", "pop3.163.com", 110, null,
user, password);
Store store = session.getStore(urln);
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message messages[] = folder.getMessages();
System.out.println("Messages's length: " + messages.length);

for (Message message : messages) {
System.out.println(new String(message.getSubject().getBytes("iso-8859-1"), "gb2312"));
System.out.println(message.getContentType());
System.out.println(message.getContent());
System.out.println("-----------------------------------");
}
folder.close(true);
store.close();
}

 
Socks proxy:
//        System.setProperty("mail.smtp.socks.host","localhost");
// System.setProperty("mail.smtp.socks.port","5555");
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.163.com");
email.setSmtpPort(25);
email.setDebug(true);
email.setAuthenticator(new DefaultAuthenticator("yyq2012aa", "111111"));
email.getMailSession().getProperties().setProperty("mail.smtp.socks.host","localhost");
email.getMailSession().getProperties().setProperty("mail.smtp.socks.port","5555");
email.addTo("daniely@synnex.com", "Daniel Yang");
email.setFrom("yyq2012aa@163.com", "JB6 module check ERROR");
email.setCharset("utf-8");
email.setSubject("JB6 module check ERROR");
email.setHtmlMsg("<html><b>Test email</b></html>");
email.setTextMsg("Your email client does not support HTML messages");
email.send();

 Debug information when sending simple mail:

DEBUG: JavaMail version 1.4.5-SNAPSHOT
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.163.com", port 25, isSSL false
220 163.com Anti-spam GT for Coremail System (163com[20111010])
DEBUG SMTP: connected to host "smtp.163.com", port: 25

EHLO 192.168.1.100
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFoYWs7UCa0xFrVVVVj
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "coremail", arg "1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFoYWs7UCa0xFrVVVVj"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
AUTH LOGIN
334 dXNlcm5hbWU6
eXlxMjAwN2Fh
334 UGFzc3dvcmQ6
amluZ2ppbmcyMDEx
235 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:<yyq2007aa@163.com>
250 Mail OK
RCPT TO:<yyq2008@gmail.com>
250 Mail OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP: Daniel Yang <yyq2008@gmail.com>
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Thu, 4 Dec 2008 03:23:52 +0800 (CST)
From: Me <yyq2007aa@163.com>
To: Daniel Yang <yyq2008@gmail.com>
Message-ID: <26426059.0.1228332232297.JavaMail.Administrator@LBWin7>
Subject: Test
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

This is a test mail!
.
250 Mail OK queued as smtp2,DNGowED5BkviCgdP6kbgAg--.915S2 1325861603
QUIT
221 Bye
OK!

 
  评论这张
 
阅读(1035)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018