使用java发送QQ邮件的总结
最近帮朋友做个网站,实现用邮箱订阅功能,所以现在把这个发送邮件的功能放在这里,算是这两天工作的总结吧!
首先,想要实现订阅功能,要把邮箱保存,但是这个做的是个小网站,前后台交互的太少了,所以我就直接保存在了文件里面,用到的时候,直接读取。
下面是保存邮箱号到本地文件的代码。
package ccom.llf.smfp; import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class SendEmail extends HttpServlet {
/**
* 如果是get请求就重写doget方法,如果是其他的也是一样对应的
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename =this.getClass().getClassLoader().getResource("/").getPath()+"email.text";
String filename1 =this.getClass().getClassLoader().getResource("/").getPath()+"count.text";
/*filename = filename.substring(1, filename.length());
filename1 = filename1.substring(1, filename1.length());*/
response.setContentType("application/text; charset=utf-8");
PrintWriter out = response.getWriter(); //判断该邮箱时候已经订阅过
FileReader fr=new FileReader(filename);
BufferedReader br=new BufferedReader(fr);
String line="";
String[] arrs=null;
while ((line=br.readLine())!=null) {
if(line.equals(request.getParameter("SendEmail").toString()+"\t")){
out.write("1");
return;
}
}
br.close();
fr.close(); FileWriter writer = new FileWriter(filename, true);
//writer.write(request.getParameter("SendEmail").toString()+ ";"+"/r/n");
writer.write(request.getParameter("SendEmail").toString()+"\t\n");
writer.close(); File f = new File(filename1);
int count = 0;
if (!f.exists()) {
writeFile(filename1, 100);
}
try {
BufferedReader in = new BufferedReader(new FileReader(f));
try {
count = Integer.parseInt(in.readLine())+1;
writeFile(filename1, count);
out.write(String.valueOf(count));
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} } public static void writeFile(String filename, int count) { try {
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
这里用到的就是简单的输入输出流 ,就不做过多的讲解。
下面当腰发邮件的时候,需要开启邮箱smtp服务,获取授权码。
我们的是qq邮箱,这里以qq邮箱为例,在QQ邮箱的设置——>账号——>下拉有个pop3/smtp服务 开启,获取授权码。。发邮件的时候,就用到授权码,不直接用密码,这样防止账户安全吧。
package com.poi; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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; public class SendMail2 { private String host = "smtp.qq.com"; // smtp服务器
private static String from = ""; // 发件人邮箱号
private static String to = ""; // 收件人邮箱号
private String affix = ""; // 附件地址
private String affixName = ""; // 附件名称
private static String user = ""; // 用户名
private static String pwd = ""; // 授权码
private String subject = "hello"; // 邮件标题 public void setAddress(String from, String to, String subject) {
this.from = from;
this.to = to;
this.subject = subject;
} public void setAffix(String affix, String affixName) {
this.affix = affix;
this.affixName = affixName;
} public void send(String host, String user, String pwd) {
this.host = host;
this.user = user;
this.pwd = pwd; Properties props = new Properties(); // 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
props.put("mail.smtp.host", host);
// 需要经过授权,也就是有户名和密码的校验,这样才能通过验证
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", 465);
props.put("mail.smtp.ssl.enable", true);
// 用刚刚设置好的props对象构建一个session
Session session = Session.getDefaultInstance(props); // 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
// 用(你可以在控制台(console)上看到发送邮件的过程)
session.setDebug(true); // 用session为参数定义消息对象
MimeMessage message = new MimeMessage(session);
try {
// 加载发件人地址
message.setFrom(new InternetAddress(from));
// 加载收件人地址
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// 加载标题
message.setSubject(subject); // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart(); // 设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
contentPart.setText("第二种方法···");
multipart.addBodyPart(contentPart);
// 添加附件
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(affix);
// 添加附件的内容
messageBodyPart.setDataHandler(new DataHandler(source));
// 添加附件的标题
// 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
messageBodyPart.setFileName("=?GBK?B?"
+ enc.encode(affixName.getBytes()) + "?=");
multipart.addBodyPart(messageBodyPart); // 将multipart对象放到message中
message.setContent(multipart);
// 保存邮件
message.saveChanges();
// 发送邮件
Transport transport = session.getTransport("smtp");
// 连接服务器的邮箱
transport.connect(host, user, pwd);
// 把邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
//先往你的本地写一个文件,这样附件就坑定存在了。
File file = new File("D:\\22.cvg");
try {
OutputStream os = new FileOutputStream(file);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,
"utf-8"));
bw.write("hello");
bw.close();
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} SendMail2 cn = new SendMail2();
// 设置发件人地址、收件人地址和邮件标题
cn.setAddress(from, to, user);
// 设置要发送附件的位置和标题
cn.setAffix("D:\\22.cvg", "22.cvg");
// 设置smtp服务器以及邮箱的帐号和密码
cn.send("smtp.qq.com", from,pwd);
}
}
这样我们就可以发送邮件了。。。
使用java发送QQ邮件的总结的更多相关文章
- java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)
java 无法发送邮件问题 java 发送qq邮件 报错:java mail Received fatal alert: handshake_failure (使用ssl) javax.mail.M ...
- Java发送QQ邮件
面试的时候被问到这个问题,别人问我用Java发过邮件没有,被问得一脸懵逼.然后就研究了一下,不是很难,按照网上的方法折腾了几天就搞出来了. 首先,使用QQ邮箱发送邮件之前需要在邮箱里面配置,开启pop ...
- 使用java发送QQ邮件
使用java发送邮件的时候,需要先下载两个jar包,连接如下: JavaMail mail.jar 1.4.5 JAF(版本 1.1.1) activation.jar 然后将这连个包导入,博主用的是 ...
- Java实现QQ邮件发送
首先我们需要两个jar包,点击下面即可下载这两个包: JavaMail mail.jar 1.4.5 JAF(版本 1.1.1) activation.jar 我们这里采用QQ邮箱发送邮件为例,代码如 ...
- Java实现QQ邮件发送客户端
目录 一.前言:QQ邮件发送程序 二.封装SMTP操作 三.实现多线程接收 四.QQ邮件客户端界面设计 1.连接按钮 2.发送按钮 五.QQ邮件发送效果演示 六.总结 一.前言:QQ邮件发送程序 在上 ...
- CI框架使用PHPmail插件发送QQ邮件:
有助请顶,不好请评.0:33 2016/3/12CI框架使用PHPmail插件发送QQ邮件:发送成功,不过修改了主机参数,还包含了一个phpmail中的一个另外的文件,详见下方:参见:http://c ...
- 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明
13:40 2015/11/20 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明 关键点:现在qq邮箱开通smtp服务后会给你一个很长的独立新密码,发邮件配置中的密码需要 ...
- 【python】脚本连续发送QQ邮件
今天习得用python写一个连续发送QQ邮件的脚本,经过测试,成功给国内外的服务器发送邮件,包括QQ邮箱.163邮箱.google邮箱,香港科技大学的邮箱和爱丁堡大学的邮箱.一下逐步解答相关技巧. 首 ...
- python3:利用SMTP协议发送QQ邮件+附件
转载请表明出处:https://www.cnblogs.com/shapeL/p/9115887.html 1.发送QQ邮件,首先必须知道QQ邮箱的SMTP服务器 http://service.mai ...
随机推荐
- Android 获取全局Context的技巧
回想这么久以来我们所学的内容,你会发现有很多地方都需要用到Context,弹出Toast的时候需要.启动活动的时候需要.发送广播的时候需要.操作数据库的时候需要.使用通知的时候需要等等等等.或许目前你 ...
- web.xml里welcome-file欢迎页面配置及web.xml简介
web项目欢迎页面的配置 <welcome-file-list> <welcome-file>/WEB-INF/index.html</welcome-file> ...
- for循环内嵌套finally使用的意外获得
在for循环中有continue和break,无论执行continue还是break finally的逻辑都会执行,原本以为是不执行的 格式 for (int i = 0; i < 3; i ...
- Linux 安装MySQL-python
vi ~/.bash_profile PATH="/usr/local/mysql/bin:${PATH}" export PATH export DYLD_LIBRARY_PAT ...
- leetCode Detect Capital
1.问题描述 Given a word, you need to judge whether the usage of capitals in it is right or not. We defin ...
- commonjs详解
marked here a well written artical http://javascript.ruanyifeng.com/nodejs/module.html
- Oracle EBS 数据访问权限集
SELECT frv.responsibility_name, fpo.profile_option_name, fpo.user_profile_option_name, fpv.profile_o ...
- 使用uwsgi发布项目
1.先下载 uwsgi 指定豆瓣源下载 pip install -i https://pypi.douban.com/simple uwsgi 2.查看你的uwsgi基于那个python解释器运行的 ...
- 使用AKLocationManager定位
使用AKLocationManager定位 https://github.com/ideaismobile/AKLocationManager 以下是使用情况: 是不是很简单呢,我们可以将它的步骤进一 ...
- MongoDB的Spring-data-mongodb集成(Win10 x64) 第一章 - MongoDB安装与简单命令
这是MongoDB系列的第一章,作者将持续更新. 1.下载 https://www.mongodb.com/download-center#community 2.安装与配置 有关安装的任何困难请点击 ...