msg
File f = new File(
msg
Process p = Runtime
package code
import java
import java
import java
import java
import java
import java
import java
import java
import java
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
public class EmailWriteToFile {
// 定義發件人
private String displayName;
private String to;
private String from;
private String smtpServer;
private String username;
private String password;
private String subject;
private String content;
private boolean ifAuth; // 服務器是否要身份認證
private String filename =
private Vector file = new Vector(); // 用於保存發送附件的文件名的集合
private String contentType =
private String charset =
public void addFile(String filename) {
file
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
ntentType = contentType;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this
}
/**
* 設置SMTP服務器地址
*/
public void setSmtpServer(String smtpServer) {
this
}
/**
* 設置發件人的地址
*/
public void setFrom(String from) {
this
}
/**
* 設置顯示的名稱
*/
public void setDisplayName(String displayName) {
this
}
/**
* 設置服務器是否需要身份認證
*/
public void setIfAuth(boolean ifAuth) {
this
}
/**
* 設置E
*/
public void setUserName(String username) {
this
}
/**
* 設置E
*/
public void setPassword(String password) {
this
}
/**
* 設置接收者
*/
public void setTo(String to) {
this
}
/**
* 設置主題
*/
public void setSubject(String subject) {
this
}
/**
* 設置主體內容
*/
public void setContent(String content) {
ntent = content;
}
public EmailWriteToFile() {
}
private int port =
public int getPort() {
return port;
}
public void setPort(int port) {
this
}
/**
* 發送郵件
*
* @throws IOException
* @throws FileNotFoundException
*/
public boolean send() throws FileNotFoundException
HashMap<String
map
String message =
Session session = null;
Properties props = System
props
props
try {
props
session = Session
session
Transport trans = null;
Message msg = new MimeMessage(session);
try {
Address from_address = new InternetAddress(from
msg
} catch (java
e
}
InternetAddress[] address = { new InternetAddress(to) };
msg
msg
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp
mp
if (!file
Enumeration efile = file
while (efile
mbp = new MimeBodyPart();
filename = efile
FileDataSource fds = new FileDataSource(filename); // 得到數據源
mbp
mbp
mp
}
file
}
msg
msg
// 發送信件
msg
File f = new File(
msg
} catch (AuthenticationFailedException e) {
map
message =
e
return false;
} catch (MessagingException e) {
message =
map
e
Exception ex = null;
if ((ex = e
System
ex
}
return false;
}
// System
map
return true;
}
public static void main(String[] args) throws FileNotFoundException
EmailWriteToFile o = new EmailWriteToFile();
o
o
o
o
o
o
o
o
o
Process p = Runtime
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25757.html