摘要:寄送Email程式碼
程式有問題在請通知囉
001
using System;
002
using System.Collections.Generic;
003
using System.Linq;
004
using System.Runtime.Serialization;
005
using System.ServiceModel;
006
using System.Text;
007
using System.Data.SqlClient;
008
using System.Data;
009
using System.Web.SessionState;
010
using System.Web;
011
using System.Web.Security;
012
using System.Net.Mail;
013
using System.Net.Mime;
014
015
public bool SendEmail(string strFrom, List<string> listTo, List<string> listCC, List<string> listBCC, string strSubject, string strBody, List<string> listAttachment, string strIISName)
016
{
017
Boolean blRtn = false;
018
019
if (listTo == null)
020
{
021
listTo = new List<string>();
022
}
023
if (listCC == null)
024
{
025
listCC = new List<string>();
026
}
027
if (listBCC == null)
028
{
029
listBCC = new List<string>();
030
}
031
if (listAttachment == null)
032
{
033
listAttachment = new List<string>();
034
}
035
//檢查必須要有的資料
036
//至少要有一個收件人
037
if ((listTo == null || listTo.Count == 0) && (listCC == null || listCC.Count == 0) && (listBCC == null || listBCC.Count == 0))
038
{
039
blRtn = false;
040
}
041
//要有主旨
042
if (strSubject == null || strSubject.Length == 0)
043
{
044
blRtn = false;
045
}
046
//要有內容
047
if (strBody == null || strBody.Length == 0)
048
{
049
blRtn = false;
050
}
051
052
if (strIISName == null || strIISName.Length == 0)
053
{
054
blRtn = false;
055
}
056
else if (strIISName.Equals("127.0.0.1") || strIISName.ToLower().Equals("localhost"))
057
{
058
listTo.Clear();
059
listCC.Clear();
060
listBCC.Clear();
061
listTo.Add("****@***.***");
062
}
063
064
if (strFrom == null || strFrom.Length == 0)
065
{
066
strFrom = "***@***.***";
067
}
068
069
try
070
{
071
MailMessage newMail = new MailMessage();
072
073
//寄件人
074
MailAddress From = new MailAddress(strFrom);
075
newMail.From = From;
076
077
//收件人
078
for (int i = 0; i < listTo.Count; i++)
079
{
080
MailAddress To = new MailAddress(listTo[i].Trim());
081
newMail.To.Add(To);
082
}
083
084
//副本收件人
085
for (int i = 0; i < listCC.Count; i++)
086
{
087
MailAddress CC = new MailAddress(listCC[i].Trim());
088
newMail.CC.Add(CC);
089
}
090
091
//密件副本收件人
092
for (int i = 0; i < listBCC.Count; i++)
093
{
094
MailAddress BCC = new MailAddress(listBCC[i].Trim());
095
newMail.Bcc.Add(BCC);
096
}
097
098
//附件
099
for (int i = 0; i < listAttachment.Count; i++)
100
{
101
Attachment data = new Attachment(listAttachment[i].Trim(), MediaTypeNames.Application.Octet);
102
ContentDisposition disposition = data.ContentDisposition;
103
disposition.CreationDate = System.IO.File.GetCreationTime(listAttachment[i].Trim());
104
disposition.ModificationDate = System.IO.File.GetLastWriteTime(listAttachment[i].Trim());
105
disposition.ReadDate = System.IO.File.GetLastAccessTime(listAttachment[i].Trim());
106
107
newMail.Attachments.Add(data);
108
}
109
110
//主旨
111
newMail.Subject = strSubject;
112
113
//內容
114
newMail.Body = strBody;
115
//內容編碼方式
116
newMail.BodyEncoding = Encoding.UTF8;
117
//內容是否使用HTML編碼
118
newMail.IsBodyHtml = true;
119
//主旨編碼方式
120
newMail.SubjectEncoding = Encoding.UTF8;
121
122
SmtpClient client = new SmtpClient("****");
123
client.Send(newMail);
124
blRtn = true;
125
}
126
catch (Exception error)
127
{
128
blRtn = false;
129
throw new Exception("寄送信件發生錯誤:" + CtsConvert.CutSQuotation(error.Message));
130
}
131
return blRtn;
132
}
using System; 002
using System.Collections.Generic; 003
using System.Linq; 004
using System.Runtime.Serialization; 005
using System.ServiceModel; 006
using System.Text; 007
using System.Data.SqlClient; 008
using System.Data; 009
using System.Web.SessionState; 010
using System.Web; 011
using System.Web.Security; 012
using System.Net.Mail; 013
using System.Net.Mime; 014
015
public bool SendEmail(string strFrom, List<string> listTo, List<string> listCC, List<string> listBCC, string strSubject, string strBody, List<string> listAttachment, string strIISName) 016

