发送邮件 java实现

import Java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailDemo {
 public static void main(String args[]) throws Exception {
  Properties props = new Properties();
  props.setProperty("mail.smtp.auth", "true");
  props.setProperty("mail.transport.protocol", "smtp");
  props.setProperty("mail.host", "smtp.sina.com");
  props.setProperty("mail.port", "25");
 
  Session session = Session.getInstance(props, new Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("test", "123456");
   }
  });
  session.setDebug(true);
  Message msg = new MimeMessage(session);
  msg.setFrom(new InternetAddress("test@sina.com"));
  msg.setSubject("你好吗?");
  msg.setSentDate(new Date());
  msg.setContent("<a href='https://sina.com'><span>我很好,你呢?</span></a>", "text/html;charset=gbk");
  msg.setRecipients(RecipientType.TO, InternetAddress.parse("test@sina.com,test@sohu.com"));
  Transport.send(msg);
 }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpzpf.html