applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="sender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.sina.com.cn" />
<property name="username" value="yaobo2816" />
<property name="password" value="*******" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
</beans>
接收邮件
package com; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeUtility; public class JavaMailSslReceiver { public static void main(String argv[]) throws Exception { Properties props = new Properties(); Session session = Session.getDefaultInstance(props); URLName urln = new URLName("pop3", "pop3.sina.com.cn", 110, null, "yaobo2816", "******"); Store store = session.getStore(urln); Folder inbox = null; try { store.connect(); inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.ENVELOPE); Message[] msgs = inbox.getMessages(); inbox.fetch(msgs, profile); System.out.println("收件箱的邮件数:" + msgs.length); for (int i = 0; i < msgs.length; i++) { String from = msgs[i].getFrom()[0].toString(); InternetAddress ia = new InternetAddress(from); System.out.println("-----------------------------"); System.out.println("发送者:" + ia.getPersonal() + "/" + ia.getAddress()); System.out.println("标题:" + msgs[i].getSubject()); System.out.println("大小:" + msgs[i].getSize()); System.out.println("时间:" + msgs[i].getSentDate()); } } finally { try { inbox.close(false); } catch (Exception e) { } try { store.close(); } catch (Exception e) { } } } protected static String decodeText(String text) throws UnsupportedEncodingException { if (text == null) return null; if (text.startsWith("=?GB") || text.startsWith("=?gb")) text = MimeUtility.decodeText(text); else text = new String(text.getBytes("ISO8859_1")); return text; } }
发邮件 简单
public void sendSimpleMail2(int userId) { SimpleMailMessage msg = new SimpleMailMessage(); msg.setFrom("yaobo2816@sina.com"); msg.setTo("yaobo2816@sina.com"); msg.setReplyTo("masterspring2@sina.com"); msg.setCc("masterspring2@163.com"); msg.setSubject("注册成功"); msg.setText("恭喜,您在宝宝淘论坛已经注册成功!您的用户ID为:" + userId); System.out.println(msg.getFrom()); ApplicationContext factory = new ClassPathXmlApplicationContext(new String[]{"/applicationContext.xml"}); JavaMailSender sender = (JavaMailSender)factory.getBean("sender"); sender.send(msg); System.out.println("发送成功"); }
发邮件 带附件
public void sendAttachmentMail() throws Exception { ApplicationContext factory = new ClassPathXmlApplicationContext(new String[]{"/applicationContext.xml"}); JavaMailSender sender = (JavaMailSender)factory.getBean("sender"); MimeMessage msg = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8"); helper.setFrom("yaobo2816@sina.com"); helper.setTo("yaobo2816@sina.com"); helper.setSubject("注册成功"); helper.setText("欢迎访问宝宝淘论坛!"); ClassPathResource file1 = new ClassPathResource("bbt.zip"); helper.addAttachment("file01.zip", file1.getFile()); ClassPathResource file2 = new ClassPathResource("file.doc"); helper.addAttachment("file02.doc", file2.getFile()); sender.send(msg); System.out.println("成功"); }
要用的包
activation.jar
mail.jar
spring.jar