.Net写的Mail Util(C#)
来源:互联网 发布:92game源码破解 编辑:程序博客网 时间:2024/05/18 15:56
前两天用.Net写了个发送mail的util,贴上来。刚接触C#,呵呵。。。。
.net 1.1的。.net 2.0里面关于Mail的API有所变动。 .code { font-size: 12; font-family: "lucida console", sans-serif; background-color: #E1E1E1; }.com { color: #00DD00; }.nlit { color: #008800; }.slit { color: #880000; }.kw { color: #0000AA; }.norm { color: #000000; }.pp { color: #888800; }
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Mail;
namespace Izhufu.utils{
/// <summary>
/// Summary description for SMTPClient.
/// SMTPClient is just used for SMTPMail
/// </summary>
public class SMTPClient {
private readonly string DELIMITER=";";
private MailMessage message;
private string[] s_cc;
private string[] s_bcc;
private int s_port;
private bool s_needauth;
private string s_username;
private string s_password;
//constructor
public SMTPClient (string from, string[] to, string subject, string server) {
message=new MailMessage();
message.From=from;
message.To=concatMailAddress(to, DELIMITER);
message.Subject=subject;
SmtpMail.SmtpServer=server;
}
//setter for mail server port
public void SetPort (int port){
this.s_port=port;
}
//setter for mail priority, for example: SetPrioprity(MailPriority.HIGH)
public void SetPriority (MailPriority priority){
message.Priority=priority;
}
//setter for mail server auth configuration
public void SetNeedAuth (bool needauth){
this.s_needauth=needauth;
}
//setter for mail server user name
public void SetUserName (string username){
this.s_username=username;
}
//setter for mail server password
public void SetPassword (string pwd){
this.s_password=pwd;
}
//setter for mail body format html?or text for exampel: SetFormat(MailFormat.Text)
public void SetFormat (MailFormat format){
message.BodyFormat=format;
}
//setter for mail body
public void SetBody (string body){
message.Body=body;
}
//setter for mail body encoding, for example: SetEncoding(Encoding.ASCII
public void SetEncoding (Encoding encoding){
message.BodyEncoding=encoding;
}
//add attachment to mail
public void AddAttachment (MailAttachment attachement){
message.Attachments.Add(attachement);
}
//set cc
public void SetCC (string[] cc){
this.s_cc=cc;
}
//set bcc
public void SetBcc (string[] bcc){
this.s_bcc=bcc;
}
//private method to concat the mail address with specified delimiter
private string concatMailAddress (string[] addresses, string delimiter){
if (addresses==null) return null;
if (addresses.Length==1) return addresses[0];
string concated="";
for (int i=0; i<addresses.Length-1; i++) {
concated+=addresses[i]+delimiter;
}
concated+=addresses[addresses.Length-1];
return concated;
}
//send the mail, if this mail is sent successfully return true, otherwise return false
public bool SendMail (){
if (s_cc!=null && s_cc.Length>0) {
message.Cc=concatMailAddress(s_cc, DELIMITER);
}
if (s_bcc!=null && s_bcc.Length>0) {
concatMailAddress(s_bcc, DELIMITER);
}
if (s_needauth && s_username!=null && s_password!=null) {
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", s_username);
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", s_password);
}
try {
SmtpMail.Send(message);
}
catch {
return false;
}
return true;
}
}
//End of SMTPClient
}
.net 1.1的。.net 2.0里面关于Mail的API有所变动。 .code { font-size: 12; font-family: "lucida console", sans-serif; background-color: #E1E1E1; }.com { color: #00DD00; }.nlit { color: #008800; }.slit { color: #880000; }.kw { color: #0000AA; }.norm { color: #000000; }.pp { color: #888800; }
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Mail;
namespace Izhufu.utils{
/// <summary>
/// Summary description for SMTPClient.
/// SMTPClient is just used for SMTPMail
/// </summary>
public class SMTPClient {
private readonly string DELIMITER=";";
private MailMessage message;
private string[] s_cc;
private string[] s_bcc;
private int s_port;
private bool s_needauth;
private string s_username;
private string s_password;
//constructor
public SMTPClient (string from, string[] to, string subject, string server) {
message=new MailMessage();
message.From=from;
message.To=concatMailAddress(to, DELIMITER);
message.Subject=subject;
SmtpMail.SmtpServer=server;
}
//setter for mail server port
public void SetPort (int port){
this.s_port=port;
}
//setter for mail priority, for example: SetPrioprity(MailPriority.HIGH)
public void SetPriority (MailPriority priority){
message.Priority=priority;
}
//setter for mail server auth configuration
public void SetNeedAuth (bool needauth){
this.s_needauth=needauth;
}
//setter for mail server user name
public void SetUserName (string username){
this.s_username=username;
}
//setter for mail server password
public void SetPassword (string pwd){
this.s_password=pwd;
}
//setter for mail body format html?or text for exampel: SetFormat(MailFormat.Text)
public void SetFormat (MailFormat format){
message.BodyFormat=format;
}
//setter for mail body
public void SetBody (string body){
message.Body=body;
}
//setter for mail body encoding, for example: SetEncoding(Encoding.ASCII
public void SetEncoding (Encoding encoding){
message.BodyEncoding=encoding;
}
//add attachment to mail
public void AddAttachment (MailAttachment attachement){
message.Attachments.Add(attachement);
}
//set cc
public void SetCC (string[] cc){
this.s_cc=cc;
}
//set bcc
public void SetBcc (string[] bcc){
this.s_bcc=bcc;
}
//private method to concat the mail address with specified delimiter
private string concatMailAddress (string[] addresses, string delimiter){
if (addresses==null) return null;
if (addresses.Length==1) return addresses[0];
string concated="";
for (int i=0; i<addresses.Length-1; i++) {
concated+=addresses[i]+delimiter;
}
concated+=addresses[addresses.Length-1];
return concated;
}
//send the mail, if this mail is sent successfully return true, otherwise return false
public bool SendMail (){
if (s_cc!=null && s_cc.Length>0) {
message.Cc=concatMailAddress(s_cc, DELIMITER);
}
if (s_bcc!=null && s_bcc.Length>0) {
concatMailAddress(s_bcc, DELIMITER);
}
if (s_needauth && s_username!=null && s_password!=null) {
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", s_username);
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", s_password);
}
try {
SmtpMail.Send(message);
}
catch {
return false;
}
return true;
}
}
//End of SMTPClient
}
- .Net写的Mail Util(C#)
- Ruby 写的 Mail Proxy
- JavaMail的com/sun/mail/util/LineInputStream错误 以及解决办法
- java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream的处理
- SEND E-MAIL ASP.NET 发送E-Mail的代码
- com/sun/mail/util/LineInputStream
- com/sun/mail/util/LineInputStream
- [VB.NET]vb.net 调用c写的dll问题
- Asp.net中的mail的发送
- System.Net.Mail的一些进阶示例
- 用vs2008的Net.Mail发邮件
- 发现System.Net.Mail 的一个BUG
- System.Net.Mail空间的学习
- 自己写的分页Util类
- Java mail包出现"MimeMessage contains object of type javax.mail.util.SharedByteArrayInputStream instead of MimeMultipart"错误的修正办法
- C# .net写的程序调用C++.net写的动态库装载失败,原因一句说清
- .net 2.0 下发送邮件的方式。System.Web.Mail 过时用System.Net.Mail
- 将图片写到excel里的函数 c#.net
- 名人谈健康
- 简要解析软件测试的认识误区
- 使用ATL开发AJAX控件即XMLHttpRequest控件的经验体会
- 为AJAX实现互斥
- IT好么?
- .Net写的Mail Util(C#)
- 选Ruby还是选Python?
- 用Vc6编译GuitoolKit
- 终止一個正在发送的表单
- Linux 内核解读入门
- Servlet概述
- Spring MVC框架的高级配置
- Windows 2000与Web应用开发者
- 控制到 SQL Server 的连接发出的 Transact-SQL 语句的锁定行为和行版本控制行为