您的位置:首页 > 编程语言 > Java开发

java学习之路之邮件相关

2016-05-04 23:41 555 查看
smtp 发送邮件 25端口

pop3接受邮件 110端口

============手动发送邮件==============================

D:\Documents and Settings\park>telnet smtp.sina.cn 25

ehlo park

auth login

aXRoZWltYV9wYXJrQHNpbmEuY24=

cXExMjMzMjE=

mail from:itheima_park@sina.cn

rcpt to:itheima_park@sohu.com

Data

from:itheima_park@sina.cn

to:itheima_park@sohu.com

subject: 测试邮件

xxx 自作主张

.

quit

=============Base64编码===============================

public class Base64 {

public static void main(String[] args) throws Exception {

System.out.print(“请输入用户名:”);

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String userName = in.readLine();

System.out.print(“请输入密码:”);

String password = in.readLine();

BASE64Encoder encoder = new BASE64Encoder();

System.out.println(“编码后的用户名为:”

+ encoder.encode(userName.getBytes()));

System.out.println(“编码后的密码为:”

+ encoder.encode(password.getBytes()));

}

}

==============JavaMail发送简单邮件-=====================

@Test
public void test1() throws Exception, MessagingException{
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.host", "localhost");


// prop.setProperty(“mail.smtp.auth”, “true”);

prop.setProperty(“mail.debug”, “true”);

Session session = Session.getInstance(prop);

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("aa@park.cn"));
msg.setRecipient(RecipientType.TO, new InternetAddress("bb@park.cn"));
msg.setSubject("来自javaMail的测试邮件!");
msg.setText("来自javaMail的正文内容。。");

Transport tran = session.getTransport();
tran.connect("aa", "123");
tran.send(msg, msg.getAllRecipients());
}


=========================================================

public static void main(String[] args) throws MessagingException {

Properties prop = new Properties();

prop.setProperty(“mail.transport.protocol”, “smtp”);

prop.setProperty(“mail.smtp.host”, “smtp.sina.cn”);

prop.setProperty(“mail.smtp.auth”, “true”);

prop.setProperty(“mail.debug”, “true”);

Session session = Session.getInstance(prop);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("itheima_park@sina.cn"));
msg.setRecipient(RecipientType.TO, new InternetAddress("itheima_park@sohu.com"));
msg.setSubject("这是我写的标题XXXXXXXXXX");
msg.setText("这是邮件的正文");

Transport tran = session.getTransport();
tran.connect("itheima_park", "qq123321");
tran.sendMessage(msg,msg.getAllRecipients());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: