JavaMail发送邮件和附件(源码)_3
2007-12-26 10:38
525 查看
User.java
package com.lzw.mail;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.MessagingException;
import com.lzw.string.Stringutils;
/**
* 封装和邮件相关的帐户信息
*
* <pre>
* 属性文件:
* host=
* username=
* password=
* from=
* </pre>
*
* @author 李赵伟 Create: 2007-12-20
*/
public class User {
private String host; // smtp服务器
private String username; // 用户名
private String password; // 密码
private String from; // 发件人地址
private static final String KEY_HOST = "host";
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
private static final String KEY_FROM = "from";
public User(String host, String username, String password)
throws MessagingException {
this(host, username, password, null);
}
public User(String host, String username, String password, String from)
throws MessagingException {
set(host, username, password, from);
}
private void set(String host, String username, String password, String from)
throws MessagingException {
if (!check(host, username, password))
throw new MessagingException("请输入正确的参数!");
setHost(host);
setUsername(username);
setPassword(password);
setFrom(from);
}
/**
* 使用属性文件配置邮件相关的帐户信息
*
* @param propertiesFileName
* 属性文件
* @throws IOException
* @throws MessagingException
*/
public User(String propertiesFileName) throws IOException,
MessagingException {
InputStream is = getClass().getResourceAsStream(propertiesFileName);
readProperties(is);
}
/**
* 使用属性文件配置邮件相关的帐户信息
*
* @param propertiesStream
* 属性文件
* @throws IOException
* @throws MessagingException
*/
public User(InputStream propertiesStream) throws IOException,
MessagingException {
readProperties(propertiesStream);
}
private void readProperties(InputStream is) throws IOException,
MessagingException {
Properties p = new Properties();
try {
p.load(is);
} finally {
if (null != is)
is.close();
}
String host = Stringutils.toGBKString(p.getProperty(KEY_HOST),
Stringutils.ISO8859_1);
String username = Stringutils.toGBKString(p.getProperty(KEY_USERNAME),
Stringutils.ISO8859_1);
String password = Stringutils.toGBKString(p.getProperty(KEY_PASSWORD),
Stringutils.ISO8859_1);
String from = Stringutils.toGBKString(p.getProperty(KEY_FROM),
Stringutils.ISO8859_1);
set(host, username, password, from);
}
private boolean check(String host, String username, String password) {
if (null != host && 0 != host.trim().length() && null != username
&& 0 != username.trim().length() && null != password
&& 0 != password.trim().length())
return true;
else
return false;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
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 getFrom() {
return from;
}
public void setFrom(String from) {
if (null != from && 0 != from.trim().length())
this.from = from;
else {
String a[] = this.host.split("//.");
this.from = this.username + "@" + a[1] + "." + a[2];
}
}
}
package com.lzw.mail;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.MessagingException;
import com.lzw.string.Stringutils;
/**
* 封装和邮件相关的帐户信息
*
* <pre>
* 属性文件:
* host=
* username=
* password=
* from=
* </pre>
*
* @author 李赵伟 Create: 2007-12-20
*/
public class User {
private String host; // smtp服务器
private String username; // 用户名
private String password; // 密码
private String from; // 发件人地址
private static final String KEY_HOST = "host";
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
private static final String KEY_FROM = "from";
public User(String host, String username, String password)
throws MessagingException {
this(host, username, password, null);
}
public User(String host, String username, String password, String from)
throws MessagingException {
set(host, username, password, from);
}
private void set(String host, String username, String password, String from)
throws MessagingException {
if (!check(host, username, password))
throw new MessagingException("请输入正确的参数!");
setHost(host);
setUsername(username);
setPassword(password);
setFrom(from);
}
/**
* 使用属性文件配置邮件相关的帐户信息
*
* @param propertiesFileName
* 属性文件
* @throws IOException
* @throws MessagingException
*/
public User(String propertiesFileName) throws IOException,
MessagingException {
InputStream is = getClass().getResourceAsStream(propertiesFileName);
readProperties(is);
}
/**
* 使用属性文件配置邮件相关的帐户信息
*
* @param propertiesStream
* 属性文件
* @throws IOException
* @throws MessagingException
*/
public User(InputStream propertiesStream) throws IOException,
MessagingException {
readProperties(propertiesStream);
}
private void readProperties(InputStream is) throws IOException,
MessagingException {
Properties p = new Properties();
try {
p.load(is);
} finally {
if (null != is)
is.close();
}
String host = Stringutils.toGBKString(p.getProperty(KEY_HOST),
Stringutils.ISO8859_1);
String username = Stringutils.toGBKString(p.getProperty(KEY_USERNAME),
Stringutils.ISO8859_1);
String password = Stringutils.toGBKString(p.getProperty(KEY_PASSWORD),
Stringutils.ISO8859_1);
String from = Stringutils.toGBKString(p.getProperty(KEY_FROM),
Stringutils.ISO8859_1);
set(host, username, password, from);
}
private boolean check(String host, String username, String password) {
if (null != host && 0 != host.trim().length() && null != username
&& 0 != username.trim().length() && null != password
&& 0 != password.trim().length())
return true;
else
return false;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
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 getFrom() {
return from;
}
public void setFrom(String from) {
if (null != from && 0 != from.trim().length())
this.from = from;
else {
String a[] = this.host.split("//.");
this.from = this.username + "@" + a[1] + "." + a[2];
}
}
}
相关文章推荐
- JavaMail发送邮件和附件(源码)_5
- JavaMail发送邮件和附件(源码)
- JavaMail发送邮件和附件(源码)
- JavaMail发送邮件和附件(源码)_1
- JavaMail发送邮件和附件(源码)
- JavaMail发送邮件和附件(源码)_2
- JavaMail发送邮件和附件(源码)_4
- 使用JavaMail发送邮件之发送带附件邮件一
- Javamail简单的发送邮件附件实例
- 邮件实现详解(四)------JavaMail 发送(带图片和附件)和接收邮件
- javamail发送带附件的邮件
- JavaMail发送邮件(带附件)
- java与JavaMail 发送邮件(包含附件)
- javaMail发送邮件实例,支持附件和多人邮件的发送
- javaMail发送邮件,支持多人发送,多人收信,多附件等
- 用javamail发送带附件的邮件
- Java程序中使用JavaMail发送带图片和附件的邮件
- JavaMail发送带多个附件的邮件,多个收件人,多个抄送人
- javaMail邮件发送功能(多收件人,多抄送人,多密送人,多附件)
- JavaMail实现邮件发送的简单封装(包括文本、图片、html、附件)