Spring中javaMail通过SMTP发送邮件

参考 http://www.blogjava.net/fancydeepin/category/51935.html

1、如果是maven项目,需要引入依赖包


	javax.mail
	mail
	1.4.4
2、在项目加载的basic.xml中,配置两个bean:JavaMailSenderImpl和SimpleMailMessage,在后续发送邮件中通过这两个类进行发送。

	
        
        	----
        
        
        	----
        
        
        	smtps
        
        
        	----
        
        
        	----
        
        
        
            
                
                true
                true
				smtps
           
        
    
    
    
    	
    		${notify_service.smtp_mail.username}
    	
    	

3、在实际发送的class里面调用
private MailSender mailSender;
	private SimpleMailMessage simpleMailMessage;

//初始化mailSender 和SimpleMailMessage 两个对象

simpleMailMessage.setTo(sendTo);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
mailSender.send(simpleMailMessage);	


另外如果是发送HTML格式的邮件 参考:http://www.blogjava.net/fancydeepin/archive/2012/06/12/spring-smtp-mail-with-attachment_maven3.html

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

private JavaMailSender javaMailSender;
	private MimeMessage mimeMessage;
	private MimeMessageHelper mimeMessageHelper;
	private SimpleMailMessage simpleMailMessage;

try {
                        mimeMessage = javaMailSender.createMimeMessage();
			mimeMessageHelper = new MimeMessageHelper(mimeMessage,true,"GBK");
			mimeMessageHelper.setFrom(simpleMailMessage.getFrom());
			mimeMessageHelper.setTo(sendTo);
			mimeMessageHelper.setSubject(subject);
			mimeMessageHelper.setText(content);
			javaMailSender.send(mimeMessage);
		} catch (MessagingException e) {
			e.printStackTrace();
		}




4、如果不用Spring直接用javamail发送

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.mail.Session;
import javax.mail.MessagingException;
import javax.mail.Transport;

public class SendHtmlMail {
   public void sendmail(){  
          
        //创建邮件发送类 JavaMailSender  
        //用于发送基本的文本邮件信息(不能包括附件,及图片)  
        JavaMailSender sender = new JavaMailSenderImpl();   
          
          
        //设置邮件服务主机  
        ((JavaMailSenderImpl)sender).setHost("smtp.sohu.com");  
        //发送者邮箱的用户名  
        ((JavaMailSenderImpl)sender).setUsername("userName");  
        //发送者邮箱的密码  
        ((JavaMailSenderImpl)sender).setPassword("password");  
          
        //配置文件,用于实例化java.mail.session  
        Properties pro = System.getProperties();  
          
        //登录SMTP服务器,需要获得授权,网易163邮箱新近注册的邮箱均不能授权。  
        //测试 sohu 的邮箱可以获得授权  
        pro.put("mail.smtp.auth", "true");  
        pro.put("mail.smtp.socketFactory.port", "25");  
        pro.put("mail.smtp.socketFactory.fallback", "false");  
          
        //通过文件获取信息  
        ((JavaMailSenderImpl)sender).setJavaMailProperties(pro);  
          
          
        //创建基本邮件信息  
        MailMessage mailMessage = new SimpleMailMessage();  
          
        //发送者地址,必须填写正确的邮件格式,否者会发送失败  
        mailMessage.setFrom("******@sohu.com");  
        //邮件主题  
        mailMessage.setSubject("测试邮件");  
        //邮件内容,简单的邮件信息只能添加文本信息  
        mailMessage.setText("测试");  
        //邮件接收者的邮箱地址  
        mailMessage.setTo("******@qq.com");  
          
          
        //发送邮件,参数可以是数组  
        //sender.send(SimpleMailMessage[])  
        sender.send((SimpleMailMessage)mailMessage);  
          
    }  
}


5、javaMail发送HTML个是的邮件

public void sendMimeMail() throws MessagingException{  
          
        //创建邮件发送类 JavaMailSender  
        //用于发送多元化邮件,包括附件,图片,html 等  
        JavaMailSender mimeMailSender = new JavaMailSenderImpl();  
          
        //设置邮件服务主机  
        ((JavaMailSenderImpl)mimeMailSender).setHost("smtp.sohu.com");  
        //发送者邮箱的用户名  
        ((JavaMailSenderImpl)mimeMailSender).setUsername("userName");  
        //发送者邮箱的密码  
        ((JavaMailSenderImpl)mimeMailSender).setPassword("password");  
          
        //配置文件,用于实例化java.mail.session  
        Properties pro = System.getProperties();  
          
        //登录SMTP服务器,需要获得授权,网易163邮箱新近注册的邮箱均不能授权。  
        //测试 sohu 的邮箱可以获得授权  
        pro.put("mail.smtp.auth", "true");  
        pro.put("mail.smtp.socketFactory.port", "25");  
        pro.put("mail.smtp.socketFactory.fallback", "false");  
          
        ((JavaMailSenderImpl)mimeMailSender).setJavaMailProperties(pro);  
          
        //创建多元化邮件  
        MimeMessage mimeMessage = ((JavaMailSenderImpl)mimeMailSender).createMimeMessage();  
          
        //创建 mimeMessage 帮助类,用于封装信息至 mimeMessage  
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "GBK");   
          
        //基本信息  
          
        //发送者地址,必须填写正确的邮件格式,否者会发送失败  
        helper.setFrom("*******@sohu.com");  
        //邮件主题  
        helper.setSubject("测试邮件");  
        //邮件内容,简单的邮件信息只能添加文本信息  
        helper.setText("测试");  
        //邮件接收者的邮箱地址  
        helper.setTo("******@qq.com");  
          
          
        //多元化信息  
          
        //添加HTML 信息  
        File myPicture = new File("C:\\Documents and Settings\\Administrator\\桌面\\kz2.jpg");  
          
        //如果是html代码,boolean 的参数用该为true  
        helper.setText("

新书快递通知

你的新书快递申请已推送新书,请到空间" + "中查看
", true); //添加图片至HTML 代码中 helper.addInline("myPicture", myPicture); //添加附件 File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\validate.jar"); helper.addAttachment("picture", myPicture); helper.addAttachment("picture", file); ((JavaMailSender)mimeMailSender).send(mimeMessage); System.out.println("发送成功"); }








你可能感兴趣的:(Java,Spring,JavaMail,SMTP)