ssh 发送邮件

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>


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值