📄 sendemaildemo.java
字号:
package com.shfe.mail;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
public class SendEmailDemo {
private Properties props = System.getProperties();
private Session session = null;
private Transport transport = null;
//private Message msg = null;
private MimeMessage msg = null;
private MimeMultipart mm = new MimeMultipart();
private MimeBodyPart mbp = new MimeBodyPart();
private String auth = "false";
private final static String mailer = "netkiller";
private String username = null;
private String password = null;
private String host = null;
private String from = null;
private String to = null;
private String cc = "";
private String bcc = "";
private String subject = "";
private String text = "";
private String footer = "";
private boolean debug = false;
public SendEmailDemo() {
}
public void createSession() throws Exception {
session = Session.getInstance(props, null);
session.setDebug(debug);
transport = session.getTransport("smtp");
transport.connect(host, username, password);
msg = new MimeMessage(session);
}
public void setDebug(boolean debug) throws Exception {
this.debug = debug;
}
public void setSmtpHost(String str) throws Exception {
this.host = str;
props.put("mail.smtp.host", host);
}
public void setAuth(boolean auth){
if(auth){
props.put("mail.smtp.auth", "true");
}
}
public void setUsername(String user){
this.username = user;
props.put("mail.smtp.user",username);
}
public void setPassword(String pass){
this.password = pass;
props.put("mail.smtp.password",password);
}
public String getUsername(){
return this.username;
}
public String getPassword(){
return this.password;
}
public void setFrom(String str) throws Exception {
this.from = str;
msg.setFrom(new InternetAddress(from));
}
public void setTo(String str) throws Exception{
this.to = str;
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
}
public void setCC(String str) throws Exception{
this.cc = str;
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc, false));
}
public void setBCC(String str) throws Exception{
this.bcc = str;
msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc, false));
}
public void setSubject(String str) throws Exception{
this.subject = str;
msg.setSubject(subject);
}
public void setText(String str) throws Exception{
this.text = str;
this.text += this.footer;
// msg.setText(this.text);
BodyPart bp = new MimeBodyPart();
bp.setContent(text,"text/plain; charset=gb2312");
mm.addBodyPart(bp);
msg.setContent(mm);
}
public void setFooter(String str){
this.footer = str;
}
public void setHtml(String str) throws Exception{
StringBuffer sb = new StringBuffer();
sb.append("<HTML>\n");
sb.append("<HEAD>\n");
sb.append("<TITLE>\n");
sb.append(subject + "\n");
sb.append("</TITLE>\n");
sb.append("</HEAD>\n");
sb.append("<BODY>\n");
sb.append("<H1>" + subject + "</H1>" + "\n");
sb.append(str);
sb.append(this.footer);
sb.append("\n");
sb.append("</BODY>\n");
sb.append("</HTML>\n");
this.text = sb.toString();
BodyPart bp = new MimeBodyPart();
bp.setContent(text,"text/html; charset=gb2312");
mm.addBodyPart(bp);
msg.setContent(mm);
}
public boolean addFileAffix(String filename) {
boolean bool = false;
try{
MimeBodyPart bp = new MimeBodyPart();
bp.setHeader("Content-Type","image/jpeg");
bp.setHeader("Content-Transfer-Encoding", "base64");
bp.setHeader("Content-Disposition", "attachment");
// BodyPart bp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fds));
//bp.setFileName(fds.getName());
bp.setFileName(MimeUtility.encodeWord(fds.getName(), "GB2312",null));
bp.setHeader("Content-ID",fds.getName());
mm.addBodyPart(bp);
//msg.setContent(mm);
bool = true;
}
catch(Exception e){
System.err.println("增加邮件附件:"+filename+"发生错误!"+e);
}
System.out.println("增加邮件附件:"+filename);
return bool;
}
public void send(){
try{
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Mail was recorded successfully.");
} catch (MessagingException e) {
//e.printStackTrace();
System.out.println(e.toString());
}
}
public static void main(String[] args) {
try{
SendEmailDemo am = new SendEmailDemo();
am.setDebug(true);
am.setSmtpHost("192.168.19.35");
am.createSession();
am.setFrom("SI50@shfe.com.cn");
am.setTo("os9@21cn.com");
am.setSubject("hi fruit come!");
am.setText("<a href=xxx><img src='cid:fruit.png'>水果</a>");
am.setHtml("水果市场统计信息<br>"+"<a href=xxx><img src='fruit.jpg'></a>");
am.setFooter("\n<br>====================\n<br>netkiller");
am.addFileAffix("c:/fruit.jpg");
//am.addFileAffix("c:/gomine.properties");
am.send();
}
catch(Exception e){
System.err.println(e.toString());
//e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -