zoukankan      html  css  js  c++  java
  • Java 阿里云 邮件(带附件)发送

    简单的使用. 阿里云每天免费200封 1000封才2块钱..
    465端口 使用正常
    25 端口 不正常
    
    
    附上阿里云的文档: https://help.aliyun.com/document_detail/29450.html?spm=a2c4g.11186623.6.611.5212235bSuQnh1


    package
    com.gwzx.framework.utils; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; /** * 阿里云邮件发送 */ public class MailUtil { private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com"; private static final int ALIDM_SMTP_PORT = 25;// 或80 // 发件人的账号 和 密码 private String user; private String password; public MailUtil() { this("阿里云SMTP账户", "SMTP密码"); } public MailUtil(String user, String password) { this.user = user; this.password = password; } public static void main(String[] args) { //new MailUtil().send("1161588342@qq.com", "测试1", "nihao显示"); //new MailUtil().send("1161588342@qq.com", "测试1", "市劳动纠纷联赛积分了","C:/Users/guo/Desktop/Proguard.xml"); } /** * 发送邮件 * @param toEmail 收件人邮箱地址 * @param subject 邮件标题 * @param content 邮件内容 可以是html内容 */ public void send(String toEmail, String subject, String content) { Session session = loadMailSession(); // session.setDebug(true); // 创建邮件消息 MimeMessage message = new MimeMessage(session); try { // 设置发件人 message.setFrom(new InternetAddress(user)); Address[] a = new Address[1]; a[0] = new InternetAddress(user); message.setReplyTo(a); // 设置收件人 InternetAddress to = new InternetAddress(toEmail); message.setRecipient(MimeMessage.RecipientType.TO, to); // 设置邮件标题 message.setSubject(subject); // 设置邮件的内容体 message.setContent(content, "text/html;charset=UTF-8"); // 发送邮件 Transport.send(message); } catch (MessagingException e) { String err = e.getMessage(); // 在这里处理message内容, 格式是固定的 System.out.println(err); } } /** * 发送邮件 带附件 * @param toEmail 收件人邮箱地址 * @param subject 邮件标题 * @param content 邮件内容 可以是html内容 * @param attachPath 附件路径 */ public void send(String toEmail, String subject, String content, String attachPath) { Session session = loadMailSession(); MimeMessage mm = new MimeMessage(session); try { //发件人 mm.setFrom(new InternetAddress(user)); //收件人 mm.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); // 设置收件人 // mm.setRecipient(Message.RecipientType.CC, new // InternetAddress("XXXX@qq.com")); //设置抄送人 //标题 mm.setSubject(subject); //内容 Multipart multipart = new MimeMultipart(); //body部分 BodyPart contentPart = new MimeBodyPart(); contentPart.setContent(content, "text/html;charset=utf-8"); multipart.addBodyPart(contentPart); //附件部分 BodyPart attachPart = new MimeBodyPart(); FileDataSource fileDataSource = new FileDataSource(attachPath); attachPart.setDataHandler(new DataHandler(fileDataSource)); attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName())); multipart.addBodyPart(attachPart); mm.setContent(multipart); Transport.send(mm); } catch (Exception e) { String err = e.getMessage(); // 在这里处理message内容, 格式是固定的 System.out.println(err); } } private Session loadMailSession() { try { // 配置发送邮件的环境属性 final Properties props = new Properties(); // 表示SMTP发送邮件,需要进行身份验证 props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", ALIDM_SMTP_HOST); // props.put("mail.smtp.port", ALIDM_SMTP_PORT); // 如果使用ssl,则去掉使用25端口的配置,进行如下配置, props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.port", "465"); // 发件人的账号 props.put("mail.user", user); // 访问SMTP服务时需要提供的密码 props.put("mail.password", password); // 构建授权信息,用于进行SMTP进行身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 用户名、密码 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用环境属性和授权信息,创建邮件会话 return Session.getInstance(props, authenticator); } catch (Exception e) { e.printStackTrace(); System.out.println("mail session is null"); } return null; } }
  • 相关阅读:
    JDK13中将增加文本块特性
    java8新特性Lambda表达式为什么运行效率低
    Docker安装Mysql多版本
    Mysql中的降序索引底层实现
    GITHUB慢! 怎么办?
    程序员要搞明白CDN,这篇应该够了
    HTTP长连接
    Tomcat热部署与热加载
    Tomcat是一个Servlet容器?
    DPDK
  • 原文地址:https://www.cnblogs.com/eason-d/p/8039347.html
Copyright © 2011-2022 走看看