Java发邮件

RT

 

 

  1. package  com.fxt.test;  
  2. import  org.apache.commons.mail.EmailException;  
  3. import  org.apache.commons.mail.SimpleEmail;  
  4. public   class  Mail {  
  5.     public   static   void  main(String[] args)  throws  EmailException {  
  6.         SimpleEmail email = new  SimpleEmail();  
  7.         email.setTLS(true );  
  8.         email.setHostName("mail.bj.phluency.com" ); //邮件服务器      
  9.         email.setAuthentication("xuting.feng@bj.meetexpo.com""uDh" ); //smtp认证的用户名和密码      
  10.         try  {  
  11.             email.addTo("xuting.feng@bj.meetexpo.com" ); //收信者      
  12.             email.setFrom("fengxuting@qq.com" ); //发信者      
  13.             email.setSubject("Test邮件" ); //标题      
  14.             email.setCharset("UTF-8" ); //编码格式      
  15.             email.setMsg("邮件内容!" ); //内容      
  16.             email.send();//发送      
  17.             System.out.println("send success!" );  
  18.         } catch  (EmailException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22.       
  23.     public   void  getInformation(){  
  24.         String s = "aaa"  ;  
  25.         System.out.println(s);  
  26.     }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中发送邮件可以使用JavaMail API。下面是一个简单的Java程序示例,用于发送带有文本内容的邮件。 首先,需要导入javax.mail和javax.mail.internet包的类。 ```java import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { // 配置发送邮件的SMTP服务器信息 String host = "smtp.example.com"; final String username = "yourusername"; final String password = "yourpassword"; // 创建Properties对象,设置SMTP服务器信息 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); // 创建Session对象,用于发送邮件 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // 创建Message对象,设置收件人、发件人和邮件内容 Message message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("邮件主题"); message.setText("邮件内容"); // 发送邮件 Transport.send(message); System.out.println("邮件发送成功"); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 上述代码中,需要替换以下信息: - `host`:SMTP服务器的主机名。 - `username`:发件人的用户名。 - `password`:发件人的密码。 - `sender@example.com`:发件人的邮箱地址。 - `recipient@example.com`:收件人的邮箱地址。 你也可以根据需要,使用JavaMail API发送附件、HTML内容等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值