1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| package javamail; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Mail163Test { public static void main(String[] args) throws Exception{ Properties props = new Properties(); props.setProperty("mail.host", "smtp.163.com"); props.setProperty("mail.smtp.auth", "true"); Authenticator authenticator = new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("163邮箱的帐号","授权码"); } };
Session session = Session.getDefaultInstance(props, authenticator); Message message = new MimeMessage(session); message.setFrom(new InternetAddress("xxx@163.com"));
message.setRecipient(RecipientType.TO, new InternetAddress("526745683@qq.com")); message.setSubject("邮件的标题"); String str = "李四: <br/>" + "您好,您在本论坛注册用户,点击下面url进行激活<br/>" + "http://ww......<br/>" + "如果不能点击,请复制直接激活<br/>" + "如果不是本人,请删除邮件"; message.setContent(str, "text/html;charset=UTF-8"); Transport.send(message); } }
|