ssh 发送邮件
来源:互联网 发布:mac重装系统磁盘解锁 编辑:程序博客网 时间:2024/04/30 09:50
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;public class SendMailTest { /** * @param args */ public static void main(String[] args) { //************// /* 内网使用 springframework mail 发送 mail * 收件人必须和 host 在同一网段才行 * *************/// JavaMailSenderImpl mail = new JavaMailSenderImpl();// mail.setHost("192.168.218.10");// mail.setPort(25);// MimeMessage mailMessage = mail.createMimeMessage();// MimeMessageHelper messageHelper = null;// try {// messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");// messageHelper.setFrom("youjianbo_han_87@hotmail.com");// messageHelper.setTo(new String[]{"lingchen@synnex.com.tw"});// // messageHelper.setSubject("mail test");// messageHelper.setText("hi i am springfremawork mail test", false);// //messageHelper.addAttachment("附件", new File(""));// /* send email */// mail.send( mailMessage );// System.out.println("发送成功");// } catch (Exception ex) // {// ex.printStackTrace();// } /**-----------------外网发送mail----------------**/ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender"); SimpleMailMessage mail = new SimpleMailMessage(); mail.setFrom("abcd@163.com"); mail.setTo("abcd@gmail.com"); mail.setSubject(" 测试spring Mail"); mail.setText("hello,java"); mailSender.send(mail); }}
<?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="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.163.com" /> <property name="port" value="25" /> <property name="username" value="abcd@163.com" /> <property name="password" value="你的密码" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> </beans>