1.发送普通纯文本邮件:
SimpleEmail email = new SimpleEmail();
email.setHostName("SMTP服务器");
email.setAuthentication("用户名","密码");
email.addTo("收件人", "收件人名字");
email.setFrom("发件人邮件", "发件人名字");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
2。发送带附件的邮件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("D:\\123.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("John");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("SMTP服务器");
email.setAuthentication("用户名","密码");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// add the attachment
email.attach(attachment);
// send the email
email.send();
3.HTML格式邮件
HtmlEmail email = new HtmlEmail();
email.setHostName("SMTP服务器");
email.setAuthentication("用户名","密码");
email.addTo("收件人", "收件人名字");
email.setFrom("发件人邮件", "发件人名字");
email.setSubject("The picture");
URL url = new URL("http://****.gif");
String cid = email.embed(url, "Apache logo");
// set the html message
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
===========================================================================
package com.cn.cosoft.util.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
/**
* <P>
* Title:用java发送邮件的例子
* </P>
*
* <P>
* Description:发送图片附件并在html中使用该图片
* </P>
*
* <P>
* Copyright: Copyright (c) 2007
* </P>
*
* @author 孙钰佳
* @main sunyujia@yahoo.cn
* @date Jun 10, 2008 12:35:26 AM
*/
public class SendMail extends Thread {
private String username = "a123456";//发送者邮箱用户名
private String password = "123456";//发送者邮箱密码
private String smtpServer = "smtp.gmail.com";//发送者邮箱的smtp
private String fromMailAddress = "a123456@gmail.com";// "yncosoft@gmail.com";//发送者的邮箱
private int smtpPort = 465;//默认smtp端口号是25
private String toMailAddress;//接收者的邮箱地在
private String subject;//主题
private String message;//邮件正文
private String filename;//读取某个文件的内容为邮件正文
public static String getPasswordFile = "getPassword";//获取密码的文件
public static String registerStatusFile = "registerStatus";//注册验证的邮件
private String getPasswordStatusCode = "";
private String registerStatusStatusCode = "";
public void setGetPasswordStatusCode(String getPasswordStatusCode) {
this.getPasswordStatusCode = getPasswordStatusCode;
}
public void setRegisterStatusStatusCode(String registerStatusStatusCode) {
this.registerStatusStatusCode = registerStatusStatusCode;
}
public void setFilename(String filename) {
this.filename = filename;
}
public void setMessage(String message) {
this.message = message;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setToMailAddress(String toMailAddress) {
this.toMailAddress = toMailAddress;
}
public static void main(String[] args) throws Exception {
SendMail send = new SendMail();
send.setFilename(SendMail.registerStatusFile);
send.setToMailAddress("123456@qq.com");
send.setSubject("主题");
send.sendFileContext();
}
@Override
public void run() {
try {
try {
sendFileContext();
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (EmailException ex) {
Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void sendFileContext() throws EmailException, UnsupportedEncodingException {
HtmlEmail email = new HtmlEmail();
email.setHostName(smtpServer);//设置SMTP
email.setSmtpPort(smtpPort);//设置SMTP 端口
email.setSSL(true);//设置SMTP 安全:SSL
email.setAuthentication(username, password);//设置邮件的登录用户名和密码
email.setFrom(fromMailAddress, "发送者名字"); //设置发送者和名字
email.addTo(toMailAddress, "接收者密码");//设置接收者和名字
email.setSubject(subject);//设置邮件主题
// URL url1 = new URL("http://css.tudouui.com/skin/login/img/logo_0.png");
// String cid2 = email.embed(url1, "Apache logo2");//附件(附件内容,名字)
File file = new File(SendMail.class.getResource(filename).toString().substring(5));
BufferedReader reader = null;
StringBuilder msg = new StringBuilder("");
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
msg.append(tempString);
}
} catch (Exception e) {
}
message = msg.toString();
message = new String(message.getBytes("utf-8"), "ISO-8859-1");//处理中文乱码转码
email.setHtmlMsg(message);
email.setTextMsg(message);
email.send();
}
public void send() throws EmailException, MalformedURLException, UnsupportedEncodingException {
HtmlEmail email = new HtmlEmail();
email.setHostName(smtpServer);
email.setSmtpPort(smtpPort);
email.setAuthentication(username, password);
email.addTo(toMailAddress, toMailAddress);
email.setFrom(fromMailAddress, fromMailAddress);
email.setSubject(subject);
// URL url1 = new URL("http://css.tudouui.com/skin/login/img/logo_0.png");
// String cid2 = email.embed(url1, "Apache logo2");//附件(附件内容,名字)
message = new String(message.getBytes("gbk"), "ISO-8859-1");//处理中文乱码转码
email.setHtmlMsg(message);
email.setTextMsg("Your email client does not support HTML messages");
email.send();
}
}