java邮件发送,
分享于 点击 310 次 点评:182
java邮件发送,
import java.util.Properties;import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSend {
// PS:邮件发送异常需要加入 activation-1.1.jar
private void sendMail(final String smtpHost, final String from, final String to,
final String subject, final String body) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
Session session = Session.getDefaultInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject,"UTF-8");
msg.setText(body,"UTF-8");
Transport.send(msg);
}
}
相关文章
- 暂无相关文章
用户点评