{ 017
Boolean blRtn = false; 018
019
if (listTo == null) 020
{ 021
listTo = new List<string>(); 022
} 023
if (listCC == null) 024
{ 025
listCC = new List<string>(); 026
} 027
if (listBCC == null) 028
{ 029
listBCC = new List<string>(); 030
} 031
if (listAttachment == null) 032
{ 033
listAttachment = new List<string>(); 034
} 035
//檢查必須要有的資料 036
//至少要有一個收件人 037
if ((listTo == null || listTo.Count == 0) && (listCC == null || listCC.Count == 0) && (listBCC == null || listBCC.Count == 0)) 038
{ 039
blRtn = false; 040
} 041
//要有主旨 042
if (strSubject == null || strSubject.Length == 0) 043
{ 044
blRtn = false; 045
} 046
//要有內容 047
if (strBody == null || strBody.Length == 0) 048
{ 049
blRtn = false; 050
} 051
052
if (strIISName == null || strIISName.Length == 0) 053
{ 054
blRtn = false; 055
} 056
else if (strIISName.Equals("127.0.0.1") || strIISName.ToLower().Equals("localhost")) 057
{ 058
listTo.Clear(); 059
listCC.Clear(); 060
listBCC.Clear(); 061
listTo.Add("****@***.***"); 062
} 063
064
if (strFrom == null || strFrom.Length == 0) 065
{ 066
strFrom = "***@***.***"; 067
} 068
069
try 070
{ 071
MailMessage newMail = new MailMessage(); 072
073
//寄件人 074
MailAddress From = new MailAddress(strFrom); 075
newMail.From = From; 076
077
//收件人 078
for (int i = 0; i < listTo.Count; i++) 079
{ 080
MailAddress To = new MailAddress(listTo[i].Trim()); 081
newMail.To.Add(To); 082
} 083
084
//副本收件人 085
for (int i = 0; i < listCC.Count; i++) 086
{ 087
MailAddress CC = new MailAddress(listCC[i].Trim()); 088
newMail.CC.Add(CC); 089
} 090
091
//密件副本收件人 092
for (int i = 0; i < listBCC.Count; i++) 093
{ 094
MailAddress BCC = new MailAddress(listBCC[i].Trim()); 095
newMail.Bcc.Add(BCC); 096
} 097
098
//附件 099
for (int i = 0; i < listAttachment.Count; i++) 100
{ 101
Attachment data = new Attachment(listAttachment[i].Trim(), MediaTypeNames.Application.Octet); 102
ContentDisposition disposition = data.ContentDisposition; 103
disposition.CreationDate = System.IO.File.GetCreationTime(listAttachment[i].Trim()); 104
disposition.ModificationDate = System.IO.File.GetLastWriteTime(listAttachment[i].Trim()); 105
disposition.ReadDate = System.IO.File.GetLastAccessTime(listAttachment[i].Trim()); 106
107
newMail.Attachments.Add(data); 108
} 109
110
//主旨 111
newMail.Subject = strSubject; 112
113
//內容 114
newMail.Body = strBody; 115
//內容編碼方式 116
newMail.BodyEncoding = Encoding.UTF8; 117
//內容是否使用HTML編碼 118
newMail.IsBodyHtml = true; 119
//主旨編碼方式 120
newMail.SubjectEncoding = Encoding.UTF8; 121
122
SmtpClient client = new SmtpClient("****"); 123
client.Send(newMail); 124
blRtn = true; 125
} 126
catch (Exception error) 127
{ 128
blRtn = false; 129
throw new Exception("寄送信件發生錯誤:" + CtsConvert.CutSQuotation(error.Message)); 130
} 131
return blRtn; 132
}
using