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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM