lisq037的专栏

pom.xml

<!– 邮件发送 –><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.1</version></dependency>EmailServicepackage net.deniro.email;/** * 邮件服务 * * @author deniro *15-2-5下午3:11 */public interface EmailService {/*** 发送html格式的邮件** @param to接收地址* @param subject 邮件主题* @param htmlText 邮件内容* @throws EmailException*/void sendMail(String to, String subject, String htmlText) throws EmailException;}EmailServiceImplpackage net.deniro.email;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.*;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.util.Properties;import static javax.mail.Message.RecipientType.TO;/** * @author deniro *15-2-5下午3:16 */public class EmailServiceImpl implements EmailService {private JavaMailSender javaMailSender;/*** 系统邮箱*/private String systemEmail;public static void main(String[] args) {try {String host = "smtp.sina.com";final String from = "lisq047@sina.com";String to = "lisq047@163.com";Properties props = System.getProperties();props.put("mail.smtp.host", host);props.put("mail.smtp.auth", "true");Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(from, "deniro");}};Session session = Session.getDefaultInstance(props, authenticator);MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress(from));message.addRecipients(TO, String.valueOf(new InternetAddress(to)));message.setSubject("测试");message.setText("测试内容");message.saveChanges();Transport.send(message);} catch (MessagingException e) {e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.}}@Overridepublic void sendMail(String to, String subject, String htmlText) throws EmailException {try {MimeMessage msg = javaMailSender.createMimeMessage();MimeMessageHelper msgHelper = new MimeMessageHelper(msg, "utf-8");//解决中文乱码msgHelper.setFrom(systemEmail);msgHelper.setTo(to);msgHelper.setSubject(subject);msgHelper.setText(htmlText, true);//html格式javaMailSender.send(msg);} catch (MessagingException e) {throw new EmailException("邮件发送失败。", e);}}public JavaMailSender getJavaMailSender() {return javaMailSender;}public void setJavaMailSender(JavaMailSender javaMailSender) {this.javaMailSender = javaMailSender;}public String getSystemEmail() {return systemEmail;}public void setSystemEmail(String systemEmail) {this.systemEmail = systemEmail;}}

email.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xsi:schemaLocation=" http://www.springframework.org/schema/beans/spring-beans.xsd"><!–载入属性文件–><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="location" value="classpath:service.properties"></property></bean><!–邮件发送–><bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="protocol" value="${email.protocol}"/><property name="host" value="${email.host}"/><property name="port" value="${email.port}"/><property name="username" value="${email.username}"/><property name="password" value="${email.password}"/><property name="javaMailProperties"><props><prop key="mail.${email.protocol}.auth">${email.auth}</prop></props></property></bean><!–邮件服务–><bean id="emailService" class="net.deniro.email.EmailServiceImpl"><property name="javaMailSender" ref="javaMailSender"/><property name="systemEmail" value="${email.systemEmail}"/></bean></beans>service.properties

email.protocol=smtpemail.host=smtp.sina.comemail.port=25email.username=lisq047email.password=******email.auth=trueemail.systemEmail=lisq047@sina.com邮件发送结果:

调用前,,记得先开启系统邮箱的SMTP服务:

美好的生命应该充满期待惊喜和感激

lisq037的专栏

相关文章:

你感兴趣的文章:

标签云: