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);

    }

}

你可能感兴趣的:(java)