去哪网实习总结:定时发送邮件(JavaWeb)

本来是以做数据挖掘的目的进去哪网的,结构却成了系统开发。。。

不过还是比较认真的做了三个月,老师很认同我的工作态度和成果。。。

实习马上就要结束了,总结一下几点之前没有注意过的变成习惯和问题,分享给大家。


同时打个广告:去哪网内审部招JavaWeb开发实习生,时间非常自由,每周一天、周六周日甚至都可以,时间充裕的小伙伴给我留言啊,挣个零花钱,还能长点经验。。。。(保研的、想工作的大四狗最合适不过了。。。)



接上一篇:http://blog.csdn.net/mmc2015/article/details/47685369

需要的包:

activation.jar:http://download.csdn.net/detail/mmc2015/9009847

mail.jar:http://download.csdn.net/detail/mmc2015/9009839

补充连接数据库的包:mysql-connector-java-5.1.27-bin.jar:


不介绍过多知识点,直接上代代码:

public static void sendMail() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qunar.com");
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.auth", "true");
        
        //使用JavaMail发送邮件的5个步骤
        //1、创建session
        Session session = Session.getInstance(prop);
        //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);
        //2、通过session得到transport对象
        Transport ts = session.getTransport();
        //3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
        ts.connect("smtp.qunar.com", "你的邮箱账号", "你的密码");
        //4、创建邮件(只包含文本)
        //Message message = createSimpleMail(session);
        //4、创建邮件(带附件)
        Message message = createAttachMail(session);
        //5、发送邮件
        ts.sendMessage(message, message.getAllRecipients());
        ts.close();
}




//创建一封只包含文本的邮件
    public static MimeMessage createSimpleMail(Session session) throws Exception {
        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        //指明邮件的发件人
        message.setFrom(new InternetAddress("<span style="font-family: Arial, Helvetica, sans-serif;">你的邮箱账号</span><span style="font-family: Arial, Helvetica, sans-serif;">@qunar.com"));</span>
        //指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人邮箱账号@qunar.com"));
        //邮件的标题
        message.setSubject("只包含文本的简单邮件");
        
        //邮件的文本内容,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码
        message.setContent("Hello Mail!", "text/html;charset=UTF-8");
        
        //返回创建好的邮件对象
        System.out.println(message);
        return message;
    }



//创建一封包含附件的邮件
    public static MimeMessage createAttachMail(Session session) throws Exception {
        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        //指明邮件的发件人
        message.setFrom(new InternetAddress("<span style="font-family: Arial, Helvetica, sans-serif;">你的邮箱账号</span><span style="font-family: Arial, Helvetica, sans-serif;">@qunar.com"));</span>
        //指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("<span style="font-family: Arial, Helvetica, sans-serif;">收件人邮箱账号</span><span style="font-family: Arial, Helvetica, sans-serif;">@qunar.com"));</span>
        //邮件的标题
        message.setSubject("包含附件的邮件");
        
        //创建邮件正文,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码
        MimeBodyPart text = new MimeBodyPart();
        String mailText = "使用JavaMail创建的带附件的邮件\n";
        mailText = "定时任务,时间为:12:00:00\n";
        text.setContent(mailText, "text/html;charset=UTF-8");
        //创建邮件附件
        MimeBodyPart attach = new MimeBodyPart();
        //DataHandler dh = new DataHandler(new FileDataSource("C:\\Users\\userName\\Desktop\\test.xlsx"));
      	String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
      	String serverRealRootPath = GetServerRealPathUtil.getRootPath();
      	String fileName = <strong>serverRealRootPath</strong>+<strong>File.separator</strong>+nowDate+".xlsx";//这个函数还记得吧。。。。。
      	DataHandler dh = new DataHandler(new FileDataSource(fileName));
        attach.setDataHandler(dh);
        attach.setFileName(dh.getName());
        
        
        //创建容器描述数据关系(附件和正文内容本质是一种容器的包含关系,描述好了就ok)
        MimeMultipart mp = new MimeMultipart();
        mp.addBodyPart(text);
        mp.addBodyPart(attach);
        mp.setSubType("mixed");
        message.setContent(mp);
        message.saveChanges();
        
        //返回创建好的邮件对象
        System.out.println(message);
        return message;
    }





  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This error message usually occurs when the JDBC driver for MySQL is not found or not properly configured in your Java application. To fix this issue, you need to make sure that you have the correct JDBC driver for MySQL installed and that the driver is included in your application's classpath. Here are some steps you can follow to resolve this issue: 1. Download the JDBC driver for MySQL from the official website: https://dev.mysql.com/downloads/connector/j/ 2. Extract the downloaded ZIP file and copy the JAR file to a folder in your project directory. 3. Add the JDBC driver JAR file to your project's classpath. You can do this by adding the following line to your project's build file (e.g. pom.xml for Maven): <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> 4. Make sure that you are using the correct JDBC URL for your MySQL database. The format should be like this: jdbc:mysql://localhost:3306/your_database_name Replace "your_database_name" with the actual name of your MySQL database. 5. Check that your MySQL database is running and accessible from your Java application. You can test this by using a MySQL client tool (e.g. MySQL Workbench) to connect to the database using the same credentials that your Java application is using. Once you have verified that the JDBC driver is installed and configured correctly, and that your MySQL database is running and accessible, you should be able to connect to the database from your Java application without any issues.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值