1、使用的Service類方法
package com.sais.inkaNet.common.service;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
public class MailService {
private JavaMailSender mailSender;
private String fromMail;
public JavaMailSender getMailSender() {
return mailSender;
}
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public String getFromMail() {
return fromMail;
}
public void setFromMail(String fromMail) {
this.fromMail = fromMail;
}
public void sendMail(String toMailAddr, String subject, String content) {
SimpleMailMessage smm = new SimpleMailMessage();
smm.setTo(toMailAddr);
smm.setFrom(fromMail);
smm.setSubject(subject);
smm.setText(content);
mailSender.send(smm);
}
/** */
/**
* 發送帶附件的郵件
*
* 郵件發送器
* @throws Exception
*/
public void sendMimeMessage(final String toMailAddr, final String subject,
final String contents, final List files) throws Exception {
MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage)
throws MessagingException {
mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(toMailAddr));
mimeMessage.setFrom(new InternetAddress(fromMail));
mimeMessage.setSubject(subject, "gb2312");
Multipart mp = new MimeMultipart();
// 向Multipart添加正文
MimeBodyPart content = new MimeBodyPart();
content.setText(contents);
// 向MimeMessage添加(Multipart代表正文)
mp.addBodyPart(content);
if (files != null && files.size() > 0) {
// 向Multipart添加附件
Iterator it = files.iterator();
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
while (it.hasNext()) {
MimeBodyPart attachFile = new MimeBodyPart();
String filename = it.next().toString();
FileDataSource fds = new FileDataSource(filename);
attachFile.setDataHandler(new DataHandler(fds));
attachFile.setFileName("=?GBK?B?"
+ enc.encode(fds.getName().getBytes()) + "?=");
mp.addBodyPart(attachFile);
}
files.clear();
}
// 向Multipart添加MimeMessage
mimeMessage.setContent(mp);
mimeMessage.setSentDate(new Date());
}
};
// 發送帶附件的郵件
mailSender.send(mimeMail);
}
/** */
/**
* 發送帶附件的html郵件
*
* @param 郵件發送器
* @throws Exception
*/
public void sendMimehtmlandmultiple(final String toMailAddr,final String mailName,final String subject,final String contents) throws Exception {
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(mime, true, "utf-8");
helper.setFrom(fromMail);
//helper.setTo(new InternetAddress(toMailAddr,MimeUtility.encodeWord(mailName,"utf-8","Q")));
helper.setTo(toMailAddr);
helper.setSubject(subject);
// 需要將附件顯示在html中
// 在標籤中用cid:xx 標記,使用helper.addInline()方法添加
helper.setText(contents, true);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mailSender.send(mime);
}
}
2、配置文件中proterties
mail.host=smtp.sssss.com
mail.port=465
mail.username=ope
mail.password=ope
mail.smtp.socketFactory.port=456
mail.debug=true
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.smtp.starttls.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=true
[email protected]
3、spring配置
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">
${mail.smtp.timeout}
</prop>
<prop key="mail.debug">${mail.debug}</prop>
<prop key="mail.smtp.starttls.enable">
${mail.smtp.starttls.enable}
</prop>
<prop key="mail.smtp.socketFactory.class">
${mail.smtp.socketFactory.class}
</prop>
<prop key="mail.smtp.socketFactory.fallback">
${mail.smtp.socketFactory.fallback}
</prop>
</props>
</property>
</bean>
<bean id="mailService"
class="com.sais.inkaNet.common.service.MailService">
<property name="mailSender" ref="mailSender" />
<property name="fromMail" value="${mail.from}" />
</bean>
4、其他Action等配置完全看自己的項目如何配置,一定要注入mailService
5、