邮件发送程序
我们先使用JavaMail API发送一封简单的邮件,步骤为
创建包含邮件服务器的网络连接信息的Session对象。 创建代表邮件内容的Message对象。 创建Transport对象、连接服务器、发送Message、关闭连接。
这样使用JavaMail API发送一封最简单的邮件(纯文本邮件)的代码为:
public class SendMail {
public static void main (String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.smtp.host" , "smtp.163.com" );
prop.setProperty("mail.transport.protocol" , "smtp" );
prop.setProperty("mail.smtp.auth" , "true" );
Session session = Session.getInstance(prop);
session.setDebug(true );
Message message = createMessage(session);
Transport ts = session.getTransport();
ts.connect("yerenyuan10001" , "邮箱登录密码" );
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
private static Message createMessage (Session session) throws AddressException, MessagingException, UnsupportedEncodingException {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("yerenyuan10001@163.com" ));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("i_beautifulman@sina.com" ));
message.setSubject("love" );
message.setContent("hello hello liayun" , "text/html" );
message.saveChanges();
return message;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
提示:创建出包含邮件服务器的网络连接信息的Session对象之后,为了看清JavaMail这套API到底是如何向邮件服务器发送邮件的,可以把session的Debug开关打开,把这个调试开关打开后,JavaMail这套API会把它与邮件服务器的交互过程打印在Eclipse的控制台上。 运行以上程序,然后登陆到i_beautifulman@sina.com邮箱当中,就可以收取到由yerenyuan10001@163.com发送的Email了,如下图所示: 接着我们使用JavaMail API发送一封复杂邮件,如下:
public class SendMail {
public static void main (String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.smtp.host" , "smtp.163.com" );
prop.setProperty("mail.transport.protocol" , "smtp" );
prop.setProperty("mail.smtp.auth" , "true" );
Session session = Session.getInstance(prop);
session.setDebug(true );
Message message = createMessage(session);
Transport ts = session.getTransport();
ts.connect("yerenyuan10001" , "邮箱登录密码" );
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
private static Message createMessage (Session session) throws AddressException, MessagingException, UnsupportedEncodingException {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("yerenyuan10001@163.com" ));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("i_beautifulman@sina.com" ));
message.setSubject("我爱你,那个人" );
MimeBodyPart text = new MimeBodyPart();
text.setContent("我是真的爱你,你知道吗?<img src='cid:1.jpg'>" , "text/html;charset=UTF-8" );
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src/1.jpg" )));
image.setContentID("1.jpg" );
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src/爱的秘密蓝调口琴曲.mp3" ));
attach.setDataHandler(dh);
attach.setFileName(MimeUtility.encodeText(dh.getName()));
MimeMultipart content = new MimeMultipart();
content.addBodyPart(text);
content.addBodyPart(image);
content.setSubType("related" );
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content);
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(mbp);
mm.addBodyPart(attach);
mm.setSubType("mixed" );
message.setContent(mm);
message.saveChanges();
return message;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
运行以上程序,然后登陆到i_beautifulman@sina.com邮箱当中,就可以收取到由yerenyuan10001@163.com发送的Email了,