简单到就几句话,你你还没有记就记住了:
public class SimpleMailSender {
public static void main(String[] args) {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
//设置发送Email Server
sender.setHost("mail.hfyefan.com");
sender.setUsername("xiaohaizi");
sender.setPassword("222515");
//如果Email Server需要验证要加上以下代码
Properties prop = new Properties();
prop.setProperty("mail.hfyefan.com", "true");
prop.setProperty("mail.hfyefan.com", "20000");
sender.setJavaMailProperties(prop);
//建立邮件消息
SimpleMailMessage mailMessage = new SimpleMailMessage();
//设置收件人、寄件人、主题与正文
mailMessage.setTo("");
mailMessage.setFrom("");
mailMessage.setSubject("Send Simple Email Test");
mailMessage.setText("Hi, I am brj! This is a test email.");
//传送邮件
sender.send(mailMessage);
System.out.println("简单邮件发送成功!");
}
}
之所以叫简单邮件,就这样简单,没了,不过
要导入Spring 2.0 Music Libraries
其实像收发邮件人这些东西,都应该用spring的注入,
注入我们会在后面讲到,比如:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.126.com"></property>
<property name="username" value="lokis"></property>
<property name="password" value="****"></property>
<!-- 如果服务器要求验证,加上此 -->
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">20000</prop>
</props>
</property>
</bean>