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工具类发送邮件。
import java.util.*;
import javax.mail.*;import javax.mail.internet.*;
public class JMail {
public void SendMail(String Topic,String Content){ Properties props=new Properties(); props.put("mail.smtp.host","smtp.163.com"); props.put("mail.smtp.auth","true"); Session s=Session.getInstance(props); s.setDebug(false); MimeMessage message=new MimeMessage(s); MimeMultipart mp=new MimeMultipart(); BodyPart body = new MimeBodyPart(); InternetAddress from; InternetAddress to; try{ from=new InternetAddress("发件人邮箱"); message.setFrom(from); to = new InternetAddress("收件人邮箱"); message.setRecipient(Message.RecipientType.TO,to); message.setSubject(Topic,"utf-8"); body.setContent(Content, "text/html;charset=utf-8"); mp.addBodyPart(body); message.setContent(mp); message.setSentDate(new Date()); message.saveChanges(); Transport transport=s.getTransport("smtp"); transport.connect("smtp.163.com(邮件服务商,这是163的)","发件邮箱","发件邮箱密码"); transport.sendMessage(message,message.getAllRecipients()); transport.close(); } catch(AddressException e){ e.printStackTrace(); } catch(MessagingException e){ e.printStackTrace(); } }}
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
这是我的连续发几封邮件的代码。连续发是为了163邮箱的积分,^_^。你可以参考下。
package com.test;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail extends Authenticator {
public static final String mailuser = "xianlaioy"; // 需要你的邮箱帐号(仅@符号前面的)
public static final String password = "######"; // 需要你的邮箱密码
private Properties props;
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailuser, password);
}
public SendEmail(){
props = new Properties();
InputStream is = null;
try {
/*当部署在web容器中时,路径要修改
* Thread.currentThread().getContextClassLoader().getResource("").getPath();
* 得到的url是web容器中的项目下的class路径
**/
is = new FileInputStream("src/sendemail.properties");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
props.load(is);
} catch (IOException e) {
e.printStackTrace();
}
}
/** email为接收邮箱,text为邮件正文
* @throws IOException */
public void sendEamils(String email, String text) throws IOException {
Transport transport = null;
try {
props.setProperty("mail.smtp.host", props.getProperty("mail.smtp.host"));
props.setProperty("mail.smtp.auth", props.getProperty("mail.smtp.auth"));
SendEmail auth = new SendEmail();
javax.mail.Session session = javax.mail.Session.getDefaultInstance(props,
auth);
MimeMessage message = new MimeMessage(session);
Address addressFrom = new InternetAddress(SendEmail.mailuser
+ "@163.com", "欧阳");
Address addressTo = new InternetAddress(email, "xianlai");
message.setText(text);
message.setSubject("邮件标题");
message.setFrom(addressFrom);
message.addRecipient(Message.RecipientType.TO, addressTo);
message.saveChanges();
transport = session.getTransport("smtp");
session.setDebug(true);
/*transport.connect("smtp.126.com", SendEmail.mailuser,
SendEmail.password);*/
transport.send(message);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
if (transport != null) {
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Timer t = new Timer();
long delay =2*60*1000;
t.schedule(new TimerTask(){
private int count=0;
private SendEmail se=new SendEmail();//作为成员变量就只会创建一个SendEmail对象。
public void run() {
//在timer的run中就必须考虑好怎样或者什么时候退出的问题。
//这里是用count计数的方法来考虑退出的问题的。
if(count++1){
int i=10;
for (int j = 0; j i; j++) {
try {
se.sendEamils("xianlaioy@126.com","test"+j );
} catch (IOException e) {
e.printStackTrace();
}
}
}else {
this.cancel();
System.exit(0);
}
}}, 2000,delay);
}
}