重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1.首先你需要一个邮箱中转发送站(听着很高端的样子),说白了就是注册一个邮箱作为你的发送邮件平台,然后通过编程调用平台发送邮件(也就是你注册某个邮箱,然后开通SMTP/POP3协议,在编程中,拿着你的KEY去发送邮件),我试过很多种邮箱,QQ貌似不能用,网易经常报错,建议用新浪的,我用基本没出过问题。
目前创新互联公司已为1000多家的企业提供了网站建设、域名、虚拟主机、网站运营、企业网站设计、抚顺县网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
2.去网上下载java开源的发送邮件工具类:mail.jar,并导入myeclipse/eclipse的引用。
3.编程(工具类)
public boolean mainto()
{
boolean flag = true;
//建立邮件会话
Properties pro = new Properties();
pro.put("mail.smtp.host","smtp.qq.com");//存储发送邮件的服务器
pro.put("mail.smtp.auth","true"); //通过服务器验证
Session s =Session.getInstance(pro); //根据属性新建一个邮件会话
//s.setDebug(true);
//由邮件会话新建一个消息对象
MimeMessage message = new MimeMessage(s);
//设置邮件
InternetAddress fromAddr = null;
InternetAddress toAddr = null;
try
{
fromAddr = new InternetAddress(451144426+"@qq.com"); //邮件发送地址
message.setFrom(fromAddr); //设置发送地址
toAddr = new InternetAddress("12345367@qq.com"); //邮件接收地址
message.setRecipient(Message.RecipientType.TO, toAddr); //设置接收地址
message.setSubject(title); //设置邮件标题
message.setText(content); //设置邮件正文
message.setSentDate(new Date()); //设置邮件日期
message.saveChanges(); //保存邮件更改信息
Transport transport = s.getTransport("smtp");
transport.connect("smtp.qq.com", "451144426", "密码"); //服务器地址,邮箱账号,邮箱密码
transport.sendMessage(message, message.getAllRecipients()); //发送邮件
transport.close();//关闭
}
catch (Exception e)
{
e.printStackTrace();
flag = false;//发送失败
}
return flag;
}
这是一个javaMail的邮件发送代码,需要一个mail.jar
代码如下:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailTest {
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.163.com");
Session session = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("xxx","xxx");//这里分别填写发送email的用户名、密码
}
}
);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("xxx"));//这里是发送方的email地址如:xxx@163.com
msg.setSubject("test javamail");
msg.setRecipients(RecipientType.TO,
InternetAddress.parse("xxx"));//这里是接收方的email地址如:xxx@163.com
msg.setContent("a href=\"\"谷歌/a","text/html;charset=gb2312");
Transport.send(msg);
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.SocketException;
import java.rmi.UnknownHostException;
import java.util.StringTokenizer;
import sun.misc.BASE64Encoder;
public class Sender {
//private boolean debug = true;
BASE64Encoder encode=new BASE64Encoder();//用于加密后发送用户名和密码
static int dk=25;
private Socket socket;
public Sender(String server, int port) throws UnknownHostException,
IOException {
try {
socket = new Socket(server, dk);
} catch (SocketException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
} finally {
//System.out.println("已经建立连接!");
}
}
// 注册到邮件服务器
public void helo(String server, BufferedReader in, BufferedWriter out)
throws IOException {
int result;
result = getResult(in);
// 连接上邮件服务后,服务器给出220应答
if (result != 220) {
throw new IOException("连接服务器失败");
}
result = sendServer("HELO " + server, in, out);
// HELO命令成功后返回250
if (result != 250) {
throw new IOException("注册邮件服务器失败!");
}
}
private int sendServer(String str, BufferedReader in, BufferedWriter out)
throws IOException {
out.write(str);
out.newLine();
out.flush();
/*
if (debug) {
System.out.println("已发送命令:" + str);
}
*/
return getResult(in);
}
public int getResult(BufferedReader in) {
String line = "";
try {
line = in.readLine();
/*
if (debug) {
System.out.println("服务器返回状态:" + line);
}
*/
} catch (Exception e) {
e.printStackTrace();
}
// 从服务器返回消息中读出状态码,将其转换成整数返回
StringTokenizer st = new StringTokenizer(line, " ");
return Integer.parseInt(st.nextToken());
}
public void authLogin(MailMessage message, BufferedReader in,
BufferedWriter out) throws IOException {
int result;
result = sendServer("AUTH LOGIN", in, out);
if (result != 334) {
throw new IOException("用户验证失败!");
}
result=sendServer(encode.encode(message.getUser().getBytes()),in,out);
//System.out.println("用户名: "+encode.encode(message.getUser().getBytes()));
if (result != 334) {
throw new IOException("用户名错误!");
}
result=sendServer(encode.encode(message.getPassword().getBytes()),in,out);
//result=sendServer(message.getPassword(),in,out);
//System.out.println("密码: "+encode.encode(message.getPassword().getBytes()));
if (result != 235) {
throw new IOException("验证失败!");
}
}
// 开始发送消息,邮件源地址
public void mailfrom(String source, BufferedReader in, BufferedWriter out)
throws IOException {
int result;
result = sendServer("MAIL FROM:" + source + "", in, out);
if (result != 250) {
throw new IOException("指定源地址错误");
}
}
// 设置邮件收件人
public void rcpt(String touchman, BufferedReader in, BufferedWriter out)
throws IOException {
int result;
result = sendServer("RCPT TO:" + touchman + "", in, out);
if (result != 250) {
throw new IOException("指定目的地址错误!");
}
}
// 邮件体
public void data(String from, String to, String subject, String content,
BufferedReader in, BufferedWriter out) throws IOException {
int result;
result = sendServer("DATA", in, out);
// 输入DATA回车后,若收到354应答后,继续输入邮件内容
if (result != 354) {
throw new IOException("不能发送数据");
}
out.write("From: " + from);
out.newLine();
out.write("To: " + to);
out.newLine();
out.write("Subject: " + subject);
out.newLine();
out.newLine();
out.write(content);
out.newLine();
// 句号加回车结束邮件内容输入
result = sendServer(".", in, out);
//System.out.println(result);
if (result != 250) {
throw new IOException("发送数据错误");
}
}
// 退出
public void quit(BufferedReader in, BufferedWriter out) throws IOException {
int result;
result = sendServer("QUIT", in, out);
if (result != 221) {
throw new IOException("未能正确退出");
}
}
// 发送邮件主程序
public boolean sendMail(MailMessage message, String server) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
helo(server, in, out);// HELO命令
authLogin(message, in, out);// AUTH LOGIN命令
mailfrom(message.getFrom(), in, out);// MAIL FROM
rcpt(message.getTo(), in, out);// RCPT
data(message.getDatafrom(), message.getDatato(),
message.getSubject(), message.getContent(), in, out);// DATA
quit(in, out);// QUIT
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
再写一个MailMessage.java,set/get方法即可。
1新建Java项目
2然后再项目下新建一个lib文件夹,复制需要的那个两个jar包到lib下
3选择 activation.jar和mail.jar,右键添加Build path
4用QQ给QQ发送邮件,发送方得开启第三方登录,也就是授权登录,需要开始POP3和SMTP,还有点击生成授权码,下面是操作截图
5编写Java代码
6运行,最后显示 250 Mail OK即发送成功
7接收方的QQ会收到邮件
JAVA邮件发送的大致过程是这样的的:
1、构建一个继承自javax.mail.Authenticator的具体类,并重写里面的getPasswordAuthentication()方法。此类是用作登录校验的,以确保你对该邮箱有发送邮件的权利。
2、构建一个properties文件,该文件中存放SMTP服务器地址等参数。
3、通过构建的properties文件和javax.mail.Authenticator具体类来创建一个javax.mail.Session。Session的创建,就相当于登录邮箱一样。剩下的自然就是新建邮件。
4、构建邮件内容,一般是javax.mail.internet.MimeMessage对象,并指定发送人,收信人,主题,内容等等。
5、使用javax.mail.Transport工具类发送邮件。