import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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 sun.misc.BASE64Encoder;
public class MailSender{
public void sendMail() throws Exception{
Properties props = new Properties();
props.put("mail.smtp.host", getHost());
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(getFromAddr()));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
message.setSubject(getTitle());
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setText("这是我用Java程序发送的第一封邮件!呵呵!!!");//这里就是要发出去的邮件内容啦!
multipart. addBodyPart(contentPart);
if(getAttachPath() != null && getAttachName() != null){
BodyPart attachmentPart= new MimeBodyPart();
DataSource source = new FileDataSource(getAttachPath());
attachmentPart.setDataHandler(new DataHandler(source));
BASE64Encoder enc = new BASE64Encoder();
attachmentPart.setFileName("=?GBK?B?"+enc.encode(getAttachName().getBytes())+"?=");
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host,getUsername(),getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
private String host = "smtp.163.com";
private String fromAddr = "";
private String toAddr = "";
private String username = "";
private String password = "";
private String title = "发生地方";
private String attachPath = null;
private String attachName = null;
public String getHost(){
return host;
}
public void setHost(String host){
this.host = host;
}
public String getFromAddr(){
return fromAddr;
}
public void setFromAddr(String fromAddr){
this.fromAddr = fromAddr;
}
public String getToAddr(){
return toAddr;
}
public void setToAddr(String toAddr){
this.toAddr = toAddr;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getAttachPath(){
return attachPath;
}
public void setAttachPath(String attachPath){
this.attachPath = attachPath;
}
public String getAttachName(){
return attachName;
}
public void setAttachName(String attachName)throws Exception{
this.attachName = attachName;
}
public static void main(String[] args)
{
MailSender ms = new MailSender();
try {
ms.sendMail();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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 sun.misc.BASE64Encoder;
public class MailSender{
public void sendMail() throws Exception{
Properties props = new Properties();
props.put("mail.smtp.host", getHost());
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(getFromAddr()));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
message.setSubject(getTitle());
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setText("这是我用Java程序发送的第一封邮件!呵呵!!!");//这里就是要发出去的邮件内容啦!
multipart. addBodyPart(contentPart);
if(getAttachPath() != null && getAttachName() != null){
BodyPart attachmentPart= new MimeBodyPart();
DataSource source = new FileDataSource(getAttachPath());
attachmentPart.setDataHandler(new DataHandler(source));
BASE64Encoder enc = new BASE64Encoder();
attachmentPart.setFileName("=?GBK?B?"+enc.encode(getAttachName().getBytes())+"?=");
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host,getUsername(),getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
private String host = "smtp.163.com";
private String fromAddr = "";
private String toAddr = "";
private String username = "";
private String password = "";
private String title = "发生地方";
private String attachPath = null;
private String attachName = null;
public String getHost(){
return host;
}
public void setHost(String host){
this.host = host;
}
public String getFromAddr(){
return fromAddr;
}
public void setFromAddr(String fromAddr){
this.fromAddr = fromAddr;
}
public String getToAddr(){
return toAddr;
}
public void setToAddr(String toAddr){
this.toAddr = toAddr;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getAttachPath(){
return attachPath;
}
public void setAttachPath(String attachPath){
this.attachPath = attachPath;
}
public String getAttachName(){
return attachName;
}
public void setAttachName(String attachName)throws Exception{
this.attachName = attachName;
}
public static void main(String[] args)
{
MailSender ms = new MailSender();
try {
ms.sendMail();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}