提供一个跟阿里云客服小姐姐要到的Demo
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
| package com.ruben;
import com.sun.net.ssl.internal.ssl.Provider;
import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.security.Security; import java.util.Date; import java.util.Properties;
public class AliCompanyEmailSender { public static void main(String[] args) throws MessagingException { Security.addProvider(new Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties(); props.put("mail.smtp.host", "smtp.mxhichina.com"); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("阿里云企业账号用户名", "阿里云企业账号密码"); } } ); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("发件人名字")); message.setRecipients(Message.RecipientType.TO, "收件人邮箱"); message.setSubject("标题"); String content = "正文"; message.setText(content); message.setSentDate(new Date()); message.saveChanges(); Transport.send(message); } }
|
还有就是一定要把POP3/SMTP
服务和IMAP/SMTP
服务