java邮件发送的简单实现,使用javamail通过smtp协议发信

1.通过javamail实现
 
[java]  view plain  copy
  1. import javax.mail.*;  
  2. import javax.mail.internet.InternetAddress;  
  3. import javax.mail.internet.MimeMessage;  
  4. import java.util.Properties;  
  5.   
  6. public class SimpleAliDMSendMail {  
  7.     private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";  
  8.     private static final int ALIDM_SMTP_PORT = 25;  
  9.   
  10.     public static void main(String[] args) throws MessagingException {  
  11.         // 配置发送邮件的环境属性  
  12.         final Properties props = new Properties();  
  13.         // 表示SMTP发送邮件,需要进行身份验证  
  14.         props.put("mail.smtp.auth""true");  
  15.         props.put("mail.smtp.host", ALIDM_SMTP_HOST);  
  16.         props.put("mail.smtp.port", ALIDM_SMTP_PORT);     
  17.         // 如果使用ssl,则去掉使用25端口的配置,进行如下配置,   
  18.         // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
  19.         // props.put("mail.smtp.socketFactory.port", "465");  
  20.         // props.put("mail.smtp.port", "465");  
  21.   
  22.   
  23.         // 发件人的账号  
  24.         props.put("mail.user""***");  
  25.         // 访问SMTP服务时需要提供的密码  
  26.         props.put("mail.password""***");  
  27.   
  28.         // 构建授权信息,用于进行SMTP进行身份验证  
  29.         Authenticator authenticator = new Authenticator() {  
  30.             @Override  
  31.             protected PasswordAuthentication getPasswordAuthentication() {  
  32.                 // 用户名、密码  
  33.                 String userName = props.getProperty("mail.user");  
  34.                 String password = props.getProperty("mail.password");  
  35.                 return new PasswordAuthentication(userName, password);  
  36.             }  
  37.         };  
  38.         // 使用环境属性和授权信息,创建邮件会话  
  39.         Session mailSession = Session.getInstance(props, authenticator);  
  40.         // 创建邮件消息  
  41.         MimeMessage message = new MimeMessage(mailSession);  
  42.         // 设置发件人  
  43.         InternetAddress form = new InternetAddress(  
  44.                 props.getProperty("mail.user"));  
  45.         message.setFrom(form);  
  46.   
  47.         // 设置收件人  
  48.         InternetAddress to = new InternetAddress("***");  
  49.         message.setRecipient(MimeMessage.RecipientType.TO, to);  
  50.   
  51.         // 设置邮件标题  
  52.         message.setSubject("测试邮件");  
  53.         // 设置邮件的内容体  
  54.         message.setContent("测试的HTML邮件""text/html;charset=UTF-8");  
  55.   
  56.         // 发送邮件  
  57.         Transport.send(message);  
  58.     }  
  59. }  


2.使用Apache Commons-email组件发送邮件
commons-email是apache提供的一个开源的API,是对javamail的封装,
因此在使用时要将javamail.jar加到 class path中,
主要包括SimpleEmail,MultiPartEmail,HtmlEmail,EmailAttachment四个类。
 
SimpleEmail:发送简单的email,不能添加附件
MultiPartEmail:文本邮件,可以添加多个附件
HtmlEmail:HTML格式邮件,同时具有MultiPartEmail类所有“功能”
EmailAttchment:附件类,可以添加本地资源,也可以指定网络上资源,在发送时自动将网络上资源下载发送。

 
发送基本文本格式邮件:
[java]  view plain  copy
  1. SimpleEmail email = new SimpleEmail();  
  2. //smtp host   
  3. email.setHostName("mail.test.com");  
  4. //登陆邮件服务器的用户名和密码  
  5. email.setAuthentication("test","testpassword");  
  6. //接收人  
  7. email.addTo("ttest@163.com""John Doe");  
  8. //发送人  
  9. email.setFrom("me@163.com""Me");  
  10. //标题  
  11. email.setSubject("Test message");  
  12. //邮件内容  
  13. email.setMsg("This is a simple test of commons-email");  
  14. //发送  
  15. email.send();  



发送文本格式,带附件邮件:
[java]  view plain  copy
  1. //附件,可以定义多个附件对象  
  2. EmailAttachment attachment = new EmailAttachment();  
  3. attachment.setPath("e:\\1.pdf");  
  4. attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  5. attachment.setDescription("Picture of John");  
  6. //  
  7. MultiPartEmail email = new MultiPartEmail();  
  8. //smtp host   
  9. email.setHostName("mail.test.com");  
  10. //登陆邮件服务器的用户名和密码  
  11. email.setAuthentication("test","testpassword");  
  12. //接收人  
  13. email.addTo("test@163.com""John Doe");  
  14. //发送人  
  15. email.setFrom("me@163.com""Me");  
  16. //标题  
  17. email.setSubject("Test message");  
  18. //邮件内容  
  19. email.setMsg("This is a simple test of commons-email");  
  20. //添加附件  
  21. email.attach(attachment);  
  22. //发送  
  23. email.send();  


 
发送HTML格式带附件邮件:
[java]  view plain  copy
  1. //附件,可以定义多个附件对象  
  2. EmailAttachment attachment = new EmailAttachment();  
  3. attachment.setPath("e:\\1.pdf");  
  4. attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  5. attachment.setDescription("Picture of John");  
  6. //  
  7. HtmlEmail email = new HtmlEmail ();  
  8. //smtp host   
  9. email.setHostName("mail.test.com");  
  10. //登陆邮件服务器的用户名和密码  
  11. email.setAuthentication("test","testpassword");  
  12. //接收人  
  13. email.addTo("test@163.com""John Doe");  
  14. //发送人  
  15. email.setFrom("me@163.com""Me");  
  16. //标题  
  17. email.setSubject("Test message");  
  18. //邮件内容  
  19. email.setHtmlMsg("This is a simple test of commons-email");  
  20. //添加附件  
  21. email.attach(attachment);  
  22. //发送  
  23. email.send();  



下面提供一个完整的程序示例:
[java] view plain copy
  1. package test  
  2.   
  3. import org.apache.commons.mail.*;  
  4.   
  5. public class SendEMail  
  6. {  
  7.     public static void main ( String[] arg ) throws Exception  
  8.     {  
  9.         SimpleEmail email = new SimpleEmail ( );  
  10.   
  11.         // smtp host  
  12.         email.setHostName ( "smtp.163.com" );  
  13.         // 登陆邮件服务器的用户名和密码  
  14.         email.setAuthentication ( "test""123456" );  
  15.         // 接收人  
  16.         email.addTo ( "test@yahoo.com.cn""test" );  
  17.         // 发送人  
  18.         email.setFrom ( "test@163.com""测试者" );  
  19.         // 标题  
  20.         email.setSubject ( "Test message" );  
  21.         // 邮件内容  
  22.         email.setMsg ( "This is a simple test of commons-email 测试......" );  
  23.         // 发送  
  24.         email.send ( );  
  25.           
  26.         System.out.println ( "发送成功 Send email successful!" );  
  27.   
  28.     }  
  29. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值