java学习之路之邮件相关

smtp 发送邮件 25端口

pop3接受邮件 110端口

============手动发送邮件==============================
D:\Documents and Settings\park>telnet smtp.sina.cn 25
ehlo park

auth login

aXRoZWltYV9wYXJrQHNpbmEuY24=

cXExMjMzMjE=

mail from:[email protected]

rcpt to:[email protected]

Data

from:[email protected]
to:[email protected]
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("[email protected]"));
    msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
    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("[email protected]"));
    msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
    msg.setSubject("这是我写的标题XXXXXXXXXX");
    msg.setText("这是邮件的正文");

    Transport tran = session.getTransport();
    tran.connect("itheima_park", "qq123321");
    tran.sendMessage(msg,msg.getAllRecipients());
}

你可能感兴趣的:(java学习之路之邮件相关)