javamail 可以发邮件,发附件(附件大小可以超过2M),抄送多人,抄送密件,定时发送,消除乱码[附javamail的jar包]

突然要用到发邮件,今天看了下javamail,从网上找了个例子,自己改了改,调通了,放到这里,以便今后使用 除原有功能外,增加定时发送的功能,增加消除邮件中的乱码解决方法,即通过服务器自动发送时,邮件标题,内容,发件人,收件人不会出现乱码,已经测试过,没问题


/**
* 除原有功能外,增加定时发送的功能
* 增加消除邮件中的乱码解决方法,
* 即通过服务器自动发送时,邮件标题,内容,发件人,收件人不会出现乱码
* 已经测试过,没问题
*/
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
import javax.mail.internet.MimeUtility;

class PopupAuthenticator extends Authenticator {
public static final String mailuser=""; // 邮箱名,@之前的
public static final String password=""; // 邮箱密码
public static final String subject = "Javamal发送测试";
// 设置多个抄送人
public static final String[] cc = {"",""};
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailuser,password);
}
}

public class test{


// props.put("mail.transport.protocol", "smtp"); //使用smtp协议
public static void main(String[] args){

new test().beepForAnHour();

}

public void test_main() throws MessagingException, UnsupportedEncodingException{

Properties props = new Properties();
props.put("mail.smtp.host","smtp.qq.com");
props.put("mail.smtp.auth","true"); // 是否需要身份验证

PopupAuthenticator auth = new PopupAuthenticator();

Session session = Session.getInstance(props, auth);
session.setDebug(true); //主要是利于调试,默认为false

// 发件人
Address addressFrom = new InternetAddress(PopupAuthenticator.mailuser+"@qq.com", MimeUtility.encodeWord("自动发送的测试员","utf-8", "Q"));
// 收件人
Address addressTo = new InternetAddress("", MimeUtility.encodeWord("自动发送的测试员","utf-8","Q"));

// 密件收件人
Address addressBCC = new InternetAddress("","自动发送的测试员");

MimeBodyPart textBodyPart=new MimeBodyPart();
textBodyPart.setText("邮件成功发送,且无乱码"); // 邮件内容

MimeBodyPart fileBodyPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource("");//要发送的附件地址
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(fds.getName());// 设置附件的名称

Multipart container=new MimeMultipart();
container.addBodyPart(textBodyPart);
container.addBodyPart(fileBodyPart);

try{
MimeMessage message = new MimeMessage(session);
message.setSentDate(new Date());// 设置发送时间

message.setSubject(PopupAuthenticator.subject,"utf-8");// 设置邮件主题的编码格式

message.setFrom(addressFrom); // 发件人
if(null != addressTo){
message.addRecipient(Message.RecipientType.TO,addressTo);// 收件人
}
if(null != PopupAuthenticator.cc){
Address[] ccAdresses = new InternetAddress[PopupAuthenticator.cc.length];
for (int i=0; i<PopupAuthenticator.cc.length; i++){
            ccAdresses[i] = new InternetAddress(PopupAuthenticator.cc[i]);
        }
// 将抄送者信息设置到邮件信息中
message.setRecipients(Message.RecipientType.CC, ccAdresses);
}

if(null != addressBCC){
// message.addRecipient(Message.RecipientType.BCC, addressBCC);// 设置密件抄送
}
message.setContent(container,"utf-8"); // 添加 文本文件和附件
message.saveChanges();

Transport transport = session.getTransport("smtp");
// host,user,password
transport.connect("smtp.qq.com", PopupAuthenticator.mailuser,PopupAuthenticator.password);
Transport.send(message);

transport.close();
}catch (MessagingException mex) {
        System.out.println("send failed, exception: " + mex);
    }

}

public void beepForAnHour() {
TimerTask task = new TimerMask1();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date_str = null;
try {
date_str = sdf.parse("2011-01-28 10:35:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date date = date_str;
Timer timer = new Timer();
timer.schedule(task, date, 10000);
}
}

class TimerMask1 extends TimerTask {
public void run() {
java.sql.Time sTime = new java.sql.Time(new Date().getTime());
System.out.println("现在时间  " + sTime);
try {
new test().test_main();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------------------------
注意:上面用的是QQ邮箱来发送的,所以要加上QQ的SMTP,而且要把QQ邮箱里的SMTP打开,打开可以参考 http://service.mail.qq.com/cgi-bin/help?subtype=1&no=166&id=28
不打开,就发不了
进入方法:google上输入“QQ邮箱”--登入--账户--POP3/IMAP/SMTP服务 中的 开启POP3/SMTP服务 打上勾 就可以了

你可能感兴趣的:(sql,qq,Google,cgi)