发送邮件

[code]
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;


public static void sendMail() throws MailException {
try {

JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

Properties props = new Properties();// 也可用Properties props =
// System.getProperties();
props.put("mail.smtp.host", "mail.test.com.cn");// 存??送?件服?器的信息
props.put("mail.smtp.auth", "true");// 同?通???
Session s = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("test@test.com.cn", "password");
}
});// 根据属性新建一个?件会?
s.setDebug(true);

MimeMessage message = new MimeMessage(s);// 由?件会?新建一个消息?象
// InternetAddress from=new InternetAddress("test@test.com.cn");
// message.setFrom(from);//?置?件人
// InternetAddress to=new InternetAddress("test@test.com.cn");
// message.setRecipient(Message.RecipientType.TO,to);//?置收件人,并?置其接收?型?TO
// message.setSubject("TO jzb2b");//?置主?
// message.setText("my test");//?置信件内容
// message.setSentDate(new Date());//

message.setHeader("Errors-To", "test@test.com.cn");

// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true, getCharset());

helper.setFrom("test@test.com.cn");
helper.setTo("test@test.com.cn");
helper.setText("111111");
helper.setSubject("222222");

Transport transport = s.getTransport("smtp");
transport.connect("mail.test.com.cn", "test@test.com.cn", "passworld");

transport.sendMessage(message, message.getAllRecipients());//

// ((JavaMailSenderImpl) mailSender).send(message);
}
catch (MessagingException e) {
e.printStackTrace();
}
}

public static String getCharset() {
return "iso-2022-jp";
}
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值