JAVA MAIL开发


package cn.gshs.util;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

public class EmailSend {

//发送邮件
public static void sendMail() {
//String host = "192.168.1.98"; // 指定的smtp服务器,本机的局域网IP
String host = "smtp.live.com"; // 本机smtp服务器
//String host = "smtp.163.com"; // 163的smtp服务器
String from = "chenmeng@gseshop.com.cn"; // 邮件发送人的邮件地址
String to = "chenmeng.4270@yahoo.com.cn"; // 邮件接收人的邮件地址
final String username = "gochenmeng@hotmail.com"; //发件人的邮件帐户
final String password = "844270"; //发件人的邮件密码

// 创建Properties 对象
Properties props = System.getProperties();

// 添加smtp服务器属性
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.starttls.enable","true");
props.setProperty("mail.smtp.port", "25");
// props.setProperty("mail.smtp.socketFactory.port", "465");

// 创建邮件会话
Session session = Session.getDefaultInstance(props, new Authenticator(){
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}

});

try {
// 定义邮件信息
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
//message.setSubject(transferChinese("我有自己的邮件服务器了"));
message.setSubject("I hava my own mail server");
message.setText("From now, you have your own mail server, congratulation!");

// 发送消息
session.getTransport("smtp").send(message);
//Transport.send(message); //也可以这样创建Transport对象发送
System.out.println("SendMail Process Over!");

} catch (MessagingException e) {
e.printStackTrace();
}
}

//接受邮件
public static void getMail(){
String host = "210.51.54.164";
final String username = "chenmeng";
final String password = "844270";

// 创建Properties 对象
Properties props = new Properties();

// 创建邮件会话
Session session = Session.getDefaultInstance(props, new Authenticator(){
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}

});


try {
// 获取邮箱的pop3存储
Store store = session.getStore("pop3");
store.connect(host, username, password);

// 获取inbox文件
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY); //打开,打开后才能读取邮件信息

// 获取邮件消息
Message message[] = folder.getMessages();

for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ": " + message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
try {
message[i].writeTo(System.out);
} catch (IOException e) {
e.printStackTrace();
}

}

// 关闭资源
folder.close(false);
store.close();

} catch (MessagingException e) {
e.printStackTrace();
}

System.out.println("GetMail Process Over!");

}

//邮件主题中文字符转换
public static String transferChinese(String strText){
try{
strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");
}catch(Exception ex){
ex.printStackTrace();
}
return strText;
}

public static void main(String[] args) {
EmailSend.sendMail();
//EmailSend.getMail();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值