import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
public class SpringMail {
public static void main(String[] args) throws Exception{
ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");
JavaMailSenderImpl sender = (JavaMailSenderImpl)ctx.getBean("mailSender");
SpringMail springMail=new SpringMail();
springMail.sendInit(sender);
}
private void sendInit(JavaMailSenderImpl sender) throws MessagingException {
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true,"GB2312");
helper.setFrom("dongweiyi1125@sina.com");
helper.setTo("dongweiyi1125@sina.com");
helper.setSubject("Test");
helper.setText("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='#'>郁闷!"
+ "<img src=\"cid:identifier\"></body></html>", true);
FileSystemResource res = new FileSystemResource(new File("c:/weiyi.jpg"));
helper.addInline("identifier", res);
try {
sender.send(message);
} catch (MailException e) {
e.printStackTrace();
}
System.out.println("成功发送内嵌文件");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.sina.com</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
<property name="username">
<value>dongweiyi1125</value>
</property>
<property name="password">
<value>邮箱密码</value>
</property>
</bean>
</beans>
以上代码给新浪邮箱发送邮件时图片总不能正常显示,但是给QQ邮箱发送邮件时却可以正常显示,不知什么原因,请大虾出来帮忙……
回复 更多评论