从网上找了一个封装.Net的Smtp邮件发送的类
class MailSender { private MailMessage mailMessage; private SmtpClient smtpClient; private string password;//发件人密码 /// <summary> /// 处审核后类的实例 /// </summary> /// <param name="To">收件人地址</param> /// <param name="From">发件人地址</param> /// <param name="Body">邮件正文</param> /// <param name="Title">邮件的主题</param> /// <param name="Password">发件人密码</param> public MailSender(string To, string From, string Body, string Title, string Password) { mailMessage = new MailMessage(); mailMessage.To.Add(To); mailMessage.From = new System.Net.Mail.MailAddress(From); mailMessage.Subject = Title; mailMessage.Body = Body; mailMessage.IsBodyHtml = true; mailMessage.BodyEncoding = System.Text.Encoding.UTF8; mailMessage.Priority = System.Net.Mail.MailPriority.Normal; this.password = Password; } /// <summary> /// 添加附件 /// </summary> public void Attachments(string Path) { string[] path = Path.Split(','); Attachment data; ContentDisposition disposition; for (int i = 0; i < path.Length; i++) { data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件 disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期 disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期 disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期 mailMessage.Attachments.Add(data);//添加到附件中 } } /// <summary> /// 异步发送邮件 /// </summary> /// <param name="CompletedMethod"></param> public void SendAsync(SendCompletedEventHandler CompletedMethod) { if (mailMessage != null) { smtpClient = new SmtpClient(); smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtpClient.Host = "smtp." + mailMessage.From.Host; smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件 smtpClient.SendAsync(mailMessage, mailMessage.Body); } } /// <summary> /// 发送邮件 /// </summary> public void Send() { if (mailMessage != null) { smtpClient = new SmtpClient(); smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtpClient.Host = "smtp." + mailMessage.From.Host; smtpClient.Send(mailMessage); } } }
使用某些邮箱的时候会报错(如sohu):不允许使用邮箱名称,服务器响应为: xxx。需要设置单独的用户名,故修改了下,并做了一些优化。如下:
class MailSender { private readonly MailMessage mail; private readonly string password;//发件人密码 private readonly string user; /// <summary> /// 处审核后类的实例 /// </summary> /// <param name="to">收件人地址</param> /// <param name="from">发件人地址</param> /// <param name="body">邮件正文</param> /// <param name="subject">邮件的主题</param> /// <param name="sendAccount">发件人账号</param> /// <param name="sendPassword">发件人密码</param> public MailSender(string to, string from, string body, string subject, string sendAccount, string sendPassword) { mail = new MailMessage(); mail.To.Add(to); mail.From = new MailAddress(from); mail.Subject = subject; mail.Body = body; mail.IsBodyHtml = true; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.Priority = MailPriority.Normal; this.password = sendPassword; this.user = sendAccount; } /// <summary> /// 添加附件 /// </summary> public void Attachments(string path) { string[] pathes = path.Split(','); for (int i = 0; i < pathes.Length; i++) { var data = new Attachment(pathes[i], MediaTypeNames.Application.Octet);//实例化附件 var disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(pathes[i]);//获取附件的创建日期 disposition.ModificationDate = System.IO.File.GetLastWriteTime(pathes[i]);//获取附件的修改日期 disposition.ReadDate = System.IO.File.GetLastAccessTime(pathes[i]);//获取附件的读取日期 mail.Attachments.Add(data);//添加到附件中 } } /// <summary> /// 异步发送邮件 /// </summary> /// <param name="CompletedMethod"></param> public void SendAsync(SendCompletedEventHandler CompletedMethod) { if (mail != null) { var smtpClient = new SmtpClient(); smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据 smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Host = "smtp." + mail.From.Host; smtpClient.SendCompleted += CompletedMethod;//注册异步发送邮件完成时的事件 smtpClient.SendAsync(mail, mail.Body); } } /// <summary> /// 发送邮件 /// </summary> public void Send() { if (mail != null) { var smtpClient = new SmtpClient(); smtpClient.Credentials = new System.Net.NetworkCredential(user, password);//设置发件人身份的票据 smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Host = "smtp." + mail.From.Host; smtpClient.Send(mail); } } }
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* xml常用工具类
*/
public class XmlUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(XmlUtils.class);
public static NodeList runXpath(Node doc, String xquery, NamespaceContext ns) {
NodeList list = null;
try {
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
if (null != ns) {
xpath.setNamespaceContext(ns);
}
XPathExpression expression = xpath.compile(xquery);
Object result = expression.evaluate(doc, XPathConstants.NODESET);
list = (NodeList) result;
} catch (Exception e) {
LOGGER.warn("XMLUtils: unable to evaluate xpath", e);
LOGGER.error("", e);
}
return list;
}
public static String getValueByXpath(Node doc, String xquery) {
try {
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expression = xpath.compile(xquery);
String result = (String) expression.evaluate(doc, XPathConstants.STRING);
return result;
} catch (Exception e) {
LOGGER.error("", e);
LOGGER.warn("XMLUtils: unable to evaluate xpath", e);
}
return null;
}
public static String nodeAsString(Node node) {
String nodeStr = "";
TransformerFactory tff = TransformerFactory.newInstance();
try {
Transformer tf = tff.newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
tf.transform(new DOMSource(node), new StreamResult(bos));
return bos.toString("UTF-8");
} catch (Exception e) {
LOGGER.warn("XMLUtils#nodeAsString: ", e);
LOGGER.error("", e);
}
return nodeStr;
}
public static Document getW3CDom(String xmlStr) throws ParserConfigurationException,
SAXException, IOException {
StringReader sr = new StringReader(xmlStr);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
return doc;
}
}
GridFTP 安装
该页面描述了安装和配置GridFTP服务的向导,有任何问题都可以联系 Bogdan Ludwiczak.
Globus Toolkit/GridFTP - 安装本小节大概描述一下 Globus Toolkit ver. 4.x 的安装过程.
Globus Toolkit 提供了两个安装版本,一个是二进制的,一个是源码包的。它也提供了一个可用的完全安装版本,但是只作为 Java WS-core 的一部分。 在介绍安装步骤之前,需要说明一下 QosCosGrid 的基础安装步骤,其中的一部分就是 GridFTP 相关的内容。
下面是编译和安装 Globus Toolkit ,其中包括 GridFTP 后台进程的说明。
备注: 这是一个关于安装GridFTP服务的离线安装过程,所有详细的信息可以查看官方的 Globus文档 (Globus documentation)。
- 检查目标系统是否有下列软件:
- gcc (latest 3.4 or 4.1)
- GNU tar, sed, make
- Perl >5.6
- 创建一个专用的,没有特权的用户,名字为 globus 。用这个用户来安装和部署 Globus Toolkit 过程,并做一些管理相关的任务。
- 创建部署目录。
- 确认用户 globus 拥有在安装目录里面的读写权限。
备注: 使用 globus 这个用户执行下面的步骤.
- 下载和解压 Globus Toolkit ver.4.2.x 所有的源码安装包 (可以的下载地址为 http://www.globus.org/toolkit/downloads/4.2.1/#source Globus Toolkit )
- 更改当前的工作目录为源码目录 (比如 ~/gt4.2.x-all-source-installer)
- 设置环境变量 GLOBUS_LOCATION 指向安装目录 (e.g. /opt/globus/gt421)
- 然后使用 ./configure 来设置编译和安装选项。至少也要使用一些 --prefix 选项来设置部署目录。
./configure --prefix=${GLOBUS_LOCATION}
./configure --disable-system-openssl --prefix=${GLOBUS_LOCATION}
- 编译并安装 Globus Toolkit 使用下面的命令:
make gridftp make install
make gridftp 2>&1 | tee build.log make install
- GT4 不像其他的软件一样会直接生产目标位置 ($GLOBUS_LOCATION)
- 假如你忽略 gridftp 来make整个的 Globus Toolkit 的话,将会花费好几个小时才能完成。
- 在执行 make install 来解析Globus 配置文件的时候将会被初始化。
- 你也可以单独选择编译 GridFTP 服务器或者客户端工具,通过指定 globus_gridftp_服务器或者 globus-data-management-client 这两个选项来完成。
下面的步骤可以完成 Globus Toolkit/GridFTP 安装。 下面的命令是典型的安装过程,shell 命令如下:
bogdanl@cress ~ $ su - cress ~ # useradd -m globus cress ~ # mkdir /opt/globus cress ~ # chown globus:users /opt/globus cress ~ # su - globus globus@cress ~ $ wget http://www-unix.globus.org/ftppub/gt4/4.2.1/installers/src/gt4.2.1-all-source-installer.tar.bz2 globus@cress ~ $ tar xjf gt4.2.1-all-source-installer.tar.bz2 globus@cress ~ $ export GLOBUS_LOCATION=/opt/globus/gt421 globus@cress ~ $ cd gt4.2.1-all-source-installer globus@cress ~/gt4.2.1-all-source-installer $ ./configure --prefix=${GLOBUS_LOCATION} \ globus@cress ~/gt4.2.1-all-source-installer $ make gridftp cd gpt && OBJECT_MODE=32 ./build_gpt ... Your build completed successfully. Please run make install. globus@cress ~/gt4.2.1-all-source-installer $ make install
在 Globus Toolkit 安装完成之后, 在启动 Globus 服务之前,有好几个方面的 GSI 安全相关的项需要配置:
- 配置 Globus (and thus GridFTP daemon) 的认证证书,需要设置 CAs (Certificate Authorities), 例如. 放置信任的认证证书在 CAs 指定的目录里。 - CA 只在CA证书存在,并且名字和目录是合法的情况下才是一个被信任的CA证书。 然而, pre-ws 服务(包括 GridFTP), 签名的公有密钥文件也必须存在相同的位置。 换句话说,需要给出两个被信任的CA文件:
- cert_hash.0 - the trusted CA certificate and
- cert_hash.signing_policy - the signing policy.Globus services and tools looks for that directory in following locations:
- the value of $X509_CERT_DIR environment variable if it is set and the directory exists,
- otherwise, in $HOME/.globus/certificates if it exists,
- otherwise, in /etc/grid-security/certificates if it exists,
- otherwise, in $GLOBUS_LOCATION/share/certificates if it exists.
备注: 我们建议使用 /etc/grid-security/certificates 作为系统认证 CAs 的目录, 记住$X509_CERT_DIR 和 $HOME/.globus/certificates 有更高级的优先权。
The cert_hash.0, i.e. certificate of the CA, is provided by CA, 通常使用一些合适的哈希名。哈希名由8个16进制数字和后缀 ".0"组成 (例如8a661490.0). 哈希值可以用下面的命令来获得 ( $GLOBUS_LOCATION/bin/ 下面的命令必须可用):
openssl x509 -hash -noout -in ca_certificate
- cert_hash.signing_policy 通常也由 CA提供, 但是它可以手工构造. 这个签名的方针文件有下面的格式:
access_id_CA X509 'CA Distinguished Name' pos_rights globus CA:sign cond_subjects globus '"Name Pattern1" "Name Pattern2" ...'
openssl x509 -subject -noout -in cert_hash.0
- "Name patternX" 是一个字符串,被用来匹配CA证书给出的正确的名字,CA的名字通常是一个带 * 号的正则表达式。例如:
"/C=PL/O=GRID/CN=Polish Grid CA" -> '"/C=PL/O=GRID/*"'
- 通过 grid-cert-request 命令来配置正确的缺省值,一遍生成认证请求。下面的文件需要正确的配置来启用 Globus tools 来生成验证证书的请求:
- /etc/grid-security/globus-user-ssl.conf - defines the distinguished name to use for a user's certificate request.
- /etc/grid-security/globus-host-ssl.conf - defines the distinguished name for a host and service certificate request.
- /etc/grid-security/grid-security.conf - is a main configuration file that contains the name and email address for the given CA.
这些文件通常由 CA 提供, i.e. PL-Grid. 典型的 CA 配置文件被放置在 /etc/grid-security/certificates/ 目录,有一些附加的扩展.CA_hash_name 和对应的符号链接备创建在 /etc/grid-security/里。 Globus Toolkit提供了 grid-default-ca 命令,该命令会自动创建合适的链接。
请求主机和用户的 X.509 证书所有的 Globus 服务(包括GridFTP) 都需要一个主机 (or service) 认证操作过程。 每个用户都需要一个用户认证来使用 Globus 服务。你可以使用 Globus命令 grid-cert-request 来生成 host/users 认证请求,然后发送到你的 CA 来进行签名。 确认 CA 配置文件和认证是在你生成请求之前。这么命令创建了3个文件: