import org.apache.commons.mail.Email
import org.apache.commons.mail.EmailException
import org.apache.commons.mail.SimpleEmail
public class SimpleEmailClient {
public static void main(String[] args) {
Email email = new SimpleEmail()
email.setSSLOnConnect(true)
try {
email.setHostName("smtp.163.com")//设置邮件的服务器地址
email.setSmtpPort(25)//邮件的端口,如果是SSL连接,端口为
email.setAuthentication("username", "password")// your mail's address and password
email.setFrom("your mail address")
email.setSubject("TestMail")
email.setMsg("This is a test mail ... :-)")
email.addTo("destination mail address")
String result = email.send()
System.out.println("Send mail status:" + result)
} catch (EmailException e) {
e.printStackTrace()
}
}
}
需要下载电子邮箱。打开电子邮箱,点击注册Xbox Live。他会发信息到你手机 , 输入验证码你就可以设置密码 ,然后注册完成 再关联你的电子邮箱就可以了。
电子邮件是—种用电子手段提供信息交换的通信方式,是互联网应用最广的服务。通过网络的电子邮件系统,用户可以以非常低廉的价格(不管发送到哪里,都只需负担网费)、非常快速的方式(几秒钟之内可以发送到世界上任何指定的目的地),与世界上任何一个角落的网络用户联系。
/**serverName 接收邮件地址
user 用户信息
pwd 密码
path 邮件临时储存路径
max 每次接收邮件的最大数量
**/
public int receive(String serverName, String user, String pwd, String path, int max) throws Exception {
//新建ExchangeVersion.Exchange2007_SP1版本的Exchange服务
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1)
String[] userInfo = user.split("/")
//用户认证信息
ExchangeCredentials credentials = new WebCredentials(userInfo[1], pwd,userInfo[0])
service.setCredentials(credentials)
//设置Exchange连接的服务器地址
service.setUrl(new URI(serverName))
//绑定邮箱
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox)
//获取邮箱文件数量
int count = inbox.getTotalCount()
if(max >0) count = count >max ? max : count
//循环获取邮箱邮件
ItemView view = new ItemView(count)
FindItemsResults<Item>findResults = service.findItems(inbox.getId(), view)
for (Item item : findResults.getItems()) {
EmailMessage message = EmailMessage.bind(service, item.getId())
List<Attachment>attachs = message.getAttachments().getItems()
try{
if(message.getHasAttachments()){
for(Attachment f : attachs){
if(f instanceof FileAttachment){
//接收邮件到临时目录
File tempZip = new File(path,f.getName())
((FileAttachment)f).load(tempZip.getPath())
}
}
//删除邮件
message.delete(DeleteMode.HardDelete)
}
}catch(Exception err){
log.equals(err)
}
}
return count
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)