发送邮件的例子
package cc.yiyao.common;
import java.security.Security;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendGmail {
private MimeMessage mimeMsg;
private Session session;
private Properties props;
private String username = "";// 用户名?
private String password = "";// 密码
private Multipart mp;
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
public SendGmail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
/**
* @param hostName
* String
*/
public void setSmtpHost(String hostName) {
if (props == null)
props = System.getProperties(); //获取系统properties
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.smtp.host", hostName);
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.auth", "true");
// props.setProperty("mail.smtp.starttls.enable", "false");
}
/**
* @return boolean
*/
public boolean createMimeMessage() {
try {
session = Session.getDefaultInstance(props, null);
} catch (Exception e) {
return false;
}
try {
mimeMsg = new MimeMessage(session);
mp = new MimeMultipart();
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param need
* boolean
*/
public void setNeedAuth(boolean need) {
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtps.auth", "true");
} else {
props.put("mail.smtps.auth", "false");
}
}
/**
* @param name
* String
* @param pass
* String
*/
public void setNamePass(String name, String pass) {
username = name;
password = pass;
}
/**
* @param mailSubject
* String
* @return boolean
*/
public boolean setSubject(String mailSubject) {
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param mailBody
* String
*/
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(
"<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ mailBody, "text/html;charset=gb2312");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean addFileAffix(String filename) {
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(MimeUtility.encodeWord(fileds.getName(), "gb2312",
null));
String contentType = bp.getContentType();
bp.setHeader("Content-Type", contentType + ";charset=gb2312");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setFrom(String from) {
try {
mimeMsg.setFrom(new InternetAddress(from)); // 发件人地址?
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功");
transport.close();
return true;
} catch (Exception e) {
System.err.println("发送邮件失败?" + e);
return false;
}
}
// 发磅邮件(自己写的文件(OK) )
/**调用方法(jsp页面中要引入activation.jar和mail.jar包):
* SendGmail themail = new SendGmail("smtp.gmail.com");
* themail.sendEmail("标题","内容","要发送的邮箱","从哪里发送的邮箱")就行了。
* (当然,main的方法也可以直接运行,不用引入activation.jar和mail.jar包)也能实现发送邮件的功能!作测试用的)
* */
public boolean sendEmail(String subjectStr,String mailbody,String toEmail,String fromEmail){
SendGmail themail = new SendGmail("smtp.gmail.com");
themail.setNeedAuth(true);
if (themail.setSubject(subjectStr) == false) //
return false;
if (themail.setBody(mailbody) == false)
return false;
if (themail.setTo(toEmail) == false) //要发送的邮箱
return false;
if (themail.setFrom(fromEmail) == false) //从哪里发送的邮箱
return false;
themail.setNamePass("straight.pn@gmail.com", "123456@");
if (themail.sendout() == false)
return false;
return true;
}
/**
* Just do it as this
*/
/* main的方法也可以直接运行,也能实现发送邮件的功能!
public static void main(String[] args) {
String mailbody = "<div align=center><a href='http://www.yiyao.cc'>minsheng</a></div>"; //内容
SendGmail themail = new SendGmail("smtp.gmail.com");
themail.sendEmail("Test标题",mailbody,"laiahu@126.com","laiahu2005@yahoo.com.cn");
System.out.println(themail.sendEmail("Test标题",mailbody,"laiahu@126.com","laiahu2005@yahoo.com.cn"));
themail.setNeedAuth(true);
if (themail.setSubject("Test标题") == false) //标题
return;
if (themail.setBody(mailbody) == false)
return;
if (themail.setTo("laiahu@126.com") == false) //要发送的邮箱
return;
if (themail.setFrom("laiahu2005@yahoo.com.cn") == false) //从哪里发送的邮箱
return;
// if (themail.addFileAffix(SendMail.class.getResource("").getPath()
// + "Blue hills.jpg") == false)
// return;
themail.setNamePass("straight.pn@gmail.com", "123456@");
if (themail.sendout() == false)
return;
}*/
}