1.通過javamail實現
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.util.Properties;
- public class SimpleAliDMSendMail {
- private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";
- private static final int ALIDM_SMTP_PORT = 25;
- public static void main(String[] args) throws MessagingException {
- // 配置發送郵件的環境屬性
- 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", "***");
- // 訪問SMTP服務時需要提供的密碼
- props.put("mail.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);
- }
- };
- // 使用環境屬性和授權信息,創建郵件會話
- Session mailSession = Session.getInstance(props, authenticator);
- // 創建郵件消息
- MimeMessage message = new MimeMessage(mailSession);
- // 設置發件人
- InternetAddress form = new InternetAddress(
- props.getProperty("mail.user"));
- message.setFrom(form);
- // 設置收件人
- InternetAddress to = new InternetAddress("***");
- message.setRecipient(MimeMessage.RecipientType.TO, to);
- // 設置郵件標題
- message.setSubject("測試郵件");
- // 設置郵件的內容體
- message.setContent("測試的HTML郵件", "text/html;charset=UTF-8");
- // 發送郵件
- Transport.send(message);
- }
- }
2.使用Apache Commons-email組件發送郵件
SimpleEmail:發送簡單的email,不能添加附件
MultiPartEmail:文本郵件,可以添加多個附件
HtmlEmail:HTML格式郵件,同時具有MultiPartEmail類所有“功能”
EmailAttchment:附件類,可以添加本地資源,也可以指定網絡上資源,在發送時自動將網絡上資源下載發送。
發送基本文本格式郵件:
- SimpleEmail email = new SimpleEmail();
- //smtp host
- email.setHostName("mail.test.com");
- //登陸郵件服務器的用戶名和密碼
- email.setAuthentication("test","testpassword");
- //接收人
- email.addTo("[email protected]", "John Doe");
- //發送人
- email.setFrom("[email protected]", "Me");
- //標題
- email.setSubject("Test message");
- //郵件內容
- email.setMsg("This is a simple test of commons-email");
- //發送
- email.send();
發送文本格式,帶附件郵件:
- //附件,可以定義多個附件對象
- EmailAttachment attachment = new EmailAttachment();
- attachment.setPath("e:\\1.pdf");
- attachment.setDisposition(EmailAttachment.ATTACHMENT);
- attachment.setDescription("Picture of John");
- //
- MultiPartEmail email = new MultiPartEmail();
- //smtp host
- email.setHostName("mail.test.com");
- //登陸郵件服務器的用戶名和密碼
- email.setAuthentication("test","testpassword");
- //接收人
- email.addTo("[email protected]", "John Doe");
- //發送人
- email.setFrom("[email protected]", "Me");
- //標題
- email.setSubject("Test message");
- //郵件內容
- email.setMsg("This is a simple test of commons-email");
- //添加附件
- email.attach(attachment);
- //發送
- email.send();
發送HTML格式帶附件郵件:
- //附件,可以定義多個附件對象
- EmailAttachment attachment = new EmailAttachment();
- attachment.setPath("e:\\1.pdf");
- attachment.setDisposition(EmailAttachment.ATTACHMENT);
- attachment.setDescription("Picture of John");
- //
- HtmlEmail email = new HtmlEmail ();
- //smtp host
- email.setHostName("mail.test.com");
- //登陸郵件服務器的用戶名和密碼
- email.setAuthentication("test","testpassword");
- //接收人
- email.addTo("[email protected]", "John Doe");
- //發送人
- email.setFrom("[email protected]", "Me");
- //標題
- email.setSubject("Test message");
- //郵件內容
- email.setHtmlMsg("This is a simple test of commons-email");
- //添加附件
- email.attach(attachment);
- //發送
- email.send();